How to run bash script from Java - 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();

Related

run python script from batchfile doesn't work by using java ProcessBuilder

I need to run a batch file from Java code, in which a python script shall be executed and it's not working.
Run the batch file by doubleclick, the python script executes correctly. Run the batch file with JAVA ProcessBuilder doesn't work completely. textoutput from batch file(-->echo) is printed, but python isn't called. How can I fix that? I've even tried to run the Python Script directly with ProcessBuilder, but no output is generated either.
run Python directly in JAVA
String pyPath = "D:/<...>/my_py_file.py";
ProcessBuilder pb = new ProcessBuilder("cmd","/c","D:/Python27/ArcGISx6410.5/python",pyPath);
Process proc = pb.start();
This isn't working as well as:
run Batch file in JAVA, calling my_py_file.py
ProcessBuilder pb = new ProcessBuilder("cmd","/c","D:/<...>/my_bat_file.bat");
Process proc = pb.start();
This is my bat-file:
#echo execute script
#echo off
cd\
d:
cd Python27/ArcGISx6410.5
#echo Python Version
python -V
#echo start script
python D:/<..>/my_py_file.py
exit
OUTPUT by doubleclick:
execute script
Python Version
Python 2.7.13
start script
**pythonOutput**
OUTPUT by using ProcessBuilder:
execute script
Python Version
start script
*nothing*
I think what's missing in your "run Python directly in JAVA" implementation is that you don't process the output after Process proc = pb.start();
For example, you can take redirect the output-stream to a file like this:
pb.redirectOutput(Redirect.appendTo(FILENAME));
(The file referenced should already be created before redirecting there)
Or you can channel it into an InputStream that you feed into an InputStreamReader for further processing, like this:
BufferedReader br = new BufferedReader(new InputStreamReader(pb.getInputStream()));
So either way, add it after Process proc = pb.start();

Running an external program from Katalon's Groovy / Java test case

In my Katalon project (running on Windoes OS), I like to run an external python program.
I was looking at several examples of how to execute an external program from a Java or Groovy program.
The problem is that no matter what program I try to run (the python script or even a simple cd command), I get the following error from Katalon-Studio:
[ERROR] - Test Cases/CallPython FAILED because (of) java.io.IOException: Cannot run program "cd": CreateProcess error=2, The system cannot find the file specified
Here are some of the options I tried:
Groovy:
println "python myp.py".execute().text
println "cd".execute().text
Java
Process p = Runtime.getRuntime().exec("python myp.py");
String[] cmd = ["python", "myp.py"];
Process p = Runtime.getRuntime().exec(cmd);
Process p = Runtime.getRuntime().exec("cd");
If everything has good HOME_PATH this code should help
ProcessBuilder pb = new ProcessBuilder("cmd", "python myp.py")
Process process = pb.start()

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.

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

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

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

Categories

Resources