Remote Debugging a Java Process on Heroku
Java processes on Heroku run inside of a dyno, which has a few restrictions that make it difficult to attach debuggers and management consoles. But it’s not impossible.
My Java Debug Wire Protocol (JDWP) Buildpack can be added to your Heroku application with just a few simple commands. It will use ngrok to proxy the debug session on Heroku, making it externally accessible. Or you can run ngrok locally to have your Java process connect to you. I’ll begin with the former.
Connect from your local debugger
First, create a free ngrok account. This is necessary to use TCP with their service. Then capture your API key, and set it as a config var on your Heroku app like this:
Next, add the JDWP buildpack to your app:
Then add your primary buildpack. For example, if you are using Java:
Now modify your Procfile
by prefixing your web
process with the with_jdwp
command. For example:
Finally, commit your changes, and redeploy the app:
Once your app is running with the JDWP buildpack and the with_jdwp
command, you’ll see something like this in your logs:
Then, from your local machine, you can connect to the process using the ngrok URL from the logs. For example:
Now you can use it:
Or just use your favorite IDE. Your favorite is IntelliJ IDEA right?
Connect to your local debugger
If you’d like to have your process connect to your local machine (going the opposite direction) you can install ngrok locally. Then run it
This will display the ngrok URL. Use it to set the following config vars on your Heroku app:
Finally, start your debugger locally:
jdp -listen 9999
And redeploy your app with git push
.