Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
New Member

Run java code from jar file with set step

Hi,

based on article

https://supportforums.cisco.com/document/98986/uccx-quick-java-recipes

i copied the code from 1.) and modified them a little bit to my reqiurements. The only reqiurement is, give the script a parameter (here the credit card number and return the result to UCCX(maybe the last reqiurement is missing).


Following the code:

###############################################################################

 

public class ClassCheckCC {
    

        public static void main (String args[]) {
            // TODO Auto-generated method stub
            String ccnumber = args[0];
            //String ccnumber = "66666666666666666";
            int isValid=99;
            isValid=verifyCC(ccnumber);
            System.out.println(isValid);
            }
        
        
        
        
        private static int verifyCC(String ccnumber){        
            int sum = 0;
            boolean alternate = false;
            boolean isValid = false;
            try {
            int i = 0;
            for (i = ccnumber.length() - 1; i >= 0; i--) {
              int n = Integer.parseInt(ccnumber.substring(i, i + 1));
              if (alternate) {
               n = n * 2;
               if (n > 9) {
                n = (n % 10) + 1;
               }
              }
              sum += n;
              alternate = !alternate;
            }
             isValid = (sum % 10 == 0);

            } catch (Exception e) {

            return -1;

            }

            return (isValid == true ? 1 : 0);
        }
    }

###############################################################################

In Eclipse i create a new java project named "CheckCreditCardNumber" and inserted this code into the new project. The name of the new class is "ClassCheckCC". Than i created a executable jar file "CheckCCNumber1.jar". This will run as expected in a DOS box.
I call him in this manner: java -jar CheckCCNumber1.jar 1234567890123456

The number represents a credit card number.

 

From my opinion, always ready to implement it into UCCX.

JAR file uploaded to UCCX (you can find it as an attachment here), moved on the right side (to Selected Classpath Entries:). Some service restarted. Do some tries to access the file and can see it in the UCCX editor.

 

First try to declare all needed variables.

Next i created a new script file and inserted a set step

 

set step to call the java code

the following error occures:

error message

But the expected result from java code from type byte(-1,0, or 1). Shit happens, i declared the variable isValid as string:

variable isValid now declared as string

One more try to call the java code:

set step to call the java code

 

At next the error message is different:

set step not accepted

 

So, here my questions. What´s wrong? Where can i successful call this little java code.

I know, it´s possible to execute the code without additional jar files. For future purposes, i am interessted to implement more java classes into a jar file and this is a first step.

 

Greetings Maic Naatz

 

1 ACCEPTED SOLUTION

Accepted Solutions

Hi,er... let's get a few

Hi,

er... let's get a few things straight if you don't mind.

Your class is valid, as you said, you tested it with Eclipse, which is a fair way to go. You have two methods there, "main" and a private static "verifyCC" returning an int value. Good. Now if I take a look at your UCCX script I can see some interesting stuff there. First, you create a new variable of type ClassCheckCC, with the name CheckCC and with null as the default value. Cool, it's like telling UCCX's JRE to "hey, we are going to use a new variable of type ClassCheckCC", or more precisely:

ClassCheckCC CheckCC = null;

Alrighty. Next, you sort of try to assign the value of isValid by saying CheckCC(ccnumber). Why this is incorrect: in Java this actually does:

isValid = CheckCC(ccnumber);

Do you already see? The above code actually means:

isValid = null(ccnumber); // invalid!

CheckCC has null value.

Let's try a slightly different approach:

public class CheckCC {
        public static void main (String args[]) {
    System.out.println("Move along, nothing to see here, call verifyCC(String s) instead.");
            }
        public static int verifyCC(String ccnumber) {
            int sum = 0;
            boolean alternate = false;
            boolean isValid = false;
            try {
            int i = 0;
            for (i = ccnumber.length() - 1; i >= 0; i--) {
              int n = Integer.parseInt(ccnumber.substring(i, i + 1));
              if (alternate) {
               n = n * 2;
               if (n > 9) {
                n = (n % 10) + 1;
               }
              }
              sum += n;
              alternate = !alternate;
            }
             isValid = (sum % 10 == 0);
            } catch (Exception e) {
                return -1;
            }
            return (isValid == true ? 1 : 0);
        }
    }


You might have noticed I made some adjustments. I renamed ClassCheckCC to the simpler CheckCC, I changed the method modifier of verifyCC to public. This is actually a great example of a utility class where you don't need any instances, so we should let JDK do some code optimization.

Using this approach, we don't need to create object instances - they are unnecessary in this case. Simply calling CheckCC.verifyCC(String aString) is absolutely sufficient. We can, as I will demonstrate, but again, why should we create new class instances each time a script is run.

The following three constructs are valid (assuming I have created the necessary variables: val, validCCNumber and invalidCCNumber):

Set valid = CheckCC.verifyCC(validCCNumber)

Plain and simple. I just call the static verifyCC method on class CheckCC with a parameter.

Set valid = { return CheckCC.verifyCC(invalidCCNumber); }

I just padded the above one liner with curly braces, effectively creating a code block, so semicolon and the return keyword became necessary.

Set valid = { CheckCC instance = new CheckCC(); return instance.verifyCC(validCCNumber); }

Now, this is the absolutely redundant but working example. I created an instance of the class CheckCC, named it instance and then ran the verifyCC method on the newly created class instance. Again, this is a situation when a developer is bored and has nothing better to do but creating class instances and sending them to the eternal bytefields.

G.

Here's a screenshot:

 

 

4 REPLIES

 CheckCC is your Java Class.

 

CheckCC is your Java Class..you are not specifying a Method for the Java Class you are trying to utilize; if this doesn't work then something is wrong with your JAR: CheckCC.verifyCC(ccnumber) which returns int so I would probably match isValid with that type..

 

Hi,er... let's get a few

Hi,

er... let's get a few things straight if you don't mind.

Your class is valid, as you said, you tested it with Eclipse, which is a fair way to go. You have two methods there, "main" and a private static "verifyCC" returning an int value. Good. Now if I take a look at your UCCX script I can see some interesting stuff there. First, you create a new variable of type ClassCheckCC, with the name CheckCC and with null as the default value. Cool, it's like telling UCCX's JRE to "hey, we are going to use a new variable of type ClassCheckCC", or more precisely:

ClassCheckCC CheckCC = null;

Alrighty. Next, you sort of try to assign the value of isValid by saying CheckCC(ccnumber). Why this is incorrect: in Java this actually does:

isValid = CheckCC(ccnumber);

Do you already see? The above code actually means:

isValid = null(ccnumber); // invalid!

CheckCC has null value.

Let's try a slightly different approach:

public class CheckCC {
        public static void main (String args[]) {
    System.out.println("Move along, nothing to see here, call verifyCC(String s) instead.");
            }
        public static int verifyCC(String ccnumber) {
            int sum = 0;
            boolean alternate = false;
            boolean isValid = false;
            try {
            int i = 0;
            for (i = ccnumber.length() - 1; i >= 0; i--) {
              int n = Integer.parseInt(ccnumber.substring(i, i + 1));
              if (alternate) {
               n = n * 2;
               if (n > 9) {
                n = (n % 10) + 1;
               }
              }
              sum += n;
              alternate = !alternate;
            }
             isValid = (sum % 10 == 0);
            } catch (Exception e) {
                return -1;
            }
            return (isValid == true ? 1 : 0);
        }
    }


You might have noticed I made some adjustments. I renamed ClassCheckCC to the simpler CheckCC, I changed the method modifier of verifyCC to public. This is actually a great example of a utility class where you don't need any instances, so we should let JDK do some code optimization.

Using this approach, we don't need to create object instances - they are unnecessary in this case. Simply calling CheckCC.verifyCC(String aString) is absolutely sufficient. We can, as I will demonstrate, but again, why should we create new class instances each time a script is run.

The following three constructs are valid (assuming I have created the necessary variables: val, validCCNumber and invalidCCNumber):

Set valid = CheckCC.verifyCC(validCCNumber)

Plain and simple. I just call the static verifyCC method on class CheckCC with a parameter.

Set valid = { return CheckCC.verifyCC(invalidCCNumber); }

I just padded the above one liner with curly braces, effectively creating a code block, so semicolon and the return keyword became necessary.

Set valid = { CheckCC instance = new CheckCC(); return instance.verifyCC(validCCNumber); }

Now, this is the absolutely redundant but working example. I created an instance of the class CheckCC, named it instance and then ran the verifyCC method on the newly created class instance. Again, this is a situation when a developer is bored and has nothing better to do but creating class instances and sending them to the eternal bytefields.

G.

Here's a screenshot:

 

 

New Member

Hi Gergely,this is an

Hi Gergely,

this is an excellent answer to my question. Also non java experts can reproduce this one. Many thanks to you!smiley

 

The code you provides is valid, all three set step examples are working as expected. So i can continue with my project...

 

 

Hi,thanks, always happy to

Hi,

thanks, always happy to help. Let us know if you need more help.

And thanks for the rating.

G.

575
Views
0
Helpful
4
Replies
CreatePlease to create content