Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

How to pass arrays from a PERL code to a JAVA code


I call my java code from my perl code as seen below:

my @javaCmd = ('java', '-jar', "$mydir/myTool/Tool-1.0.jar", '$Login', '@Rec', '$ID', "$clstr', 
'$optionLog', '$optionSb', '$soapEndpointURL', '@Files', '@Com', '@CR', '$File');

Open in new window


and this is the beginning of my java code:

public static void main(java.lang.String args[])
       {   
    	   if (args.length == 11) {
	    	   java.lang.String login = args[0];
	    	   java.lang.String rec = args[1];
	    	   java.lang.String id = args[2];
	    	   //java.lang.String pathToJobFile = args[3];
	    	   java.lang.String clstr= args[3];
	    	   java.lang.Boolean optionlog = args[4].equals("1");
	    	   java.lang.Boolean optionsb = args[5].equals("1");
	    	   java.lang.String soapEndpointURL = args[6];
                   java.lang.String Files = args[7];
                   java.lang.String Com = args[8];
                   java.lang.String CR = args[9];
                   java.lang.String File = args[10];

Open in new window


Can I do it this way?

I think something is wrong in here with the arrays that I pass.

How can I do it the right way?

Note: @Rec in the function call has numbers in it.

Thanks,
0
Tolgar
Asked:
Tolgar
1 Solution
 
nemws1Database AdministratorCommented:
Hmm... You've got literal quotes when you're building the command in perl.

Change your single quotes to double-quotes - otherwise @Rec is getting passed as the literal string '@Rec' instead of the values stored in that array.
0
 
nemws1Database AdministratorCommented:
Also, you have one variable with mixed quotes:    "$clstr',


Also, once you start passing actual values, your Java code will break if @Rec has more than value, since in Java you're only looking for one value.

In the past where I've had to do something similar, I've build a much more complex argument parser, something along the lines of calling Java with arguments like:
 
java -jar $mydir/myTool/Tool-1.0.jar login:my_name rec:1 rec:4 rec:5 id:id_info .... (etc.)

Open in new window


And then using a loop on args in Java to figure out what each argument is and then assign it to the proper variable(s) in Java.
0
 
TolgarAuthor Commented:
Ok I changed the single quotes to double quotes.

But for your other command, I didn't really understand what you mean.

So, my question was how to pass an array in Perl to another array in Java.

And the length of the array can change. So I cannot hard-code it. The way I do it in Java can be changed. So I don't enforce anything there. But I just want to pass the entire array to Java at once.

Isn't it possible? If it is, how can I do it?

Thanks,

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nemws1Database AdministratorCommented:
Gotcha... I was trying to explain one way of doing that.  Your code would get a bit complex, but in perl you'd do something like this:
my @javaCmd = ('java', '-jar', "$mydir/myTool/Tool-1.0.jar",
    "login:$Login",
    "Rec:" . join(" Rec:", @Rec),
    "id:$ID",
    "clstr:$clstr', 
    ... (the rest)

Open in new window

Which will produce:
java -jar /path/to/mydir/myTool/Tool-1.0.jar login:login_value Rec:rec_value_1 Rec:rec_value_2 Rec:rec_value_3 Rec:rec_value_4 id:id_value clstr:clstr_value ... (the rest of your arguments)

Open in new window

You'll then need to loop over all your arguments in Java (as strings), grab everything up to the first colon ":" and then assign that value appropriately.  For example, for "Login", you would just need to assign everything after the "Login:" to your variable "login" in Java.  When you come across a known array, like "Rec", you would need to take each value after "Rec:" and add it onto a "rec" array (right now you have just a single string variable in Java - you'll need to change that).

There's another way to do this.  Keep your code pretty much the same, except prepend any arrays with an argument that says how many value are in the upcoming array.  For example, your perl code would look like:
 
my @javaCmd = ('java', '-jar', "$mydir/myTool/Tool-1.0.jar",
    "$Login",
    ($#Rec + 1),
    "@Rec",
    "$ID",
    "$clstr', 
    ... (the rest)

Open in new window


Then in Java, you would still loop over your arguments, but you would read in the size of "Rec" before any values in "Rec", thus, you'd know how many values of 'args' should be read into whatever array you set up in Java for 'Rec'.

Either way, you're going to have to add in more logic in your argument parsing in Java.
0
 
TolgarAuthor Commented:
Can you please send me a sample code in Java with this example (for Rec) on how to loop through the arguments?

I am kind of new to Java. So that would really help me to accelerate.

Thanks,
0
 
for_yanCommented:
If your array is strings it will be something like that:

public ststic void main(String [] args){
String login = args[0];
String numRec = args[1];

int i = -1;

try{
i = Integer.parseInt(numRec);

}catch(Exception ex){
System.out.println("second argument should be int");
System.exit(0);
}

String [] recs = new String[i];

for(int j=0; j<recs.length; j++){

recs[j] = args[2+j];
}


}

Open in new window

0
 
parparovCommented:
I think you'll have to use Inline::Java in your perl when things get that complicated:

http://search.cpan.org/dist/Inline-Java/Java.pod
0
 
TolgarAuthor Commented:
nemws1: I have a follow-up question  

ID: 27380245


Thanks,
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now