I am trying to run a simple ImageMagick command from a Java class and as I have to run only a few commands I thought instead of using Im4Java ,I could directly use ProcessBuilder.start().
I am using the following code-
ProcessBuilder pb = new ProcessBuilder("convert","pic2.png","pic52.png");
pb.directory(new File("/user/gaurav_kl"));
pb.start();
but I am getting the error
IOException - Cannot run program "convert" (in directory "/user/gaurav_kl"): error=2, No such file or directory
What could be the reason.
when I run the same command from terminal it works fine from any Dir as IM has been added to classpath
The behavior of ProcessBuilder when searching for a command executable is system/jvm dependent. While you might expect that it uses same logic as the underlying shell (i.e. BASH), there is no guarantee of it in the documentation.
Based on your experience (and others), it is better to provide the complete command path. For example:
String IMGK_PATH="/usr/local/magick/bin";
ProcessBuilder pb = new ProcessBuilder(IMGK_PATH + "/convert","pic2.png","pic52.png");
pb.directory(new File("/user/gaurav_kl"));
pb.start();
Related
We have a Java server-client application that allows us to run processes on different boxes (i.e. the clients), which are started by the Java ProcessBuilder. I want to run a process that will be copied/synced to the user's home directory (i.e. the user who started the client).
How do I reference the unix home directory in the String that is passed to the ProcessBuilder? (Due to the design of the server-client application, only a String of the process, args etc. is passed to the ProcessBuilder.)
It works if I state the home directory explicitly:
/home/user/processes/process.sh
However, that assumes that I know which user is running the client. (Part of the design is that we can switch/substitute boxes/clients to run jobs, without necessarily knowing who started the client on a given box.)
I've also tried, but to no avail:
$HOME/processes/process.sh
~/processes/process.sh
The issue is that both ~ and $HOME are only understood by your shell, probably BASH, not by ProcessBuilder or Java.
$HOME should be available via the property user.home. See System Properties documentation
String home = System.getProperty("user.home");
i.e.
File fullpath = new File(System.getProperty("user.home"), "processes/process.sh");
ProcessBuilder processBuilder = new ProcessBuilder(fullpath.getAbsolutePath());
or could call it relative to current directory
ProcessBuilder processBuilder = new ProcessBuilder("processes/process.sh");
processBuilder.directory(new File(System.getProperty("user.home")));
In the end, the approach I took was to pass the script and its arguments to bash - along the lines of:
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "$HOME/processes/process.sh args");
From a command line, both $HOME and ~ are expanded to a user's home directory by the shell.
ProcessBuilder runs the process directly with no shell, so those expansions will not work.
Not tried it myself but that should work:
Change into the home before and then execute the process.sh
cd && ./processes/process.sh
I'm trying to run Ant from a Java program. This works:
// Compile project
ProcessBuilder pb =
new ProcessBuilder("/usr/local/Cellar/ant/1.9.6/libexec/bin/ant", "-f", pathToProject + "build.xml");
Process p = pb.start();
p.waitFor();
However, this program will be used on machines other than my own, so I'd rather instantiate the ProcessBuilder like this:
new ProcessBuilder("ant", "-f", pathToProject + "build.xml");
Although ant -f build.xml works on my Mac terminal, placing the above code in my program outputs java.io.IOException: Cannot run program "ant": error=2, No such file or directory.
How can I programmatically show ProcessBuilder where Ant is located? This program will be run on OS X and possibly Ubuntu.
Note: This question is not Ant-specific. I need use ProcessBuilder to invoke other command-line tools as well.
Inject the absolute path of the ant to ProcessBuilder by making it customizable through properties file.This way you can run on multiple environments just by providing the corresponding path in the properties file.
I do this:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", updater.getAbsolutePath(), downStuff.getAbsolutePath(), actStuff.getAbsolutePath(), toRunAfter.getAbsolutePath());
pb.start();
...passing 3 string file paths as an arguments. Working beautifully on Windows, but Mac keeps saying: "No Java runtime present, requesting install."
Do I need to install JDK to client's machine just to get this simple command working? ...or is there any other way how I can run external jar file programatically while passing an arguments?
Because right now it seems like I'll have to save those three lines of text into a file just because Oracle and Apple didn't solve their s**t together...
I am trying to execute make clean command from a Java program.I need to change the directory first and then execute it.Or is there any other method?
I have tried both the ways as stated in the site.
1. using ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("make "," clean");
pb.directory(new File("\\home\\p\\lipta-v1-3"));
pb.start();
Using Runtime
Runtime runtime = Runtime.getRuntime();
String command="make clean";
process=runtime.exec(command,null,new File("/home/p/lipta-v1-3"));
However none of them are working.The other commands such as cp ,rm etc.are working fine.
I am using Netbeans7.0.1 and ubuntu 12.04
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();