How to Run a Batch File from My Java Application

How do I run a batch file from my Java Application?

Batch files are not an executable. They need an application to run them (i.e. cmd).

On UNIX, the script file has shebang (#!) at the start of a file to specify the program that executes it. Double-clicking in Windows is performed by Windows Explorer. CreateProcess does not know anything about that.

Runtime.
getRuntime().
exec("cmd /c start \"\" build.bat");

Note: With the start \"\" command, a separate command window will be opened with a blank title and any output from the batch file will be displayed there. It should also work with just `cmd /c build.bat", in which case the output can be read from the sub-process in Java if desired.

Run batch file from Java code

Rather than Runtime.exec(String command), you need to use the exec(String command, String[] envp, File dir) method signature:

Process p =  Runtime.getRuntime().exec("cmd /c upsert.bat", null, new File("C:\\Program Files\\salesforce.com\\Data Loader\\cliq_process\\upsert"));

But personally, I'd use ProcessBuilder instead, which is a little more verbose but much easier to use and debug than Runtime.exec().

ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "upsert.bat");
File dir = new File("C:/Program Files/salesforce.com/Data Loader/cliq_process/upsert");
pb.directory(dir);
Process p = pb.start();

How to execute a batch file from java?

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.

How to run a batch file inside Java package using Java code?

Normally when your IDE compiles the sources, the test.bat should be placed in the folder that contains the binaries, preserving the same package structure, e.g. bin/scheduletest/test.bat. Assuming this, you can load the batch file using a classloader:

ClassLoader loader = ScheduleTest.class.getClassLoader();
URL resource = loader.getResource("scheduletest/test.bat");
Process exec = Runtime.getRuntime().exec(resource.getPath());

To read the output of the process, you'll need to get its input stream:

InputStream inputStream = exec.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}

However it's much better to have a resource directory for it, since automatic build procedures (e.g. Ant or Maven) would delete this file when cleaning.

How to run java application by .bat file

Simply create a .bat file with the following lines in it:

@ECHO OFF
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;path/to/needed/jars/my.jar

%JAVA_HOME%\bin\java -Xms128m -Xmx384m -Xnoclassgc ro.my.class.MyClass

how to run a batch file from java?

What do you expect cd: to do? That doesn't look right to me...

If your batch file is only going to run another batch file, why not run that target batch file to start with? If you're worried about the initial working directory, use the overload which takes a File argument to say which directory to use. For example:

File dir = new File("C:\\Tomcat 5.5\\webapps\\mail_testing\\james-2.3.2\\bin");
Runtime.getRuntime().exec("start_james.bat", null, dir);

Running a .bat file in Java

The Runtime.exec() method spawns a process, but doesn't wait for it to complete.

You should use the waitFor() method on the returned Process instance, in order to wait for the process to end.

Plus, @ixe013's comment is correct: you should remove the start word from the command, as it spawns yet another process. If you don't remove start, then waitFor() won't help you much: it will wait for the start command to end, which is instantaneous.



Related Topics



Leave a reply



Submit