I would like to call a unix system command in java code, I write code like this:
String cmd = "split -a -d -b 10M test.txt";
Process execProc = Runtime.getRuntime().exec(cmd);
I hope when the external command is finished, I could continue to execute the following program. How to implement it?
Try
execProc.waitFor();
This should block until the command is finished. Also maybe you should consider using ProcessBuilder, it offers much more convenient API for handling processes.
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
process.waitFor();
InputStream stream = process.getInputStream();
Related
I'm trying to run a mongod process on a Linux host using Java ProcessBuilder API in a Spring Boot Application running on the same host.
I tried the following ways.
Method 1 : Directly running mongod in bash
String confpath;
String []commands = {"./mongod","-f",confPath,"&"};
ProcessBuilder processBuilder = new ProcessBuilder(commands)
Process process = processBuilder.start();
Running a process with the above array makes ProcessBuilder take & as a mongod parameter and gives an error that & is not an option.
Method 2 : Using bash -c "command"
String exec = String.format("./mongod -f %s",confPath)
String []commands = {"/bin/bash","-c",String.format("\"%s\"",exec),"&"}
ProcessBuilder processBuilder = new ProcessBuilder(commands)
Process process = processBuilder.start();
This method exits with code 127 which means that this command wasn't found.
I tried using absolute paths as well, Any insights would be helpful.
I'm running a JAR file if that helps.
I need to run a batch file from Java code, in which a python script shall be executed and it's not working.
Run the batch file by doubleclick, the python script executes correctly. Run the batch file with JAVA ProcessBuilder doesn't work completely. textoutput from batch file(-->echo) is printed, but python isn't called. How can I fix that? I've even tried to run the Python Script directly with ProcessBuilder, but no output is generated either.
run Python directly in JAVA
String pyPath = "D:/<...>/my_py_file.py";
ProcessBuilder pb = new ProcessBuilder("cmd","/c","D:/Python27/ArcGISx6410.5/python",pyPath);
Process proc = pb.start();
This isn't working as well as:
run Batch file in JAVA, calling my_py_file.py
ProcessBuilder pb = new ProcessBuilder("cmd","/c","D:/<...>/my_bat_file.bat");
Process proc = pb.start();
This is my bat-file:
#echo execute script
#echo off
cd\
d:
cd Python27/ArcGISx6410.5
#echo Python Version
python -V
#echo start script
python D:/<..>/my_py_file.py
exit
OUTPUT by doubleclick:
execute script
Python Version
Python 2.7.13
start script
**pythonOutput**
OUTPUT by using ProcessBuilder:
execute script
Python Version
start script
*nothing*
I think what's missing in your "run Python directly in JAVA" implementation is that you don't process the output after Process proc = pb.start();
For example, you can take redirect the output-stream to a file like this:
pb.redirectOutput(Redirect.appendTo(FILENAME));
(The file referenced should already be created before redirecting there)
Or you can channel it into an InputStream that you feed into an InputStreamReader for further processing, like this:
BufferedReader br = new BufferedReader(new InputStreamReader(pb.getInputStream()));
So either way, add it after Process proc = pb.start();
I want to run jar file from my java program.
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "parallel/Parallel.jar", "aug/*.xml");
Process p = pb.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = "";
while((s = in.readLine()) != null){
System.out.println(s);
}
int status = p.waitFor();
System.out.println("Exited with status: " + status);
Here is the error log I get:
[33mProblem found trying to create the log file[0m
[31mCannot locate configuration source aug/*.xml[0m
[31mNo files to work with[0m
Exited with status: 1
Problem 1: I tried using a whole file name instead of the * and it works. But, I want to run the jar on ALL files under the directory.
Problem 2: When run, the jar file will ask for some input "continue" or "cancel". But the jar program just exits in the Eclipse console without giving me chance to input anything. So, I am wondering if there is a way to launch the jar file inside a terminal?
For 1.
aug/*.xml does not work that way because the unfolding of the wildcard is done by cmd/bash. If you want to use it that way your jar would need to be able to unfold that string itself or you would have to call it using cmd/bash.
It would be preferable letting your java program Parallel unfold this so that you do not loose portability.
For 2.
To run it from a terminal you could use ProcessBuilder or Runtime.exec()
Something like that on windows: (windows cmd needs the path or it will not resolve your wildcard)
Process process = Runtime.getRuntime().exec("cmd /k start java -jar <path>/Parallel.jar <path>/aug/*.xml");
"/k" keeps the terminal active. "start" opens the terminal instead of just executing the command.
With ProcessBuilder it should be something like:
ProcessBuilder pb = new ProcessBuilder("cmd", "/k", "start", "java", "-jar", "<path>/Parallel.jar", "<path>/aug/*.xml");
For bash it would be along that lines:
ProcessBuilder pb = new ProcessBuilder("sh", "-c", "java", "-jar", "<path>/Parallel.jar", "<path>/aug/*.xml");
For further reading about Processbuilder/Runtime.exec() I would suggest: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
I'm using the process builder to launch new main classes. I do builder.inheritIO(); and it works in Eclipse (stdout and stderr redirect to the single console). However, when I export a jar, the the output doesn't redirect (only original process output showing). I'm on Java 7. Any ideas where I should look at?
Some code:
ProcessBuilder builder = new ProcessBuilder(arr);
//builder.redirectOutput();
//builder.redirectError();
//builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
// those don't work either
builder.inheritIO();
Process p = builder.start();
It seems to be a bug in java under windows. Will be fixed in java8.
https://bugs.openjdk.java.net/browse/JDK-8023130 .
You can use the old way and redirect the streams manual.
ProcessBuilder builder = new ProcessBuilder("...");
Process p = builder.start();
p.getOutputStream();
p.getInputStream();
p.getInputStream();
I'm trying to, through a java program, write commands in the gnome terminal. I tried this code:
String cmd = "ls";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
I'm trying to write "ls" to the terminal but nothing happens, but if I use
String cmd = "gnome-terminal";
I can open a new terminal window.
What I really want to do is run a C program from the terminal, calling it with java.
Thanks in advance.
gnome-terminal takes the -e argument, which allows you to tell it to execute a program.
gnome-terminal -e /path/executable
Just put them in a String[] and call the same method.
executing an external program works for me with the following commands:
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ls -l");
InputStream in = proc.getInputStream();
OutputStream out = proc.getOutputStream();
InputStream err = proc.getErrorStream();
proc.destroy() ;
}
Or, something similar is solved here: Executing in java code an external program that takes arguments as well.
If you're starting your java program from within a terminal then, after you've called exec() on the runtime, and get a Process you need to call the getInputStream() to read the output of the command, then you can print it out to System.out.
How I can read in this forum:
http://www.linuxquestions.org/questions/linux-general-1/run-command-in-new-gnome-terminal-185216/
you can use
gnome-terminal -x sh -c "ls"
to open a terminal and execute "ls" (if i remember, the "-c" option can execute a program in a new terminal.)
In this moment I'm at work and I haven't linux X system to try here. Sorry :)
I hope that this can help you!