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!
Easiest way: you should use
start. Setting working dir is also possible, look at this example:Additional information:
If you do not want to start your process in it’s separate console (that’s what
startdoes) you can dor.exec("cmd.exe /c compile.bat");, but because it executes in context of it’s parent console, you must wait withp.waitFor()or read it’s input stream – otherwise it may silently fail.This will run and display output from you command:
Also as of 1.5 ProcessBuilder is preffered way to start processes (from Java doc):