When we finish the development and test in local environment, we will deploy our applications in remote server. We hope and believe it will work but we are always too optimistic. The application met different kinds of problems.
In order to fix it, we sometimes need to access code directly in remote server container. Today, we will see how to do debug remotely.
So, we only cover the content of using IDE to debug here. If you are interested in using
Now, we can create a new run configuration in IDE (in our case, it is idea):
Input the host and port (remember to match our early configured port) and we are done. Now we can set a breakpoints and start debug session. Server application will suspends when it runs to our breakpoints. And because the help of IDE, which will search the source against current project, we can use source to debug like we are running it in IDE.
In this case, we can change the debug mode from
An example of config to catch remote application at startup can be:
In order to fix it, we sometimes need to access code directly in remote server container. Today, we will see how to do debug remotely.
Remote Debug
When we develop our application in local, we always using IDE to do it. We code and run and debug in IDE which provides much convenience. Even though we can debug application withjdb
– a command line tools bundled into JDK, we may prefer to use IDE.So, we only cover the content of using IDE to debug here. If you are interested in using
jdb
, you may like this jdb tutorial.Runnable Jar
In order to debug a remote JVM, we have to add some parameters to let JVM opening the debug port so that we can attach to it. The following is the command to start a runnable Jar :java "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" -jar app.jar
As far as I have tested, the order of command line parameter is very important, put -jar
before the debug parameter fails the command.Now, we can create a new run configuration in IDE (in our case, it is idea):
IntelliJ IDEA Remote Application Configuration |
Input the host and port (remember to match our early configured port) and we are done. Now we can set a breakpoints and start debug session. Server application will suspends when it runs to our breakpoints. And because the help of IDE, which will search the source against current project, we can use source to debug like we are running it in IDE.
Bootstrap Debug
This is very useful techniques when we find our application can run in IDE, but can’t start up when it is packaged into a Jar file (which is often the problem of How to refer file in Jar). We cannot attach to a remote application using default configuration (attach
debugger mode) if this application fails to bootstrap, because when we right click the debug button, the application may already down. And in this way, we can’t see the console output, which is also inconvenient to see stack trace. In this case, we can change the debug mode from
attach
to listen
: -agentlib:jdwp=transport=dt_socket,server=n,address=slave01:5005,suspend=y,onthrow=<FQ exception class name>,onuncaught=<y/n>
We can refer to this oracle help site about how to set last parameter.An example of config to catch remote application at startup can be:
-agentlib:jdwp=transport=dt_socket,server=n,address=slave01:5005,suspend=y,onuncaught=n
This way works, but a little bit tedious, we can use IDEA’s another run configuration if we can run this Jar locally, which make life easier:IntelliJ IDEA Jar Application Configuration |
Tomcat
If our application is not a runnable Jar but run in application server like Tomcat. We can also do remote debug. First, we also need to add some environment parameter intomcat/bin/setenv.sh
(or setenv.bat
for windows)JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
Then, we need to run tomcat in command line like following:catalina.sh jpda start
Finally, we create a remote tomcat startup configuration in idea, adding host and port we have specified and we are done:IntelliJ IDEA Tomcat Remote Configuration |
Ref
Written with StackEdit.
评论
发表评论