Java code in the expression editor

Answered Question
Mar 2nd, 2007

The Java tab in the expression editor makes me think it should somehow be possible to write a bunch of java code lines.. but no matter what I try it is always refused. The only java code I got to work was creating a set command, and then write the right hand side part of a

var x = something;

statement. Since there are try/catch, if, while, etc. statements in the java tab, shouldn't it be somehow possible to use them? And if so, which element(s) can they be used in (quite apparently the set element is the wrong one)?

I have this problem too.
0 votes
Correct Answer by Stefano Amici about 9 years 6 months ago

I'm using IPCC 4.5 with Premium License and i'm able to write java code inside set command.

The java code must be write between { } brackets; for example

{

try{

int i=0;

int result = 0;

for(i=0;i<4;i++){

result = result+i;

}

return result;

}catch(Exception ex){

return -1;

}

}

Hope this help.

Stefano

  • 1
  • 2
  • 3
  • 4
  • 5
Overall Rating: 3 (1 ratings)
Loading.
Correct Answer
Stefano Amici Fri, 03/02/2007 - 09:02

I'm using IPCC 4.5 with Premium License and i'm able to write java code inside set command.

The java code must be write between { } brackets; for example

{

try{

int i=0;

int result = 0;

for(i=0;i<4;i++){

result = result+i;

}

return result;

}catch(Exception ex){

return -1;

}

}

Hope this help.

Stefano

stephan.steiner Tue, 03/06/2007 - 08:00

I mean step.. like you have General steps, Contact steps, etc. E.g. Goto would be a step.

I figured out your example though.. you'd use a set step, and assing the code snippet to an integer variable. I don't see a way to do some code work without assigning a variable though.

Stefano Amici Tue, 03/06/2007 - 08:24

i'm sorry; you're right.

I write java code inside SET step and assign the return value to a script variable.

I can't find other way to execute java code.

Could i ask you a question about RisPort interface and SelectCmDevice on CCM 5.1?

stephan.steiner Tue, 03/06/2007 - 09:04

I've seen your post in the other forum but I'm afraid I've never used RisPort. I wouldn't wait too long and just open a case with Cisco.. the API situation on 5.x is somewhat shaky imho - the documentation is inconclusive at best and at times mentions contradictory things (ports being one of them.. you already had to find that out the hard way, too).

edimarzio Wed, 03/07/2007 - 01:48

Hi. Only a question: how about reading and writing global script variable from inside the javsa code? I mean inside the brackets?

I can't do this, because it seems to be an isolated name space. Have you ever tried to do this?

Thanks!

Emanuele

stephan.steiner Wed, 03/07/2007 - 06:24

What do you mean by global? Some of the enterprise variables or script variables? I can access variables defined in my script.. I figure enterprise variables would need a custom step.

edimarzio Wed, 03/07/2007 - 06:49

I mean this: I create a new variable using the CRA script editor. Assume to create the variable named "StringTest" with String type.

And then you want to use (read / write) the value of this variable inside the java code, in a way like the following:

{

try{

int i=TestString.lastIndexOf();

int result = 0;

for(i=0;i<4;i++){

result = result+i;

}

return result;

}catch(Exception ex){

return -1;

}

}

The same thing happens when you try to write the variable inside the java code.

This method doens't work because it seems that there is no visibility of the CRA Editor variables inside the brackets. Like inside the brackets another name space exists.

Have you ever investigated this issue?

Tks!

Stefano Amici Wed, 03/07/2007 - 23:45

The method lastIndexOf needs a string as argument:

TestString = "hello";

int i = TestString.lastIndexOf("h");

Maybe this is the error.

edimarzio Thu, 03/08/2007 - 01:33

Sorry, it was only an example that I wrote quickly to explain you what is the problem.

The problem is not the method, but the variable "TestString", or whatever other variable, defined in CRA Editor that cannot be accessed inside the brackets.

If you try, you will get an error, because the variable is not recognized inside tha code.

In you answer you wrote:

TestString = "hello";

int i = TestString.lastIndexOf("h");

Is exactly this the issue: you defined

TestString inside your code, but if you try to define TestString as CRA Variable, you can't reference it, inside the java code.

A more practical example should be: try to use the CED inside the java code...

Stefano Amici Thu, 03/08/2007 - 01:46

I'm using IPCC 4.5 with Premium License and this code works:

{

try{

if(test.lastIndexOf("h")==-1){

pippo = "hello";

}else{

pippo = "ciao";

}

return 1;

}catch(Exception ex){

return -1;

}

where test is a script variable.

stephan.steiner Thu, 03/08/2007 - 01:57

I can confirm Stefano's assertion.. I'm using CRS 4.05 premium and I'm also able to use script variables within code brackets.

Actions

This Discussion