0

I have a php file which runs a '.jar' file. Code is given bellow

exec("java -Xmx1g -jar \"C:\Users\Roxy\Documents\NetBeansProjects\Entity Extraction\dist\Entity Extraction.jar",$output);  

This works fine without any error
But now I need to pass an array (lets say $testArray) with this command to 'Entity Extraction.jar' file and access it in my main method in java code.

Can anyone please tell me how to do this?

Update:

In my php file, I have

$testArr[0] = 'test1';
$testArr[1] = 'test2'; 

$str = "java -Xmx1g -jar \"C:\Users\Roxy\Documents\NetBeansProjects\Entity Extraction\dist\Entity Extraction.jar ";

foreach($testArr as $param){
 $str.=$param.' ';
}

exec($str,$output);
print_r($output);

In my java Main Class, I have

public static void main(String[] args) {
     int length = args.length;
     for (int i = 0; i < length; i++) {
          System.out.println(args[i]);
     }
}
2
  • You should be able to just add the array to the exec command, provided it is composed of strings, and can then access it in your java main via the args parameter (it will show up as a single String which you'll have to split). However, could you elaborate where you're going with this? This kinda sounds like a bad thing to do in the first place... Commented Aug 12, 2012 at 12:40
  • can you please tell me how to use it in 'exec' code and use it in main method(if you can with an example). Commented Aug 12, 2012 at 12:43

2 Answers 2

1

When running a jar in the command line, every parameter you put after Extraction.jar separated by a space will automatically go into the args array.

So you need to run on the array and build the string you want to pass:

 $str = "java -Xmx1g -jar \"C:\Users\Roxy\Documents\NetBeansProjects\Entity Extraction\dist\Entity Extraction.jar ";

foreach($testArr as $param){
 $str.=$param.' ';
}

exec($str,$output);

This is assuming the array only contains strings or numbers. if it contains objects or other arrays, you need to manipulate this code a bit, but you get the idea.

Sign up to request clarification or add additional context in comments.

14 Comments

This doesn't work. I add $testArr[0] = 'test1';$testArr[1] = 'test2'; before '$str=....' argument. Add bellow command in my main method. int length = args.length; for (int i = 0; i < length; i++) { System.out.println(args[i]); }
Please update the question with the code you tried, its hard to see it in the comments.
I tried this with the command prompt also just by giving single string after the 'Extraction.jar '. But it says 'unable to access jarfile ...'
It works in the command prompt when I went into the 'dist' folder using cd command and then run the .jar file with a input parameter?
is your jar name "entity extraction.jar" with a space? if so that is a big no-no, change it to entity_extraction.jar and try again
|
0

you could pass print_r($testArray,true); or you could write a function that cycles through the array and creates a list.

2 Comments

how do I pass 'print_r($testArray,true);' with 'exec' ?
you can either do exec("stuff here ".print_r($testArray,true)." other stuff here"); or pass it via a variable using $params = print_r($testArray,true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.