简体   繁体   English

在Java应用程序中执行批处理文件时出现问题

[英]Problem executing a batch file in a Java application

I have batch file, named run.bat which inlcudes the following code: 我有批处理文件,名为run.bat,它包含以下代码:

@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: 但是,当我尝试使用以下语句在java文件中执行时:

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. 在PATH中找不到Java.exe。

If you can assume that the JAVA_HOME variable is defined, you can modify your batch file: 如果您可以假定已定义JAVA_HOME变量,则可以修改批处理文件:

%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 正如staker建议的那样,更好的方法是将PATH环境变量设置为包含%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. 作为第三个选项,您还可以通过直接调用jar的main类来避免使用批处理文件。 You archiveServer would run in the same process, however. 但是,archiveServer将在同一进程中运行。 Maybe that's not want you want. 也许这不是你想要的。

我想你没有在你的PATH环境变量中添加JAVA_HOME/bin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM