Solved

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

Posted on 2011-09-27
8
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 104
ejb example issues 3 85
mysql jsp example issue 32 92
Running JavaFX on the Raspberry Pi 27 270
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

738 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