Run command-line program in Java without absolute path - java

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.

Related

Processbuilder to run Batch file and pythonscript

My requirement is i have to run Batch file located in (C:\Users\Vk\TestBatch.bat) under C:\users\VK\Logs(This is the path in which i have to run the batch file). After that i have to run the python script. I have to do this by using ProcessBuilder. Below is the piece of code i am using, but unfortunately it is not working.
String[] command ={"cmd.exe","/C","cd C:\\Users\\vk\\Logs","C:\\users\\Vk\\TestBatch.bat",
"C:\\Python27\\ArcGIS10.3\\python.exe","C:\\Users\\vk\TestScript2.py"};
probuilder = new ProcessBuilder(command );
Can any one suggest me how to run this batch file in other location and followed by the execution of python script.
Thanks,
Sudheer
There are two options to achieve this.
Either add your python executable in your System Path.
You can go to the particular directory where executable is located and run the python script.
a. Go to Directory where batch file is located.
b. Run batch file by simply putting the name.
c. go to home directory by command cd
d. go to directory where python executable is present.
e. run python script using command `py <scriptName>`
String [] command = {"cd C:\users\Vk", "TestBatch", "cd", "cd C:\Python27\ArcGIS10.3\", "py TestScript2.py"};
ProcessBuilder probuilder = new ProcessBuilder(command);

Can not find or load class with Java ProcessBuilder using -cp and jar location

I am trying to use ProcessBuilder to start a JUnit test within my Java Application.
I am able to run the same command from the command line without issue. Do I need to use the absolute path for the jar when running from ProcessBuilder or can I use the relative path?
Running on the command line
java -cp .;lib/junit-4.12.jar org.junit.runner.JUnitCore com.test.Test1
Running inside my application
The junit library is in the lib folder
application/lib/junit-4.12.jar
ProcessBuilder builder = new ProcessBuilder(new String[] {"java", "-cp", ".;lib/junit-4.12.jar", "com.test.Test1"});
Process process = builder.start();
process.waitFor();
debug("process ended");
debug("process.exitValue() = " + process.exitValue());
Output:
process ended
process.exitValue() = 1
Error: Could not find or load main class org.junit.runner.JUnitCore
You can find what the classpath is when you run your application by using
System.getProperty("java.class.path");
Then, modify "cp" in your ProcessBuilder() statement accordingly.
Set the base directory for the ProcessBuilder you are wanting to use.
File libDir = new File("/opt/app/lib");
builder.directory(libDir);
This is the solution I ended up using after #mazaneicha 's comment to relook at the app's classpath directory.

Getting ProcessBuilder (or command execution in general) working on Mac with JRE?

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...

Using ProcessBuilder to run ImageMagick command

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();

Why can I set a path in the command prompt but not my program?

I want to set a path in my Java program with this Windows command (this path contains some DLL files that are used in a native peripheral of my program):
c:\>path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"
But this causes an exception when the program runs:
java.io.IOException: Cannot run program "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\": CreateProcess error=2, The system cannot find the file specified
I don't know why I can set the path with no problem in the command prompt but then get an exception thrown in the code.
String path = "C:\\Users\\NetBeansProjects\\IPTV1.7\\3rd_party\\";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("path=%path%;"+ path);
Your command
path=%path%;"C:\Users\NetBeansProjects\IPTV1.7\3rd_party"
is not a "real" windows command, but only a variable assignment, interpreted by your shell (cmd.exe), visible only in the same shell session and any commands (other programs) started from there.
When trying to execute this from Java with Runtime.exec(), the windows CreateProcess function tries to find a executable file with this strange name, which obviously does not exist (can't exist, I think), and you get this error.
Even if you could execute this, for example by calling cmd.exe, it would only influence this same cmd.exe process (and any programs started from there, not your own Java process (and programs started from here).
Depending on what you in fact want, you could, for example:
give the path with ProcessBuilder directly to the process which in fact needs it (like Aaron showed)
search by yourself for executable files, if you want this for finding commands in the next exec
put the variable assignment and other commands together in a .BAT or .CMD file and execute this.
When you type this on the command prompt, the cmd program processes it and changes the PATH variable for you. When you try this with Runtime, no cmd process is created and there is no command "path=%path%;C:\Users\NetBeansProjects\IPTV1.7\3rd_party\" on your harddisk (Windows actually tries to find a program with this exact name).
Put the commands in a .BAT or .CMD file. Windows automatically creates a cmd process to execute them for you.
You can use ProcessBuilder in java to spawn a process and control the environment that it gets. So you would use the ProcessBuilder environment method to set the PATH environment variable, then set the relevant command line, then launch. SO something like (untried):
ProcessBuilder b = new ProcessBuilder();
b.environment().put("PATH", whatever);
b.command(whatever);
Process p = b.start();

Categories

Resources