Parameter passing in ProcessBuilder returns "Could not find or load main class"

I have the following code which builds my java command and runs process builder.

String runCommand[] = {"java", ExerciseSelected + " " + Input};
runpb   = new ProcessBuilder(runCommand);  
Process runp = runpb.start();  

Input is a string of input separated by spaces. Currently I have an input of 100 which I am passing to my java program.

When running this it returns "Could not find or load main class Exercise 100"

Now i looked at another StackOverflow article that explains how to create a java command call. The command call to processbuilder looks like this

java Exercise 100

If I go to my java application folder and run this same call to the same Exercise.class it works from the command prompt. But it won't work from ProcessBuilder.

I have tried to enclose 100 in quotes and that also did not work. Is it possible there is something I have missed in putting together this command?

Jon Skeet
people
quotationmark

You're effectively trying to run

java "Exercise 100"

You want two arguments: "Exercise" and "100", so you should have them as different elements in the array:

String[] runCommand = { "java", exerciseSelected, input };

Note that if input is actually "1 2 3" this will be equivalent to running:

java Exercise "1 2 3"

which still may not be what you want. You may want to split input by spaces first.

(I've adjusted the variable names and the location of the [] to be more idiomatic.)

people

See more on this question at Stackoverflow