如何通过java main方法运行或启动play框架项目

b91juud3  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(309)

我启动了一个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!");
        }
    }
}

启动服务器的方法应该来点什么???

rnmwe5a2

rnmwe5a21#

您可以尝试以下代码来打开活动localhost服务器的端口。

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

相关问题