I have a Play application that is running and I wanted to start up another Play application and open a browser window to start the application so the user can click on the Apply changes now
button to allow the Evolution to happen. Once the button is clicked, I wanted to close the application - both the browser window and the running application in sbt
.
It sounds like the Java Runtime.getRuntime().exec()
method is the way to go, however the code I have written just hangs. It starts the application in sbt
- in the same Windows command prompt that is running the current application - but never gets to the next command to open the browser window. There are no errors.
Here is the method I am calling:
public static void createTablesCMD(String appName, String mainTable, Database db, String mysqlusername,
String mysqlpassword) throws IOException, Throwable {
// Let's add the tables to the database we just built...
// Start up sbt...
String[] command = new String[] { "cmd", "/c", "C: && cd C:\\" + appName + " && sbt \"run 8080\"" };
executeRuntime(command);
// Open the browser window...
command = new String[] { "cmd", "/c", "start chrome http://localhost:8080" };
executeRuntime(command);
}
public static void executeRuntime(String[] command) throws IOException, Throwable {
System.out.println("command: " + command);
System.setProperty("user.dir", "C:\\");
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
// Any error message?
StreamWriter errorWriter = new StreamWriter(proc.getErrorStream(), "ERROR");
// Any output?
StreamWriter outputWriter = new StreamWriter(proc.getInputStream(), "OUTPUT");
// Start up the writers...
errorWriter.start();
outputWriter.start();
// Any error?
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
}
Here is the StreamWriter class:
import java.io.*;
public class StreamWriter extends Thread {
InputStream is;
String type;
StreamWriter(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Here is the output:
command: [Ljava.lang.String;@10aff69
OUTPUT>Listening for transport dt_socket at address: 50933
OUTPUT>[info] Loading project definition from C:\MyApp\project
OUTPUT>[info] Set current project to MyApp (in build file:/C:/MyApp/)
OUTPUT>[info] Updating {file:/C:/MyApp/}root...
OUTPUT>[info] Resolving org.scala-lang#scala-library;2.11.8 ...
OUTPUT>
OUTPUT>[info] Resolving com.typesafe.play#play-enhancer;1.1.0 ...
OUTPUT>
...
OUTPUT>[info] Done updating.
OUTPUT>
OUTPUT>--- (Running the application, auto-reloading is enabled) ---
OUTPUT>
OUTPUT>[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:8080
OUTPUT>
OUTPUT>(Server started, use Ctrl+D to stop and go back to the console...)
OUTPUT>
The first command runs fine - starting up sbt
:
String[] command = new String[] { "cmd", "/c", "C: && cd C:\\" + appName + " && sbt \"run 8080\"" };
but it never completes - never hits the waitFor()
code.
I found a few posts that I have been referring to:
http://www.java-samples.com/showtutorial.php?tutorialid=8
Is this possible to do or is there a better way to handle this?
I appreciate the help.