Not able to execute command "make clean" from java program - java

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

Related

Run a bash script with "source XXXX.sh" by java [duplicate]

I am having a bash script file which I am calling using the source command in a shell and is setting a number of environment variables. Then I can use all the tools the environment variables are setting.
Now I want to do the same in Java by the use of:
static Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();
I know that source is an internal command and I can not call it from Java.
Is there any other way to set the enviroment variable in that file from java in order to be able to use them in my code later for calling other commands?
Thank you in advance!
Process pr = new ProcessBuilder("/bin/bash", "-c", ". env.sh; " + command).start();
Try something like this, where you both source the script and execute a subsequent command in the same shell process. Effectively you source the script every time you want to execute a command.

Run command-line program in Java without absolute path

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.

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

How to run bash script from Java

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

Categories

Resources