Problem executing a batch file in a Java application
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.
精彩评论