0

I have a tomcat servlet which calls a jar function with parameters. The first parameter sometimes contains space. So I tried to use a String array, but it doesn't work at all.

What am I doing wrong?

requestParm = "java -classpath c:\\j\\test.jar test.connect " + fileName + " new";
requestParmarray =new String[]{"java -classpath c:\\j\\test.jar test.connect ",fileName , " new"};
requestParmarrayNew =new String[]{"java -classpath c:\\j\\test.jar test.connect "+fileName+" new"};

// This line works.but can not handle space well
Process ls_proc = Runtime.getRuntime().exec(requestPar);

// Does not call the function at all
Process ls_proc = Runtime.getRuntime().exec(requestParmarray ); 

// Does not call the function at all
Process ls_proc = Runtime.getRuntime().exec(requestParmarrayNew ); 

// Does not call the function at all
Process ls_proc = new ProcessBuilder("java -classpath c:\\j\\test.jar test.connect ",fileName, "new" ).start();
0

2 Answers 2

5

You're creating the array incorrectly: Each individual argument must be in its own entry:

String[] requestParmArray = new String[] {
    "java",
    "-classpath",
    "c:\\j\\test.jar",
    "test.connect",
    fileName,
    "new"
};
Process ls_proc = Runtime.getRuntime().exec(requestParmArray);

Also note that I removed the space you had after test.connect; the spaces you put on the command line are just to separate arguments, but in the above, they're separated by being separate entries in the array.

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

1 Comment

This fixes my problem :). Thank you.
2

You should make the array in exec() have each parameter as a separate array entry like:

String[] requestPar = new String[]{"java", "-classpath", "c:\\j\\test.jar", "test.connect ", fileName, "new"};

And use it:

Process ls_proc = Runtime.getRuntime().exec(requestPar);

1 Comment

This fixes my problem. Thank you.

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.