The Artima Developer Community
Sponsored Link

Java Answers Forum
Execute a batch file from a Java class

2 replies on 1 page. Most recent reply: Dec 10, 2003 2:04 PM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Goudakos Kostas

Posts: 1
Nickname: raeziel
Registered: Dec, 2003

Execute a batch file from a Java class Posted: Dec 10, 2003 6:39 AM
Reply to this message Reply
Advertisement
Hi everyone,

My question is how can I execute a batch file from a Java class. I tried:

public class test {

public static void main(String[] args) throws IOException{
Runtime.getRuntime().exec("C:\\export.bat");
}
}

but it doesn't work. I replaced my filename with "notepad.exe" and it run, so I'm thinking that it only works for .exe files.

Does anyone knows how can I execute a .bat file?

Thanks in advance.


Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Execute a batch file from a Java class Posted: Dec 10, 2003 2:02 PM
Reply to this message Reply
How do you know that it is not executing? I was able to successfully execute a batch file with the following:
package com.joe.tests;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
/**
 * Execute a Batch File
 */
public class RunBatch {
 
	public static void main(String[] args) throws IOException, InterruptedException {
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec("c:\\temp\\test.bat " + System.currentTimeMillis());
		process.waitFor();
		BufferedReader reader =
			new BufferedReader(
				new InputStreamReader(process.getErrorStream()));
 
		String line = reader.readLine();
		while (line!= null) {
			System.out.println(line);
			line = reader.readLine();
		}
		
		BufferedReader errorReader =
			new BufferedReader(
				new InputStreamReader(process.getErrorStream()));
		line = errorReader.readLine();
		while (line != null) {
			System.out.println(line);
			line = errorReader.readLine();
		}
	}
}


Here's the contents of the bat file:

md c:\temp\stuff%1

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Execute a batch file from a Java class Posted: Dec 10, 2003 2:04 PM
Reply to this message Reply
Sorry. copy-paste error. The first "getErrorStream()" should be "getInputStream()".

Flat View: This topic has 2 replies on 1 page
Topic: Using Final in method parameters Previous Topic   Next Topic Topic: simple java drawing packahge

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use