如何从java执行批处理文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1421143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:53:40  来源:igfitidea点击:

How to execute a batch file from java?

javabatch-file

提问by Yatendra Goel

I want to execute a batch file from a java program.

我想从 Java 程序执行批处理文件。

I am using the following command.

我正在使用以下命令。

Runtime.getRuntime().exec("server.bat");

But the problem is I want to give a reative path instead of absolute path so that I can deploy that java project on any comp.

但问题是我想给出一个相对路径而不是绝对路径,以便我可以在任何 comp 上部署该 java 项目。

The dir structure of the project is like as follows:

项目的目录结构如下:

com
   |
  project
   |
   ------ parser
   |         |_____ Main.java
   |
   -------util
             |_____ Server.bat

I want to run the "Server.bat" file in the "util" dir from the "Main.java" file in the "parser" dir.

我想从“解析器”目录中的“Main.java”文件中的“util”目录中运行“Server.bat”文件。

采纳答案by PeterMmm

When Java is running and you use Runtime.exec() with a relative path, relative means relative to the current user direcory, where the JVM was invoked.

当 Java 正在运行并且您使用带有相对路径的 Runtime.exec() 时,relative 表示相对于调用 JVM 的当前用户目录。

This may work

这可能有效

Runtime.getRuntime().exec("cmd.exe", "/c", "./com/projct/util/server.bat");

if you start java from com's parent directory.

如果您从 com 的父目录启动 java。

Or you must calculate an absolut path:

或者你必须计算一个绝对路径:

Runtime.getRuntime().exec("cmd.exe", "/c", 
System.getProperty("user.dir")+"/com/projct/util/server.bat");

I forget, read When Runtime.exec() won't.

我忘了,请阅读When Runtime.exec() won't

回答by JesperE

You have to run "cmd.exe" with the arguments "/c" and "server.bat":

您必须使用参数“/c”和“server.bat”运行“cmd.exe”:

Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", "server.bat" } );

回答by Rorick

You can use ProcessBuilderfor this. It provides much more control than exec. Particularly, it allows to set working directory with method directory.

您可以ProcessBuilder为此使用。它提供了比exec. 特别是,它允许使用方法设置工作目录directory

Example:

例子:

ProcessBuilder pb = new ProcessBuilder("server.bat");
pb.directory(new File(deployDir + "\com\project\util"));
Process p = pb.start();
int exitStatus = p.waitFor();

Of course, your app must get deployDir from somewhere. It can be set in environment, in application configuration file, it can be current user directory or anything else.

当然,您的应用程序必须从某个地方获取 deployDir。它可以在环境中设置,在应用程序配置文件中,可以是当前用户目录或其他任何内容。

回答by Rich Seller

Plexus utilsprovides a Commandline type that can invoke an arbitrary command line and handle parsing of the output.

Plexus utils提供了一种命令行类型,可以调用任意命令行并处理输出的解析。

Commandline cl = new Commandline();

cl.setExecutable( "cmd.exe" );
cl.createArg().setValue( "/c" );

cl.setWorkingDirectory( new File(System.getProperty("user.dir"), 
    "/com/project/util/Server.bat"));

cl.createArg().setValue( "/c" );

StreamConsumer consumer = new StreamConsumer() {
    public void consumeLine( String line ) {
        //do something with the line
    }
};

StreamConsumer stderr = new StreamConsumer() {
    public void consumeLine( String line ) {
        //do something with the line
    }
};

int exitCode;

try {
    exitCode = CommandLineUtils.execute( cl, consumer, stderr, getLogger() );
} catch ( CommandLineException ex ) {
    //handle exception
}

回答by Dan Polites

You're best bet is to store the installation directory of the application on the system and then use that to build your paths within the application. System.getProperty("user.dir") should work on Windows and Unix platforms to get the current working directory, but it is system dependent so be aware of that.

最好的办法是将应用程序的安装目录存储在系统上,然后使用它在应用程序中构建路径。System.getProperty("user.dir") 应该在 Windows 和 Unix 平台上工作以获取当前工作目录,但它依赖于系统,因此请注意这一点。

回答by JRL

The second parameter to exec is a String[] of args for the environment settings (null means inherit the process' current ones) and the third parameter to exec should be a file providing the working directory. Try this:

exec 的第二个参数是环境设置参数的 String[](空表示继承进程的当前参数),exec 的第三个参数应该是提供工作目录的文件。尝试这个:

Runtime.getRuntime().exec("cmd /c server.bat", null, new File("./com/project/util"));

回答by user85421

You can try it with Desktop if supported (Java 1.6)

如果支持(Java 1.6),您可以尝试使用桌面

    File file = new File("server.bat");
    Desktop.getDesktop().open(file);