Roshanck
Roshanck

Reputation: 2290

How to use php 'exec' to run .jar with input parameters

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]);
     }
}

Upvotes: 0

Views: 4816

Answers (2)

Tomer
Tomer

Reputation: 17940

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.

Upvotes: 1

JohnHoulderUK
JohnHoulderUK

Reputation: 679

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

Upvotes: 0

Related Questions