Processbuilder to run Batch file and pythonscript - java

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

Related

Using Java ProcessBuilder, how do I run a process located in a unix home directory?

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

Why can't my Java web application find the .bat file?

I am facing a problem with a .bat file run from Java web application.
Current setup in local machine: create web application and method contains
Process p = Runtime.getRuntime().exec("c:/test/myFile.bat");
When I run above code, its showing file not found.
But if I copied the .bat file to tomcat bin folder it's working fine. But I need to execute the bat file in my separate folder, I even set the class path also, but I'm not able to execute the bat.
Screen 1:
I have placed 5 files in Tomcat root directory din folder, as well as placed at c:/psgsscripts/--> folder also (check the Java code; only bat file is taken other .tbc files are looking at Tomcat bin folder location at run time)
Screen 2:
bat file contains tclsh psg.tbc %1
Screen 3:
web application java code in class method( newjobid is the parameter for .bat file)
Screen 4:
If I placed it all .bat file and .tbc files bin folder it's working fine (Java code represent read the .bat file from c drive but other .tbc files look at Tomcat bin folder at run time)
but requirement is those files are placed at other drives (other than Tomcat folder) like C or D or E drivers.
You need to execute the command to run the batch file like below
Runtime.getRuntime().exec("cmd /c start c:\\test\\myFile.bat");
Try the following:
Process p = Runtime.getRuntime().exec("cmd /C start c:/test/myFile.bat");
If you look at the java docs, the method exec expects an OS command and not the file name. So in the proposed solution, "cmd" is the OS command, /C is a switch that tells the OS command to carry out the command specified by string and then terminate. Here the command specified by the string is start. The command "start" requires a file name with full path as its parameter.

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.

execute file from defined directory with Runtime.getRuntime().exec

I just want to execute my file from a specific folder. in my case /data/data/my-package/files/.
So i tried :
Process process2=Runtime.getRuntime().exec("cd /data/data/my-package/files/");
process2.waitFor();
process2=Runtime.getRuntime().exec("./myfile");
It doesn't work. could anyone tell me please the right way to do that . Thanks
It should be possible to call the executable with a specific working directory using Runtime.exec(String command, String[] envp, File dir)
as follows:
Process process2=Runtime.getRuntime().exec("/data/data/my-package/files/myfile",
null, new File("/data/data/my-package/files"));
maybe without the full path to myfile
Process process2=Runtime.getRuntime().exec("myfile",
null, new File("/data/data/my-package/files"));
Context#getFilesDir() instead of hardcoding the path should work too and is safer / cleaner than specifying the path yourself since it is not guaranteed that /data/data/..
is always the correct path for all devices.
Process process2=Runtime.getRuntime().exec("myfile",
null, getFilesDir()));
The problem with cd somewhere is that the directory is changed for a different Process so the second call to exec in a new Process does not see the change.
It works for me when I use the following overloaded method:
public Process exec(String command,
String[] envp,
File dir)
For example:
File dir = new File("C:/Users/username/Desktop/Sample");
String cmd = "java -jar BatchSample.jar";
Process process = Runtime.getRuntime().exec(cmd, null, dir);
The command just stores the command you want to run in command line. dir just stores the path of your .jar file to be executed.
A different solution will be,
Execute a .bat file from the Java Code
And do all the directory change, and stuff inside the bat file
For instance, my execute.bat file looks like this,
cd flutter_app
flutter build apk
cd ..
And the Java code looks like this,
Process process = Runtime.getRuntime().exec("execute.bat");

Execute jar cvf command by java code ( using Runtime.exec() method )

I am using the Java code to execute a bat file to make a war file by the following java code
String command = "cmd /C start C:/processFolder/paas.bat";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
which means it will execute the paas.bat file to do some jobs. There is a command within the paas.bat :
jar cfv xxx.war */ .
however, it does not jar all the file and folder into xxx.war. Does any one has the idea why this happens? Thanks!
The command uses current working directory, which will be different from java program. You may change script to do a cd and then call jar command.
cd /d <path to folder where this should execute from>
jar ....
Please capture output , error and exceptions(if any) from the command and add to your question.
(note#1. It may be more useful to use tools like ant to do these task. The tasks in ant can be called from java program using ant libraries..
Note#2 - Prefer ProcessBuilder to launch new process. )

Categories

Resources