Reputation: 1508
I made 2 .java files and 1 .bat file. Lets refer to the first .java file as "First.java" and the second as "Second.java". Batch file is "compile.bat". Lets say First.java is located in ".../Desktop/temp/First.java", and Second.java is in ".../Desktop/Test/Second.java" along with a batch file (".../Desktop/Test/compile.bat"). Now, compile.bat consists of this code:
cmd /c "cd C:\blahblah\temp && javac First.java"
I tested it out both in CMD and by double clicking, it works. I want to compile First.java from the Second.java with this code:
Process p1 = Runtime.getRuntime().exec("C:\\blahblah\\compile.bat");
(I have to navigate to dir since the default dir of CMD is not the same as the directory of compile.bat). This doesn't work. It seems it simply ignores the code. As I said, I tried a lot of different things, I even tried using some other library that should have changed the dir. Help me please!
Upvotes: 2
Views: 3357
Reputation: 3685
Easiest way: you should use start
. Setting working dir is also possible, look at this example:
Runtime r=Runtime.getRuntime();
r.exec("cmd.exe /c start compile.bat", //path to executable
null, // env vars, null means pass parent env
new File("C:\\blahblah")); // working directory
Additional information:
If you do not want to start your process in it's separate console (that's what start
does) you can do r.exec("cmd.exe /c compile.bat");
, but because it executes in context of it's parent console, you must wait with p.waitFor()
or read it's input stream - otherwise it may silently fail.
This will run and display output from you command:
Process p=r.exec("cmd.exe /c compile.bat", //path to executable
null, // env vars, null means pass parent env
new File("C:\\blahblah"));
InputStream is=p.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String line=new String();
while ((line=br.readLine())!=null) System.out.println (line);
Also as of 1.5 ProcessBuilder is preffered way to start processes (from Java doc):
java.lang.Process
The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts.
By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.
Where desired, subprocess I/O can also be redirected using methods of the ProcessBuilder class.
The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously.
There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object.
As of 1.5, ProcessBuilder.start() is the preferred way to create a Process.
Since: JDK1.0
Upvotes: 2
Reputation: 3780
If that is a direct quote from the Java, then you're missing the semicolon at the end, the necessary escape characters in the String as well as the cmd command to indicate to Windows that it requires cmd.exe to launch it:
Process p1 = Runtime.getRuntime().exec("cmd C:\\blahblah\\compile.bat");
You can also add /c after cmd to close the process as soon as it completes, and/or start to indicate it should launch in another window.
Upvotes: 1