跳至主要内容

Java Remote Debug

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.

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 with jdb – 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 in tomcat/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.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Learn Spring Expression Language

When reading the source code of some Spring based projects, we can see some code like following: @Value( "${env}" ) private int value ; and like following: @Autowired public void configure (MovieFinder movieFinder, @ Value ("#{ systemProperties[ 'user.region' ] } ") String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } In this way, we can inject values from different sources very conveniently, and this is the features of Spring EL. What is Spring EL? How to use this handy feature to assist our developments? Today, we are going to learn some basics of Spring EL. Features The full name of Spring EL is Spring Expression Language, which exists in form of Java string and evaluated by Spring. It supports many syntax, from simple property access to complex safe navigation – method invocation when object is not null. And the following is the feature list from Spring EL document :