DEV Community

Alex Laird
Alex Laird

Posted on • Updated on

java-ngrok - a Java wrapper for ngrok

java-ngrok - a Java wrapper for ngrok

java-ngrok is a Java wrapper for ngrok that manages its own binary, making ngrok available via a convenient Java

ngrok is a reverse proxy tool that opens secure tunnels from public URLs to localhost, perfect for exposing local web servers, building webhook integrations, enabling SSH access, testing chatbots, demoing from your own machine, and more, and its made even more powerful with native Java integration through java-ngrok.


java-ngrok is available on Maven Central.


Enter fullscreen mode Exit fullscreen mode


implementation "com.github.alexdlaird:java-ngrok:1.5.5"
Enter fullscreen mode Exit fullscreen mode

If we want ngrok to be available from the command line, pyngrok can be
installed using pip to manage that for us.

Basic Usage

All ngrok functionality is available through the NgrokClient. To open a tunnel, use the connect method, which returns a Tunnel, and this returned object has a reference to the public URL generated by ngrok, which can be retrieved with getPublicUrl().

final NgrokClient ngrokClient = new NgrokClient.Builder().build();

// Open a HTTP tunnel on the default port 80
// <Tunnel: "http://<public_sub>" -> "http://localhost:80">
final Tunnel httpTunnel = ngrokClient.connect();

// Open a SSH tunnel
// <Tunnel: "tcp://" -> "localhost:22">
final CreateTunnel sshCreateTunnel = new CreateTunnel.Builder()
final Tunnel sshTunnel = ngrokClient.connect(sshCreateTunnel);
Enter fullscreen mode Exit fullscreen mode

The connect method can also take a CreateTunnel (which can be built through its Builder) that allows us to pass additional properties that are supported by ngrok.

Assuming we have also installed pyngrok, all features of ngrok are available on the command line.

ngrok http 80
Enter fullscreen mode Exit fullscreen mode

For details on how to fully leverage ngrok from the command line, see ngrok's official documentation.


For more advanced usage, java-ngrok's official documentation is available at

Java 8

Java 8 support is not actively maintained, but a compatible build of this project does exist for Java 8. To use it,
include the java8-ngrok dependency from Maven Central instead.

Enter fullscreen mode Exit fullscreen mode

The Process API was introduced in Java 9, so certain convenience methods around managing the ngrok process (for instance, tearing it down) are not available in the Java 8 build.


If you would like to get involved, be sure to review
the Contribution Guide.

Want to contribute financially? If you've found java-ngrok useful, a donation
would also be greatly appreciated!

Discussion (1)