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.
Related
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 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();
I am currently developing a Java system that communicates with node.js using socket.io. The system and the script are on the same server. How can I execute the script from my Java code and keep it alive from my app?
Note when using process builder the path to your JavaScript file is an argument and "node" is the command, so they need to be separated:
ProcessBuilder pb = new ProcessBuilder("node", "app.js");
This is also useful for inheriting its console output, starting the process and getting the reference to the process:
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
Say you have a node.js script named "mynodejs.js". You can use the following java code:
Process p = Runtime.getRuntime().exec("node mynodejs.js");
or
ProcessBuilder b = new ProcessBuilder("node mynodejs.js", "-args");
Have a look at ProcessBuilder class.
You can start any process on your machine like you would via shell, if I understand your question correctly.
I've seen many similar posts but I am still stumped. I want to run this shell script with a Java program :
C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh
Here's what the code looks like:
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh");
Process p = pb.start();
I am trying to use Process Builder but I'm not sure how to get it to work - I keep getting the error:
Cannot run program "C:\Users\pro-services\Desktop\projects\github\cygwin\TEST.sh": CreateProcess error=193, %1 is not a valid Win32 application
I know there are other fields of ProcessBuilder that I'm not using. I also know that there may be syntax issues here. I wrote my script for Cygwin. Any help please?
Instead of passing the script as an executable to ProcessBuilder, pass in the path to bash binary and pass your script as argument:
ProcessBuilder pb = new ProcessBuilder("C:\\path\\to\\bash.exe", "C:\\Users\\pro-services\\Desktop\\projects\\github\\cygwin\\TEST.sh");
Process p = pb.start();
when I try in the linux shell to run the following shell script,
cd /home/fpalma/Project/resources/yices/linux64bit/
bash
chmod a+x yices
./yices /home/fpalma/Project/out/SMT.ys > /home/fpalma/Project/out/SMT.txt
it is running and generating the text file as output of that yices executable. But when I am running the same shell scripts from Java using the code,
String command = mainGUI.PROJECT_PATH+"resources/"+"yices.sh";
process = Runtime.getRuntime().exec(command);
process.waitFor();
or
String command = mainGUI.PROJECT_PATH+"resources/"+"yices.sh";
pb = new ProcessBuilder(command);
run = Runtime.getRuntime();
Process p = pb.start();
it is also running, but not exiting, and unless I am exiting the java compiler itself, it is not generating the text output file. Any suggestions???
Goal: my goal is to run a executable from java using a input paramater file and generate a output text file afterwards using '>' operator.
your "command" variable should contain the full command, that is :
mainGUI.PROJECT_PATH+"resources/"+"yices.sh /home/fpalma/Project/out/SMT.ys > /home/fpalma/Project/out/SMT.txt"