Your approaches are equivalent to calling ls with the specified arguments. In Bash notation, what you're running is:
ls ';' pwd
ls '&&' pwd
If you want ls and pwd to be run as separate commands, you can use Bash (or another shell language) to wrap them into a single command:
bash -c 'ls ; pwd'
which you can call this way:
ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "ls ; pwd");
Answer from ruakh on Stack OverflowYour approaches are equivalent to calling ls with the specified arguments. In Bash notation, what you're running is:
ls ';' pwd
ls '&&' pwd
If you want ls and pwd to be run as separate commands, you can use Bash (or another shell language) to wrap them into a single command:
bash -c 'ls ; pwd'
which you can call this way:
ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "ls ; pwd");
I'm using ProcessBuilder to compile java program like this and it works for me:
ProcessBuilder b = new ProcessBuilder("cmd.exe","/c","cd " + dir,
" & javac " + mapClassName + ".java -cp " + pathToProjectClasses);
- cmd.exe : it's start the command prompt.
- \c : not sure what its doing but its important, you can see this link for more information (\? cmd commands)
- cd + dir : is the first command and its change the directory to a certain path which is dir.
- & : its mean start the second command after you finish the first one
- javac : this word and the rest of the string is the second command
- -cp : path to external class used by the class you want to compile.
So I have 2 commands, first one is cd command and second one is javac command and i execute them sequentially using &.
Sorry for my bad writing skills, if I haven't explained my code well please ask me about anything you want to know.
This is normal: two commands mean two processes. You need two ProcessBuilders, and check the return value of the first process before executing the second one.
This syntax:
new ProcessBuilder("javac","Mocha.java","&&","java","Mocha");
does not work. && is a logical shell operator, the javac command does not understand it. Do your processing logic in Java directly instead:
if (p1.waitFor() == 0) // compile succeeded
// initiate second process
The syntax mentioned works with shell, not with java ProcessBuilder.
Option one is to launch shell and execute shell command. Another is to invoke to ProcessBuilder two times. One for javac another for java
You don't need to include spaces. The ProcessBuilder will deal with that for you. Just pass in your arguments one by one, without space:
ProcessBuilder pb = new ProcessBuilder(
dir + "library/crc",
"-s",
fileName,
addressRanges);
We need spaces between arguments in commandline because the commandline need to know which is the first argument, which is the second and so on. However when we use ProcessBuilder, we can pass an array to it, so we do not need to add those spaces to differentiate the arguments. The ProcessBuilder will directly pass the command array to the exec after some checking. For example,
private static final String JAVA_CMD = "java";
private static final String CP = "-cp";
private static final String CLASS_PATH = "../bin";
private static final String PROG = "yr12.m07.b.Test";
private static final String[] CMD_ARRAY = { JAVA_CMD, CP, CLASS_PATH, PROG };
ProcessBuilder processBuilder = new ProcessBuilder(CMD_ARRAY);
The above code will work perfectly.
Moreover, you can use
Runtime.getRuntime().exec("java -cp C:/testt Test");
But it is more convenient to use ProcessBuilder, one reason is that if our argument contains space we need to pass quote in Runtime.getRuntime().exec() like java -cp C:/testt \"argument with space\", but with ProcessBuilder we can get rid of it.
ProcessBuilder processBuilder = new ProcessBuilder("command", "The first argument", "TheSecondWithoutSpace");