I am trying to use this code in my Java application to run another Jar in Mac OS X :
Runtime runtime = Runtime.getRuntime();
String cmd = "java -Xmx1024m -jar \"/Volumes/NANO PRO/My Program/Main.jar\"";
Process process = runtime.exec( cmd );
If Main.jar exists in a path with no spaces it will work fine, but since it exists in a path with spaces, it causes the error :
Is there a way to make this run in a path that has spaces ?
Use an array to build different parts of your command and leave it to Java runtime to convert them appropriately based on the underlying environment.
String [] cmd = {"java",
"-Xmx1024m",
"-jar",
"/Volumes/NANO PRO/My Program/Main.jar"};
Also you can take a look at ProcessBuilder:
Related
I'm trying to open a PDF file in Linux with the xdg-open command in java.
String[] command = {"xdg-open","\""+path+"\""}
Process p = Runtime.getRuntime().exec(command,null);
p.waitFor();
When I run the code in terminal nothing happens even tho if I type it in terminal:
xdg-open path
it opens the PDF.
Any ideas whats wrong?
You should not escape the path: if the program was called, it was with an invalid path ("path" and not path).
String[] command = {"xdg-open", path}
The Runtime.getRuntime().exec(command,null); will use ProcessBuilder internally which, in the case of Linux, should invoke the system command execve.
I am trying to execute the following
on the command line via the java runtime environment.
Runtime rt = Runtime.getRuntime();
String runtime = "cmd /c start cmd.exe /k \"cd /d C:\\Users\\User\\Documents\\ & python book.py \" "
rt.exec(runtime);
When running the command prompt directly, i.e. python book.py (assuming that I have already changed the location to the correct directory), python runs fine without any issues.
However, when done through java, the command prompt window looks different,
with C:\WINDOWS\system32\cmd.exe instead of displaying Command Prompt.
The above java runtime also gives me 'python' is not recognized as an internal or external command, operable program or batch file. (Whereas the normal command prompt the runs python perfectly fine)
How would I go about including my path and environment variables such that python, or any other path/environment variable, is recognized when I run the command prompt from java?
This may sound naiive but apparently, the solution was to restart the computer. I guess the PATH variables in the command prompt that was run JAVA weren't synchronized like in the other instances of running the command prompt directly.
For a project to automate some mutation adequacy testing, I'm trying to make GoLang from source from inside a Java program. I have been able to make it from source in the Terminal, and have tried using that command in Java's Runtime.getRuntime().exec() command:
String[] envp = new String[3];
envp[0] = "CC=/usr/bin/clang";
envp[1] = "GOROOT_BOOTSTRAP=/usr/local/go";
envp[2] = "CGO_ENABLED=0";
Runtime.getRuntime().exec("./all.bash", envp, "$HOME/Desktop/go/src");
The equivalent command to this works fine in the Terminal. Running this code in java (And printing the output) gets the following:
./all.bash
##### Building Go bootstrap tool.
cmd/dist
go tool dist: FAILED: uname -r: exec: "uname": executable file not found in $PATH
So that's weird that it can't find uname. Again, if I enter 'uname' on the Terminal, it works fine. So I found the directory of uname ('which uname' gives '/usr/bin/uname') and set $PATH to that for this command:
String[] envp = new String[4];
envp[0] = "CC=/usr/bin/clang";
envp[1] = "GOROOT_BOOTSTRAP=/usr/local/go";
envp[2] = "CGO_ENABLED=0";
envp[3] = "PATH=/usr/bin";
Runtime.getRuntime().exec("./all.bash", envp, "$HOME/Desktop/go/src");
And that instead gets the output:
./all.bash
env: bash: No such file or directory
So when I set the path, it can't find the program in the directory. This suggests to me that when Runtime.getRuntime().exec() is called, it overwrites $PATH to be the directory I passed it, then overwrites the environment variables I gave it. But in order for ./all.bash to work, I need both paths to be in the $PATH variable. How can I do this?
On Mac OS X 10.11.6.
Runtime.exec was replaced by ProcessBuilder twelve years ago, as part of Java 1.5.
Among its many superior features is the ability to add to the existing environment:
ProcessBuilder builder = new ProcessBuilder("./all.bash");
builder.inheritIO();
builder.directory(
new File(System.getProperty("user.home") + "/Desktop/go/src"));
builder.environment().put("CC", "/usr/bin/clang");
builder.environment().put("GOROOT_BOOTSTRAP", "/usr/local/go");
builder.environment().put("CGO_ENABLED", "0");
builder.start();
I am trying to execute the below code
String command="cmd /c ls";
String location="C:\\project";
final Process process = Runtime.getRuntime().exec(
dosCommand + " " + location);
I am getting the files, but when I run the cmd /c java I didn't get the output.
my Java home is added to environment variables.
When I give Java in command prompt I am getting the Java related files. I am not getting through my program.
I assume you want to run a Java program throught command line in your Java program? Try this:
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java ...");
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
Assuming you use Java 1.5 or higher, I'd recommend using ProcessBuilder if you want to execute from a different path. It allows you to easily set the working directory for the process.
final Process pr = new ProcessBuilder(
"java",
"-Xms512M",
"-Xmx1024M",
"-jar",
"your_jar_to_use.jar",
"your Java class")
.directory(new File("<your directory>")) //Set the working directory to <your directory>
.start();
Or better yet, use the Java Compiler API, as demonstrated in this article and this one.
Using process builder to launch other Java applications in their own OS process. The implementation works on Windows 7, but not on Linux. Both machines are using Java 1.7. Here is some example code:
//Windows OK, but Linux Could not find or load main class
//weka.subspaceClusterer.MySubspaceClusterEvaluation
ArrayList<String> commands = new ArrayList<String>();
commands.add("java");
commands.add("-cp");
commands.add("\".:lib/*\"");
commands.add("weka.subspaceClusterer.MySubspaceClusterEvaluation");
procBuilder = new ProcessBuilder();
procBuilder.inheritIO();
procBuilder.command(commands);
Process proc = procBuilder.start();
I encountered a similar issue on Mac OS X. It worked in Terminal but not in Eclipse. It worked for me if I removed quotes around the class path string. I guess Eclipse JVM does not like it when there are quotes around any of the arguments passed to ProcessBuilder.
The classpath separator is ; under Windows, but : under Unix.
Consider creating a runnable jar, where your classpath is stored in the MANIFEST.MF entry, so you can just execute java -jar whatever.jar.
Your code looks correct. It just can't find your class file. Try setting the working directory of your process:
procBuilder.directory(new File("package/structure/starts/here"));