我启动了一个play框架项目,我需要通过java类main方法来运行它。 这是我的 Java 类。
import java.util.Scanner;
public class Tester {
public static void main(String[] args) {
System.out.println("Menu");
System.out.println("Press A to add");
System.out.println("Press D to Delete");
System.out.println("Press V to View");
System.out.println("Press G to Start Server");
Scanner scanner = new Scanner(System.in);
System.out.print("Please Enter Your Select: ");
String select = scanner.nextLine();
switch (select){
case "A":
// add method
break;
case "D":
// delete method
break;
case "V":
// view method
break;
case "G":
// start server
break;
default:
System.out.println("Something went wrong!");
}
}
}
启动服务器方法应该包含什么???
您可以尝试以下代码来打开活动本地主机服务器的端口。
Desktop desktop = Desktop.getDesktop();
URI url = new URI("http://localhost:9000/");
desktop.browse(url);
否则,如果您需要访问终端命令,请尝试此操作;但请确保您的播放项目目录路径。现在可以使用它在 java 控制台中运行 sbt 命令。
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class Cmd {
public static void main(String[] args) throws IOException {
Process process = Runtime.getRuntime()
.exec("cmd /c dir", null, new File("C:\\Users\\"));
//.exec("sh -c ls", null, new File("Pathname")); for non-Windows users
printResults(process);
}
private static void printResults(Process process) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
您可以使用
play.Application
构造 play.inject.guice.GuiceApplicationBuilder
实例。
围绕此的播放文档侧重于测试,但它也适用于独立应用程序:
Application application =
new GuiceApplicationBuilder()
.build();
更详细的配置选项可以在播放文档中找到。
GuiceApplicationBuilder javadoc:https://www.playframework.com/documentation/2.8.x/api/java/play/inject/guice/GuiceApplicationBuilder.html
播放文档:https://www.playframework.com/documentation/2.8.x/JavaTestingWithGuice