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

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

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

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.

Launching batch file from JAVA not working for directory path having spaces

I have created a batch file with the following contents
xcopy "C:\Documents\javascript\src\*" "C:\Program Files (x86)\Apache Group\Apache2\htdocs\docs\8.1\version1\" /s /y
I am running the batch file using Java. Running the script from command line or directly executing it(double clicking) doesn't seem to have any problem. However when I run using java the copy operation doesn't succeed. In the console I see a message More? when the script is executed.
Also running the copy operation for directory path without spaces seems to work fine.
Here is the java method which does the batch file running.
public void run(String input)
{
File dir = new File(input);
ProcessBuilder processBuilder = new ProcessBuilder("cmd");
processBuilder.redirectInput(dir);
Process process = processBuilder.start();
int exitStatus = process.waitFor();
process.destroy();
}
Any suggestions? Thanks in advance.
It depends on the way you are launching the bat file
for me this works fine:
Runtime.getRuntime().exec("cmd /c start xcopy-file.bat"

Executing a .exe or .linux file in Java during Run time

I am trying to create a function within a Java Class that can execute a .exe or a .linux file during runtime.
The program is espresso.exe (for Windows OS) and espresso.linux for (Linux based systems)
Typically, the way to run the program is by going to command line, and going to the folder in which the executable is stored and typing:
(in Command Prompt)
espresso A0.txt > m.txt
or espresso A0.txt (which returns the output in cmd)
(in linux Terminal)
./espresso.linux A0.txt > m.txt
or ./espresso.linux A0.txt (which returns the output in the terminal window)
Here A0.txt is the input argument and m.txt is the file that espresso creates.
I have stored A0.txt and espresso.linux and espresso.exe under a folder src/resources
I tried the following:
ProcessBuilder pb = new ProcessBuilder("./src/resources/espresso.exe","src/resources/A0.txt",">src/resources/m.txt");
try {
Process p = pb.start();
}catch (IOException ex) {
Logger.getLogger(NetSynth.class.getName()).log(Level.SEVERE, null, ex);
}
I also tried:
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("src/resources/espresso.linux src/resources/A0.txt > src/resources/m.txt");
int waitFor = p.waitFor();
Both of them fail to identify the file to be executed and do not run the command. I understand there may be many errors in the 2 approaches. I could use some help to figure out the approach and the code to be written to run the executable file.
Also, is there a path to be mentioned to run espresso.linux? Will /src/resources/espresso.linux suffice?
Thanks in advance.
You can't do standard output redirection like this (because the ">" sign is interpreted by the OS shell), see this answer for a working solution: ProcessBuilder redirecting output
Since Java 7 there is a Java-only solution in order to achieve redirecting: http://tamanmohamed.blogspot.co.at/2012/06/jdk7-processbuilder-and-how-redirecting.html
The > is a shell syntax. If you want to redirect the output to a file you need to use a shell or read the output and write it to a file yourself.
The way you have used > it is just another argument.

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