views:

6045

answers:

3

In my Java application I want to run a batch file that calls "scons -Q implicit-deps-changed build\file_load_type export\file_load_type"

It seems that I can't even get my batch file to execute. I'm out of ideas.

This is what I have in Java:

Runtime.getRuntime().exec("build.bat", null, new File("."));

Previously I had a python Sconscript file that I wanted to run but since that didn't work I decided I would call the script via a batch file but that method has not been successful as of yet.

+2  A: 

The executable used to run batch scripts is cmd.exe which uses the /c flag to specify the name of the batch file to run:

Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "build.bat"});

Theoretically you should also be able to run Scons in this manner, though I haven't tested this:

Runtime.getRuntime().exec(new String[]{"scons", "-Q", "implicit-deps-changed", "build\file_load_type", "export\file_load_type"});

EDIT: Amara, you say that this isn't working. The error you listed is the error you'd get when running Java from a Cygwin terminal on a Windows box; is this what you're doing? The problem with that is that Windows and Cygwin have different paths, so the Windows version of Java won't find the scons executable on your Cygwin path. I can explain further if this turns out to be your problem.

Eli Courtwright
Thank you. It still doesn't work - that piece of code doesn't even execute in my app. I'll try the other option you presented. Thanks again.
Amara
When I try the second alternative it gives me this error: Exception in thread "main" java.io.IOException: Cannot run program "scons": CreateProcess error=2, The system cannot find the file specified
Amara
No I don't have Cygwin terminal. I use Windows Command terminal. It's strange - I don't know why it wouldn't work. It completely baffles me.
Amara
+4  A: 
Runtime.getRuntime().exec("cmd /c start build.bat");
Paulo Guedes
This works! Why does it work? I'm curious. I have been stuck on this for too long. Thank you to all. I love SO! :)
Amara
Batch files alone are not executable. They need an application to tun them, in this case, cmd. On UNIX-likes you have the shebang (#!) at the start of a file to specify the program that executes it. Double-clicking in Windows is performed by explorer. CreateProcess doesn't know anything about that.
Joey
Okay. Makes sense. Thank you!
Amara
I was going to answer that but Johannes did it perfectly. :-)
Paulo Guedes
+2  A: 

ProcessBuilder is the Java 5/6 way to run external processes.

basszero
Why is the ProcessBuilder the way to go in Java 5/6?
hoffmandirt
Interesting choice to resurrect an old post ... ProcessBuilder offers more control, specifically the ability to easily redirect stderr to stdout. I also find the setup more intuitive, but that's a personal pref
basszero