0

I need to execute a Java code from php and pass a JSON string as an 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 is the problem: because the "main" constructor can take only String[] array and not a simple string, the Java script breaks the JSON string into array - like:

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

How can I force it to take the whole JSON input string? If I cannot find a way of getting the intact JSON string in Java, what can I do with arrays as JSON elements - see args[2]?

I could split the array part and use a different separator, but perhaps there is a more elegant way of doing this.

Thanks

4
  • You need to quote and escape the string you are passing into the command line. But I'm hard pressed to see a good reason to do this. Commented Sep 4, 2013 at 4:15
  • If you need to get the value from JAVA to PHP, you can use CURL in PHP to post and get the values from JAVA application as JSON Commented Sep 4, 2013 at 4:21
  • it is Java - not JavaScript. The problem is that the main Java constructor can take only String[] array, but the PHP exec passes a string. Also I dont't have any problems of getting values from java to PHP. Commented Sep 4, 2013 at 4:27
  • if u have no choice but to go with this route, i suggest u break your json into individual strings into the argument or delimited string whichever. Commented Sep 4, 2013 at 4:29

2 Answers 2

2

To pass it all as a single string, put quotes around it.

So it should work if you use

exec("java StepRegTest.Main \"" . $input . "\" 2>&1", $output); //Note the added quotes

The issue has nothing to do with String vs. String[], it's just an issue that, if you don't put them in quotes function arguments are delimited by whitespace. Compare:

java SomeClass these are four arguments

java SomeClass "this is one argument"

EDIT: Originally I mentioned wrote that you had to escape the quotations within the string; just tested this, it's not the case. Example:

java SomeClass "this is "still" one argument"

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

5 Comments

Thanks - it did the trick! Actually, I have tried escaping the JSON string, but didn't add the double quotes in the exec string. Also, it doesn't work unless you escape the JSON string - double quotes within the exec command are not enough.
+1 for giving a straight answer to the question instead of asking back why the OP wouldn't use 1 language or use a completely different method.
Re: String vs. String[]. What is happening when we pass a string via PHP exe to a Java sub with array String[] as an argument? If we pass the same from Java to Java, it will throw an exception, But in our case Java takes the input string as args[0]. Why?
I'm not sure what you mean by "passing from Java to Java will throw an exception"; you don't normally have a Java program call other Java programs from the command line, do you?
But this works because putting quotes around the entire string causes it to be treated as a single string. The args array is just a list of command line arguments, so the entire string gets put as the first argument. If you don't, command line arguments are separated by whitespace, so it treats it as several arguments.
0

Expose an interface in your Java code. Call it from PHP using PHP/Java Bridge

Comments

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.