Call Java from PHP with JSON arguments -


i need execute java code php , pass json string argument.

php part:

$json_arr["regrtype"] = 3; $json_arr["fund_id"] = 25106478; $json_arr["factors"] = array(2,7,18,19); $input = json_encode($json_arr); exec("java stepregtest.main " . $input . " 2>&1", $output); 

java entry:

public class main {       public static void main(string[] args) throws exception { 

here problem: because "main" constructor can take string[] array , not simple string, java script breaks json string array - like:

args[0] = "regrtype:3" args[1] = "fund_id:25106478" args[2] =  "factors:[2" 

how can force take whole json input string? if cannot find way of getting intact json string in java, can arrays json elements - see args[2]?

i split array part , use different separator, perhaps there more elegant way of doing this.

thanks

to pass single string, put quotes around it.

so should work if use

exec("java stepregtest.main \"" . $input . "\" 2>&1", $output); //note added quotes 

the issue has nothing string vs. string[], it's issue that, if don't put them in quotes function arguments delimited whitespace. compare:

java someclass these 4 arguments

java someclass "this 1 argument"

edit: mentioned wrote had escape quotations within string; tested this, it's not case. example:

java someclass "this "still" 1 argument"


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -