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');
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];
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,
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:
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.
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.)
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.
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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,
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];
}
}
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
http://search.cpan.org/dist/Inline-Java/Java.pod
ASKER
nemws1: I have a follow-up question
ID: 27380245
Thanks,
ID: 27380245
Thanks,
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.