首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java运行ngrok

如何使用java运行ngrok
EN

Stack Overflow用户
提问于 2018-09-30 15:44:22
回答 2查看 1.9K关注 0票数 1

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

代码语言:javascript
复制
   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();
            }
        }

以下方式输出&& error

代码语言:javascript
复制
   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

错误是这样的:

代码语言:javascript
复制
 ERROR:  Unrecognized command: src/Ngrok/ngrok.exe http 8080
EN

回答 2

Stack Overflow用户

发布于 2021-08-20 23:09:48

一个更简单的解决方案是只使用java-ngrok (公开地说,我是开发人员),它是ngrok的Java包装器。它为您提供了对API、隧道等一切内容的编程访问。它可以在Maven Central (文档是here)上找到,您可以这样使用它:

代码语言:javascript
复制
final NgrokClient ngrokClient = new NgrokClient.Builder().build();
final CreateTunnel createTunnel = new CreateTunnel.Builder()
        .withAddr(8080)
        .build();
// Open a HTTP tunnel on port 8080
// <Tunnel: "http://<public_sub>.ngrok.io" -> "http://localhost:8080">
final Tunnel httpTunnel = ngrokClient.connect(createTunnel);

javadoc.io上的完整文档。

票数 1
EN

Stack Overflow用户

发布于 2019-11-04 03:36:16

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52575763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档