How to execute a batch file from java?

2020-02-05 03:44发布

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

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.

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.

7条回答
太酷不给撩
2楼-- · 2020-02-05 04:16

You can use ProcessBuilder for this. It provides much more control than exec. Particularly, it allows to set working directory with method 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.

查看更多
做自己的国王
3楼-- · 2020-02-05 04:20

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.

This may work

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

if you start java from com's parent directory.

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.

查看更多
够拽才男人
4楼-- · 2020-02-05 04:21

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:

Runtime.getRuntime().exec("cmd /c server.bat", null, new File("./com/project/util"));
查看更多
smile是对你的礼貌
5楼-- · 2020-02-05 04:25

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

Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", "server.bat" } );
查看更多
ゆ 、 Hurt°
6楼-- · 2020-02-05 04:28

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.

查看更多
戒情不戒烟
7楼-- · 2020-02-05 04:35

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

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
}
查看更多
登录 后发表回答