views:

1395

answers:

7

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.

+4  A: 

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

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

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.

Rorick
+1  A: 

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.

PeterMmm
there is no "exec" method in Runtime class that takes three string parameters.
Yatendra Goel
That is true. I mean the String[] version but i'm not be able to input square parenthsis in SO text box ... i think i must read SO manuals/wiki ...
PeterMmm
A: 

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
}
Rich Seller
A: 

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.

hoffmandirt
A: 

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"));
JRL
A: 

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

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