Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

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,
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

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.
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.
Avatar of Tolgar
Tolgar

ASKER

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,

ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar

ASKER

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,
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

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
Avatar of Tolgar

ASKER

nemws1: I have a follow-up question  

ID: 27380245


Thanks,