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
Related
I'm using a java process to open cmd to run a command, then save the output to a text file.
ArrayList<String> commands = new ArrayList<>();
commands.add("cmd.exe");
commands.add("/c");
commands.add("cd "+System.getenv("LOGSTASH_PATH")+" && start /B cmd.exe /c \"logstash --config.test_and_exit -f "+configFileName+"\""+" > testing.txt");
ProcessBuilder builder = new ProcessBuilder(commands);
Process subProcess = builder.start();
Thread.sleep(50000);
subProcess.destroy();
This piece of code works when i try this with eclipse,But when i generate a war out of this and deploy in tomcat, it doesn't work. What could be the problem?
How to solve this?
I suspect the problem may be with the CD instruction:
Check that the LOGSTASH_PATH environment variable is available in the Tomcat process.
In case the current Tomcat directory and the LOGSTASH_PATH belong to different drives, add a /d qualifier:
"cd /d "+System.getenv("LOGSTASH_PATH")+ ...
(Tough is much better to replace the cd call by a Java call to directory(File) instead, as #nitind pointed).
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 invoke this exe command in prompt line, it works fine, just output the txt file to the path I told it.
But I cannot find this file when I invoke this exe from java
the function I use is
Process pro = Runtime.getRuntime().exec(cmds);
any clue will be appreciate!
You need to set its working directory(where the directory which contains the file you want to execute).
Do do this:
Process p;
ProcessBuilder pb = new ProcessBuilder("your command", "arg", "arg", "etc");
pb.directory("/thepath/to/thefile");
p = pb.start();
I try to run an exe file from cmd that I open by Java, but nothing happens.
The cmd that oppened seems like:
C:\>file.exe
C:\>
When I open manually cmd the exe file runs. the cmd seems the same in both cases! (manually and via Java).
My code is:
File projDir = new File("C:/");
String command = "cmd /c start file.exe";
Process p = Runtime.getRuntime().exec(command, null, projDir);
Do you have an idea?
You dont need run an exe thru cmd. This should be enough:
Runtime.getRuntime().exec("file.exe", null, projDir);
and thru cmd with:
Runtime.getRuntime().exec(new String[]{"cmd","/c","start file.exe"}, null, projDir);
Thank you all for your help, I found a parallel way to run the exe file, and this way works:
List<String> args = new ArrayList<String>();
args.add("path\\of\\exe\\file");
ProcessBuilder pb = new ProcessBuilder(args);
pb.start();
anyway - thanks for trying to help me!
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"