dcsimg
How do you execute a bat file using Java?
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Latta
Posted On:   Thursday, January 16, 2003 04:02 PM

Here is the code inside the bat file: c: mkdir TestFolder" Here is the how I think this should be performed: Note: batchFile is a String containing the correct file path and name. try { Process processToExecute = Runtime.getRuntime().exec("java -cp " + batchFile); } catch (Exception e) { System.out.println("Exception caught in method, loadDataFile(): " + e); } The code executes, but I never see the folder created. If I just simply double click on the bat file, the folder is created. Thanks for your help in advance. - Jason    More>>

Here is the code inside the bat file:

c:
mkdir TestFolder"

Here is the how I think this should be performed:

Note: batchFile is a String containing the correct file path and name.

			
try
{
Process processToExecute = Runtime.getRuntime().exec("java -cp " + batchFile);
}
catch (Exception e)
{
System.out.println("Exception caught in method, loadDataFile(): " + e);
}


The code executes, but I never see the folder created. If I just simply double click on the bat file, the folder is created. Thanks for your help in advance.

- Jason    <<Less

Re: How do you execute a bat file using Java?

Posted By:   Sandip_Chitale  
Posted On:   Saturday, January 18, 2003 11:34 AM

One good way to try out java quickly is using
BeanShell:


http://www.beanshell.org

Re: How do you execute a bat file using Java?

Posted By:   Sandip_Chitale  
Posted On:   Saturday, January 18, 2003 11:34 AM

Why are you running it with Java

try
{
Process processToExecute = Runtime.getRuntime().exec(
"java -cp "
 + batchFile);
}
catch (Exception e)
{
System.out.println("Exception caught in method, loadDataFile(): " + e);
}


Just run it like this -


Process processToExecute = Runtime.getRuntime().exec(batchFile);


If not this should work:


Process processToExecute = Runtime.getRuntime().exec("cmd /C " + batchFile);

Re: How do you execute a bat file using Java?

Posted By:   Sandip_Chitale  
Posted On:   Saturday, January 18, 2003 11:34 AM

Why are you running it with Java

try
{
Process processToExecute = Runtime.getRuntime().exec(
"java -cp "
 + batchFile);
}
catch (Exception e)
{
System.out.println("Exception caught in method, loadDataFile(): " + e);
}


Just run it like this -


Process processToExecute = Runtime.getRuntime().exec(batchFile);


If not this should work:


Process processToExecute = Runtime.getRuntime().exec("cmd /C " + batchFile);

Re: How do you execute a bat file using Java?

Posted By:   Blake_Minghelli  
Posted On:   Thursday, January 16, 2003 05:12 PM

It's not working because you are executing "java". That code would work if you are trying to run a Java program, but you aren't, you are trying to run a batch file.


try this:

String batchFile = "C:\temp\Test.bat";
try
{
Process processToExecute = Runtime.getRuntime().exec(batchFile);
}
catch (Exception e)
{
debug("Exception caught in method, loadDataFile(): " + e);
}
About | Sitemap | Contact