Reputation: 764
My program requires that I run a .bat file which will compile java source. This is running fine, however I am looking for a solution which will get the output (and possible errors) of compile.bat and add it to a text pane on a GUI. I have the following code, however when executed the process happens without printing anything to the pane and without any errors.
GenerationDebugWindow.main(null);
Process process = rut.exec(new String[] {file.getAbsolutePath() + "\\compile.bat"});
Scanner input = new Scanner(process.getInputStream());
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
int exit = -1;
while ((line = reader.readLine()) != null) {
// Outputs your process execution
try {
exit = process.exitValue();
GenerationDebugWindow.writeToPane(line);
System.out.println(line);
if (exit == 0) {
GenerationDebugWindow.writeToPane("Compilation Finished!");
if(new File(file + "/mod_" + WindowMain.modName.getText()).exists()){
GenerationDebugWindow.writeToPane("Compilation May Have Experienced Errors.");
}
}
} catch (IllegalThreadStateException t) {
}
}
GenerationDebugWindow
private static JTextPane outputPane;
public static void writeToPane(String i){
outputPane.setText(outputPane.getText() + i + "\r\n");
}
Upvotes: 1
Views: 3083
Reputation: 168835
My program requires that I run a .bat file which will compile java source.
Your users on *nix and OS X require that source be compiled using the JavaCompiler
.
The STBC is an example of using the JavaCompiler
. It is open source. It uses a JTextArea
as opposed to a JTextPane
to hold the source and errors, but should be trivial to adapt.
Upvotes: 0
Reputation: 11805
Reference this question: Java Process with Input/Output Stream
It's likely that the output of the process is going to the error stream. However, ProcessBuilder is a more useful class than directly using System.getRuntime().exec()
In the example below, we're telling the ProcessBuilder to redirect the error stream to the same stream as the output goes to, which simplifies the code.
ProcessBuilder builder = new ProcessBuilder("cmd.exe /C " + file.getAbsolutePath() + "\\compile.bat");
builder.redirectErrorStream(true);
builder.directory(executionDirectory); // if you want to run from a specific directory
Process process = builder.start();
Reader reader = ...;
String line = null;
while ((line = reader.readLine ()) != null) {
System.out.println ("Stdout: " + line);
}
int exitValue = process.exitValue();
Upvotes: 1
Reputation: 159844
Use:
Runtime.getRuntime().exec( "cmd.exe /C " + file.getAbsolutePath() + "\\compile.bat" );
Upvotes: 2