在浏览器窗口中启动Play应用程序,以允许用户使用Java

时间:2018-06-15 14:15:33

标签: java playframework sbt

我有一个正在运行的Play应用程序,我想启动另一个Play应用程序并打开一个浏览器窗口来启动应用程序,以便用户可以单击Apply changes now按钮以允许Evolution发生。单击按钮后,我想关闭应用程序 - 浏览器窗口和sbt中正在运行的应用程序。

听起来像Java Runtime.getRuntime().exec()方法是可行的,但是我编写的代码只是挂起。它在sbt中启动应用程序 - 在运行当前应用程序的同一Windows命令提示符中 - 但永远不会到达下一个命令来打开浏览器窗口。没有错误。

这是我打电话的方法:

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);
}

这是StreamWriter类:

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();
        }
    }
}

这是输出:

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>

第一个命令正常运行 - 启动sbt

String[] command = new String[] { "cmd", "/c", "C: && cd C:\\" + appName + " && sbt \"run 8080\"" };

但它永远不会完成 - 永远不会点击waitFor()代码。

我发现了一些我一直指的帖子:

https://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2

start windows service from java

http://www.java-samples.com/showtutorial.php?tutorialid=8

这可能或者有更好的方法来解决这个问题吗?

我很感激帮助。

0 个答案:

没有答案