Solved

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

Posted on 2011-09-27
8
301 Views
Last Modified: 2012-06-27

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
Comment
Question by:Tolgar
8 Comments
 
LVL 23

Expert Comment

by:nemws1
ID: 36710517
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
 
LVL 23

Expert Comment

by:nemws1
ID: 36710569
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
 

Author Comment

by:Tolgar
ID: 36710774
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 23

Accepted Solution

by:
nemws1 earned 500 total points
ID: 36710891
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
 

Author Comment

by:Tolgar
ID: 36711101
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36711205
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
 
LVL 9

Expert Comment

by:parparov
ID: 36712261
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
 

Author Comment

by:Tolgar
ID: 36921449
nemws1: I have a follow-up question  

ID: 27380245


Thanks,
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 213
how to install java on RHEL image on EC2 4 41
by zero exception 10 52
maven not picking latest jar instead picking old jar from .m2 12 22
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question