Sometimes it’s not possible to debug your code locally in your IDE of choice, sometimes you will need to debug remotely but how do you go about it?
Assuming that you’re using IntelliJ (Eclipse is not that different incidentally) then here’s how;
- Go to Run > Edit Configurations.
- From the menu click + for a new configuration and select ‘Remote’.
- You will be faced with this window;
Change the host to the machine that your Java application is running on.
Change the port to be something that you can access and is not already in use.
Give the debug configuration a memorable name.
Make sure you select the right source for “Search sources using module’s classpath”
Click the copy icon next to the appropriate JVM version. Save the debug args somewhere, we will need them!
- Now you need to change your application so that when it’s started it contains the arguments that IntelliJ gave you. So for example;
java myapp -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
- With your application running hit debug!
A couple of important points;
- If your local source code differs from what’s running on the remote server then you will see the debugger do some strange things, for example going in to what look to be empty lines.
- If you want your application to wait for you to attach a debugger before starting just change “suspend=n” to “suspend=y”.