I have batch file, named run.bat which inlcudes the following code:
#echo off
REM bat windows script
set CXF_HOME=.\lib\apache-cxf-2.2.7
java -Djava.util.logging.config.file=%CXF_HOME%\logging.properties -jar archiveServer-0.1.jar
When I execute this file on a command line it works perfectly. However when I try to execute within a java file with the following statement:
File path = new File("C:/Documents and Settings/Zatko/My Documents/Project-workspace/IUG/external/application/archive");
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", "start", "run.bat"}, new String[]{}, path);
I get the following error in the terminal window:
'java' is not recognized as internal or external command, operable program or batch file.
Where the error may be?
Java.exe is not found in your PATH.
If you can assume that the JAVA_HOME variable is defined, you can modify your batch file:
%JAVA_HOME%\bin\java -Djava.util.logging.config.file=%CXF_HOME%\logging.properties -jar archiveServer-0.1.jar
A better way to do it would be, as staker suggested, to set the PATH environment variable to contain %JDK_HOME%\bin
File workingDirectory = new File("C:/Documents and Settings/Zatko/My Documents/Project-workspace/IUG/external/application/archive");
String javaProgram = System.getProperty("java.home") + "\bin";
String[] command = {"cmd.exe", "/C", "start", "run.bat"};
String[] environment = {"PATH=" + javaProgram};
Process process = Runtime.getRuntime().exec(command, environment, workingDirectory);
As a third option, you can also avoid to have the batch file by invoking the main-class of the jar directly. You archiveServer would run in the same process, however. Maybe that's not want you want.
I suppose you didn't add JAVA_HOME/bin to your PATH environment variable.
Related
I'm trying to use ProcessBuilder in order to create a tar.zst file that compresses a folder. However, I'm getting the following error after running this code:
try {
ProcessBuilder pb = new ProcessBuilder("tar","--zstd","-cf","info-x.tar.zst","tstpkg");
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process process = pb.start();
process.waitFor();
} catch (Exception e){
e.printStackTrace();
}
Error:
tar: Can't launch external program: zstd --no-check -3
And the tar file is created, but is empty.
If I run the same command from the terminal
tar --zstd -cf info-x.tar.zst tstpkg
then it works fine.
When you find a command that works "from the terminal" but not from Java then you should assume that the PATH or other environment required with that command is only set correctly via that terminal, and not when called by JVM.
So, one possible fix is to run your terminal indirectly which may fix the environment needed - depending on your shell and it's setup:
ProcessBuilder pb = new ProcessBuilder("bash", "-c", "tar --zstd -cf info-x.tar.zst tstpkg");
You might also try to fix by setting the correct PATH for running zstd as a sub-process of tar. Check where it is using which zstd and try adding to the PATH before pb.start():
String path = pb.environment().get("PATH"); // use Path on Windows
pb.environment().put("PATH", "/path/to/dir/ofzstd"+File.pathSeparator+path);
I am trying through Java to call a batch file in another folder directory.
String cmd = "cmd /c start /wait " + backupFolder + "\\script_encrypt.bat";
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
However, when the batch file runs, it shows me the current directory which the batch is not there, why?.
Are you sure the batch file path and the path called from Java are the same?
According the screenshot from Eclipse and the screenshot from Cmd, they are not matching.
I have done the same, and worked for me.
I am developing a Java application where one of its task is to execute a system command using Runtime exec. The command requires another JAR file to be able to work but whenever the part where the command will be executed, I am getting an "Error: Unable to access jarfile".
Firstly, my current working directory:
assets/jarFile.jar
myApp.jar
Here's what my code looks like:
try {
// To get the path of the directory where my current running JAR is
String jarPath = myApp.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String path = URLDecoder.decode(jarPath.substring(0, jarPath.lastIndexOf("/")), "UTF-8");
// The system command to execute
String command = "java -Xmx1024m -jar "+path+"/assets/jarFile.jar and-so-on...";
Process p = Runtime.getRuntime().exec(command);
...
}
The jarFile.jar is not an executable JAR by the way.
Any idea?
Seems like your code to find the path of the jar file does not return the correct path.
For a project to automate some mutation adequacy testing, I'm trying to make GoLang from source from inside a Java program. I have been able to make it from source in the Terminal, and have tried using that command in Java's Runtime.getRuntime().exec() command:
String[] envp = new String[3];
envp[0] = "CC=/usr/bin/clang";
envp[1] = "GOROOT_BOOTSTRAP=/usr/local/go";
envp[2] = "CGO_ENABLED=0";
Runtime.getRuntime().exec("./all.bash", envp, "$HOME/Desktop/go/src");
The equivalent command to this works fine in the Terminal. Running this code in java (And printing the output) gets the following:
./all.bash
##### Building Go bootstrap tool.
cmd/dist
go tool dist: FAILED: uname -r: exec: "uname": executable file not found in $PATH
So that's weird that it can't find uname. Again, if I enter 'uname' on the Terminal, it works fine. So I found the directory of uname ('which uname' gives '/usr/bin/uname') and set $PATH to that for this command:
String[] envp = new String[4];
envp[0] = "CC=/usr/bin/clang";
envp[1] = "GOROOT_BOOTSTRAP=/usr/local/go";
envp[2] = "CGO_ENABLED=0";
envp[3] = "PATH=/usr/bin";
Runtime.getRuntime().exec("./all.bash", envp, "$HOME/Desktop/go/src");
And that instead gets the output:
./all.bash
env: bash: No such file or directory
So when I set the path, it can't find the program in the directory. This suggests to me that when Runtime.getRuntime().exec() is called, it overwrites $PATH to be the directory I passed it, then overwrites the environment variables I gave it. But in order for ./all.bash to work, I need both paths to be in the $PATH variable. How can I do this?
On Mac OS X 10.11.6.
Runtime.exec was replaced by ProcessBuilder twelve years ago, as part of Java 1.5.
Among its many superior features is the ability to add to the existing environment:
ProcessBuilder builder = new ProcessBuilder("./all.bash");
builder.inheritIO();
builder.directory(
new File(System.getProperty("user.home") + "/Desktop/go/src"));
builder.environment().put("CC", "/usr/bin/clang");
builder.environment().put("GOROOT_BOOTSTRAP", "/usr/local/go");
builder.environment().put("CGO_ENABLED", "0");
builder.start();
How to change content of batch file using Java code?
I worked with parsing XML using Java program. It worked fine. But can I do same for the batch file using Java?
I am able to run batch file using below code.
String command = "cmd /c start " + batFile;
Runtime rt = Runtime.getRuntime();
rt.exec(command);
The content of my batch file is:
#echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C "C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml"
exit
But before doing this I want to change location
C:\shravani-workspace\appanalytix\src\main\resources\appanalysis.xml
..to user given XML location. How can I achieve this in my Java application?
Maybe use an environment variable instead. This way you don't need to edit the batch file, just set the variable before running it.
Here or here see how to set an environment variable from java and here's how to use them in a batch file.
How about passing command line args to the bat file like so:
#echo off
cd C:\Program Files (x86)\SourceMonitor
start SourceMonitor.exe /C %1
exit
Then modify your java code to pass in the XML file name after the bat file name