Was reading this question: Execute .jar file from a Java program and the answer tells you to use a ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar");
How do I find out the /path/to/java using code? Of course I could look up java in my machine, but an end-user may not necessarily have Java installed in the same place as I do...
Use System.getProperty("java.home").
Related
ProcessBuilder pb = new ProcessBuilder("C:\\xxxxxxx\\python.exe", "C:\\xxxxxxxxxx\\1.py");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
I am trying to run Python script from my Java program. The problem is, java run doesn't give me any result. In Python script, there is an OCR operation and I am writing a txt file.
CMD - Manually - It runs OK
Python IDLE - Manually - It runs OK
.bat doesn't work. with administrative rights doesn't work.
Java run doesn't work.
I need help, I need to run the script from java program.
I solved the problem.
The problem is;
ProcessBuilder pb = new ProcessBuilder("C:\\xxxxxxx\\python.exe", "C:\\xxxxxxxxxx\\1.py");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
When you run this code, python script will process operations in that directory, I mean in your Java class' directory. No matter where py file is.
In my case, there were 'image.save("x.png")' lines in the python script. I hoped that images will be saved in the directory where the py file is. But it is not like that.
I want to create a java program which will compile and execute program written in c++. Ijust wanted to know the links or get any idea how will I do this, I want to learn by-self, but not sure from where i should start. I got a link for executing a ".exe" file, which is the part of my program, but how can I compile a C++ program through Java.
I tried to search the related stuff, but was unable to find...suggestions are appreciated...
I think you want to run a .exe file through Java.
You should try Runtime.getRuntime().exec(String command, String[] envparam, File dir) with:
command is the location of the .exe
envparam can be null
dir is the directory of your .exe
Example:
Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
Here's how you should use Runtime.exec() to execute a C++ compiler. You might also try its more modern cousin, ProcessBuilder:
Java Runtime.getRuntime().exec() alternatives
I have a simple java file in linux. I am executing another java program HelloWorld from my Parent Java class.
If I keep the class file in current directory it workds but if i keep in another directory it wont work. could you tell me what mistake I am doing here?
Working:
theProcess = Runtime.getRuntime().exec("java HelloWorld");
Notworking Below:
theProcess = Runtime.getRuntime().exec("java -cp \"/home/sss/public_html/Project/WekaMLWorkbench/src/m85/\" HelloWorld");
Many Thanks.
I understood your problem. It happens because of the regnoition problem in linux and java based environments.
Use this method in Runtime.
exec(String[] cmdArray)
Substituting the values.
theProcess = Runtime.getRuntime().exec(new String[]{"java", "-cp" ,"/home/sss/public_html/Project/WekaMLWorkbench/src/m85/HelloWorld");
I want to run the following command from a java file command : "java hello < C:\iptest\input.txt > C:\outtest\name.txt "hello" will take input from "C:\iptest\input.txt" and will produce an output file at "C:\outtest\name.txt".
codes i have done
String command[]={"java","hello","< C:\\iptest\\input.txt >","C:\outtest\name.txt"};
ProcessBuilder pb=new ProcessBuilder(command);
pb.directory(new File("E:\"));
and now how to go forward i have no idea .Please Help!!
Here is a link to the ProcessBuilder definition on Oracle, which explains how to use this class. Typically, this is used for executing non-java processes, e.g. .BAT or .EXE processes, but I suppose you could use it to execute any process you wish.
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
I am trying to compile a c program from a java program on Linux platform. My snippet is.
ProcessBuilder processBuilder = new ProcessBuilder("/usr/bin/gcc",
"-c","/hipad/UserProject/example.c");
Process proc = processBuilder.start();
There is no error during compilation of java program but I am not able to get .o file. I tried to find out solutions but no one is working.
Any suggestion.....
The default working directory of a child process is what ever directory the Java process has as a working directory, which usually is where it was launched from. And by default gcc writes output files to current working directory. That's where you should find example.o.
There are two simple ways to solve this. You can give gcc -o option and full path and name of desired output file, or you can set working directory of child process, like this:
ProcessBuilder processBuilder =
new ProcessBuilder("/usr/bin/gcc", "-c","example.c"); // source in working dir
processBuilder.directory(new File ("/hipad/UserProject")); // or whatever
Process proc = processBuilder.start();
See ProcessBuilder javadoc for more info.