如何使用Java运行ngrok

时间:2018-09-30 07:44:22

标签: java ngrok

我写了一个Java程序,用http 8080运行ngrok。成功执行ngrok.exe后出现跟随错误。 如何获取特定输出并在控制台中打印而不是整个输出?

   String[] command = {
        "src/Ngrok/ngrok.exe",
        "src/Ngrok/ngrok.exe http 8080"
    };

    String line;
    Process p;
    BufferedReader input = null;

    try {
        p = Runtime.getRuntime().exec(command);

        input = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            System.out.println(line);
            logsStatus.replaceSelection(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != input) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

以以下方式输出&&错误

   NAME:
   ngrok - tunnel local ports to public URLs and inspect traffic

COMMANDS:
   authtoken    save authtoken to configuration file
   credits  prints author and licensing information
   http     start an HTTP tunnel
   start    start tunnels by name from the configuration file
   tcp      start a TCP tunnel
   tls      start a TLS tunnel
   update   update ngrok to the latest version
   version  print the version string
   help     Shows a list of commands or help for one command

这是错误:

 ERROR:  Unrecognized command: src/Ngrok/ngrok.exe http 8080

1 个答案:

答案 0 :(得分:0)

这不起作用,因为命令ngrok http <port number>打开一个新终端作为输出。 我遇到了同样的问题,但是在网上搜索后,我发现此ngrok命令curl -s localhost:4040/api/tunnels返回了一个JSON对象,该对象定义了您想要的所有信息,包括公共URL。您在ngrok http之后运行此命令,并执行相同的方法。