I would like to execute 2 or more commands sequentially through my Java Application using ProcessBuilder class. I Have tried multiple options as suggested in other responses/forums but no luck.
Here are the things I have tried:
ProcessBuilder processBuilder = new ProcessBuilder("ls", ";", "pwd");
Gives me following error :
Errors : ls: ;: No such file or directory
Errors : ls: pwd: No such file or directory
ProcessBuilder processBuilder = new ProcessBuilder("ls", "&&", "pwd");
Gives me similar error:
Errors : ls: &&: No such file or directory
Errors : ls: pwd: No such file or directory
List<String> command = new ArrayList<String>();
command.add("ls");
command.add(";");
command.add("pwd");
ProcessBuilder processBuilder = new ProcessBuilder(command);
Gives me following error:
Errors : ls: ;: No such file or directory
Errors : ls: pwd: No such file or directory
My OS is Linux/Mac-OSX.
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");
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.
You could get the Process from ProcessBuilder.start() from the first command, wait using waitFor() and then launch the second one.
Related
I'm trying to start a java GUI program in windows via java code as below :
ProcessBuilder builder = new ProcessBuilder(Arrays.asList(new String[] {"cmd.exe", "/C",
"C:\\path\\to\\program\\program.cmd"}));
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
The cmd file starts the program with a "start javaw .." command with ... -cp program.jar -jar program.jar . When using the java code above, it throws an error that the program.jar is not found :
I also tried with the below, using cd first :
{"cmd.exe", "/C", "cd C:\\path\\to\\program\\ && program.cmd"}));
But the above does nothing at all.
Content of .cmd :
setlocal
SET JAVAHOME=..\java
SET PATH=%JAVAHOME%\jre\bin;%JAVAHOME%\jre\bin\client;%JAVAHOME%\bin;%PATH%
SET PATH=%PATH%;bin\
SET POLICY=java.policy
SET JAR_BOOT=program.jar
SET CONFIG_FILE=program.xml
IF EXIST jar\%JAR_BOOT% copy jar\%JAR_BOOT% . >NUL
start javaw -Xbootclasspath/p:jar/xercesImpl-2.9.1.jar;jar/xml-apis-1.3.04.jar;jar/xalan-2.7.1m1.jar;jar/serializer-2.7.1m.jar -Xmx1024M -XX:MaxPermSize=200M -cp %JAR_BOOT% -Dsun.java2d.noddraw=true -DJINTEGRA_NATIVE_MODE -Djava.security.policy=%POLICY% -jar %JAR_BOOT%
title Command Prompt
endlocal
So what's the proper way to do this ?
Using the suggestion from #JMax to use the directory() method of the ProcessBuilder to set the working directory has fixed the issue
Problem Statement: I just want to start the HUB and Node to perform some tests using Selenium Grid.
I have two Batch files START HUB.bat and START NODE.bat which run perfectly when i manually run them.
But i want them to run using a Java Program #BeforeMethod.
I looked for answers
Runtime.getRuntime().exec("cmd /C start \"./BatchFiles/START HUB.bat\"");
This Opens Up the CMD but goes to the path of my .git project but doesnt run the bat file.
I tried using Process Builder but that doesn't open the cmd.
ProcessBuilder pb = new ProcessBuilder("cmd", "/C"," start", "START HUB.bat");
File dir = new File("D:\\work\\GIT REPOSITORY\\project.selenium.maven.jenkinsCI\\BatchFiles");
pb.directory(dir);
Process p = pb.start();
Can someone please help me with this issue. Below are the commands in the batch file.
D:
cd work
java -jar selenium-server-standalone-3.4.0.jar -role hub
So you want to execute the command line:
cmd /C start "./BatchFiles/START HUB.bat"
With cmd at beginning of the command line a new command process is already started with executing %SystemRoot%\System32\cmd.exe. This command process should automatically close after running the command as explicitly requested with option /C which means close as it can be read on running in a command prompt window cmd /?.
The command to execute in this command process is:
start "./BatchFiles/START HUB.bat"
The internal command start of cmd.exe is for starting an executable or script in a new process. Its help can be read on running in a command prompt window start /?.
The first double quoted string is interpreted by start as title of the new command process window opened when a batch file or a console application should be executed in a new command process.
And this is the reason why the batch file is not executed because "./BatchFiles/START HUB.bat" is interpreted as window title string.
And on Windows the directory separator is \ and not / as on Unix. / is used as begin of an option as you can see on /C. But Windows handles also file paths with / often correct because of replacing each / internally by \ in directory/file names with an absolute or relative path on accessing the directory or file.
So the solution is using either
Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" \".\\BatchFiles\\START HUB.bat\"");
or using
Runtime.getRuntime().exec("cmd.exe /C \"BatchFiles\\START HUB.bat\"");
A path starting with a directory or file name is relative to current directory of the running process on Windows like using .\ at begin of a directory or file name string.
The first code starts a command process which executes the command start which starts one more command process with title start hub executing the batch file. The first command process started with cmd.exe immediately terminates after running start while the batch file is executed in second started command process. This means your Java application continues while the batch file is executed parallel.
The second code results in executing the batch file in the single command process started with cmd.exe and halting the execution of the Java application until entire batch file execution finished.
The usage of a batch file can be removed here by using:
Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" /D D:\\work java.exe -jar selenium-server-standalone-3.4.0.jar -role hub");
With /D D:\work the working directory is defined for the command process started for executing java.exe with its parameters.
Alternatively without using start command:
Runtime.getRuntime().exec("cmd.exe /C cd /D D:\\work && java.exe -jar selenium-server-standalone-3.4.0.jar -role hub");
Run in a command prompt window cd /? for help on cd /D D:\work and see Single line with multiple commands using Windows batch file for an explanation of operator && used here to specify two commands to execute on one line whereby java.exe is executed only if cd could successfully change the working directory to D:\work.
class RunFile
{
public static void main(String[] arg){
Runtime runtime = null;
try{
runtime.getRuntime.exec("cmd /C start \"D:\\work\\GIT REPOSITORY\\project.selenium.maven.jenkinsCI\\BatchFilesmyBatchFile.bat\\START HUB.bat\"");
}
catch(RuntimeException e){
e.printStackTrace();
}
}
}
Did you try to pass the absolute path to the exec function?
As well as quote the path since you're having a whitespace between START and HUB
The explanation by #Mofi really helped here understanding as to how cmd treats each and every "/".
Runtime.getRuntime().exec("cmd.exe /C start \"start hub\" \".\\BatchFiles\\START HUB.bat\"");
Above is the solution that worked for me.
I want to use "adb logcat -d > C:\Users\lenovo 01\Documents\android\sdk\platform-tools" command line command within my java code. this works directly in command prompt but it doesn't work within java code.
for example:
pb = new ProcessBuilder("adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");
pc = pb.start();
pc.waitFor();
System.out.println("written");
when I execute this, nothing happens. It writes only "written" but the file is empty. When I run this command in command prompt, it works correctly and writes all logcat output to that file. What am I doing wrong?
Redirecting output to a file is a feature of the command interpeter; it's not something that can be performed by the process itself. Try appending cmd /c to the beginning of your command:
pb = new ProcessBuilder("cmd", "/c", "adb", "logcat", "-d", ">", "C:\\android\\cellograf.txt");
Hello and excuse me for being new to java coding, but what I am trying to do is a java program that calls an executable program with some given parameters in ubuntu. I've found the code above in another stackoverflow question:
ProcessBuilder pb = new ProcessBuilder();
pb.command("bash", "-c", "./runCalculator.sh");
Process process = pb.start();
int retValue = process.waitFor();
But how can I cd to the executable file first and then execute the program, displaying its output, through java?
Thank you.
You have 2 options:
Use absolute path
pb.command("bash", "-c", "/path/to/runCalculator.sh");
Use ProcessBuilder directory method:
pb.directory(new File("/path/to"));
You don't have to cd anywhere, just specify the absolute path.
String path = "/home/Omen/runCalculator.sh";
pb.command("bash", "-c", path);
This is the command I want to run:
su - postgres -c "pg_dump ....."
to backup the postgres database.
If I am now in linux shell, as a root, it works great.
But now, I want to run it from a java application, as:
String cmd = "su - postgres -c \"pg_dump --port 5432 .....\""
Process p = Runtime.getRuntime().exec(cmd);
// read the error stream and input stream
p.waitFor();
It throws an error:
su: unknown option "--port"
please try "su --help" to get more information
Now I change the code as:
Process p = Runtime.getRuntime().exec(new String[0]{cmd});
// read the error stream and input stream
p.waitFor();
Cannot run program "su - postgres -c \"pg_dump .....\"": java.io.IOException: error=2, no that file or directory
What should I do now?
Besides the answers given above (and it's good to understand your problem) remember that you always can make your life easier by just putting your command inside a shell script file. Eg, a dumppg.sh file which just contains two lines:
#!/bin/sh
su - postgres -c "pg_dump ....."
just make it executable, and (after testing it) call it from java.
exec(new String[]{"sh", "-c", "su - postgres ..."});