debugging java application deployed in tomcat - java

I have an application that I deployed in tomcat. Later I configured the code as a project in Eclipse. I want to connect to the tomcat via eclipse and debug the application.
Trying to setup a remote debug connection is throwing up errors. Is there any entry that I need to add somewhere in tomcat?

First, you need to run Tomcat in debugging mode. The easiest way to do that is to modify the startup file (.bat or .sh depending if you are Windows or not). Find the line near or at the end of the file that contains the start command, and change it to jpda start. This will cause Tomcat to start in debugging mode listening on port 8000.
Next, to connect to this process via Eclipse, select the Run menu and choose Debug Configurations.... In the pane on the left, choose Remote Java Application and click the New launch configuration button above the list. The important settings to note are the Host and Port fields. The host value must match the hostname of the Tomcat process, and the port should be 8000. You also might need to add entries on the Source tab to ensure that all of the source code is available to step through.

Create a new tomcat server
Create a new dynamic web project
Export war to tomcat server
Tomcat debug configuration(important), first double click tomcat server, then select "Use Tomcat installation (takes control of Tomcat installation)"
Add breakpoints
Run tomcat in debug mode
Test war in web browser
Edit source path
Debug source code in Tomcat
The war file is available here.

yes, you need to configure tomcat to accept remote debugging connections. There is a FAQ on how to do it with various IDEs.
http://wiki.apache.org/tomcat/FAQ/Developing#Q2

Simply change the line in startup.bat (if you are using Windows)
from:
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
to:
call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%

Right Click on the project you want to debug and go to Debug As Menu and in the list click on Debug Configurations.
Then Double click on the Remote Java Application from left menu list. browse for the project u wanna add in then click apply.
Also change the listening port to 7999, then after you started the web app from your localhost click the debug button on the same window. Then any break point's will be hit after that.

Steps to setup the tomcat Debugger with eclipse
Install the following things:
java 1.8+
eclipse EE version
download tomcat 8+
open eclipse and go to servers tab and add a new server(window-> show View-> Others -> Search Servers)
Mark your project as Dynamic web Project(Rightclick on Project-> project facets -> select Dynamic web module)
Export war (File-> Export-> search war -> select module and choose the path)
Run as -> Run on Server
Start the tomcat Server in Debug or Run Mode
check on localhost:8080

Debug worked for me. I was using tomcat 8, configured on centos 7. Debug port opened for me after changing 2 settings.
Open tomcat/conf/bin/startup.sh and added below lines
export JPDA_ADDRESS=8000
export JPDA_TRANSPORT=dt_socket
In the same file startup.sh, added jpda before start command.
exec "$PRGDIR"/"$EXECUTABLE" jpda start "$#"
Restart the tomcat.
In eclipse go to Run>Debug configuration>Remote java application>Add host ip and port(8000) where tomcat is running.
In my case i required to open 8000 through firewall too.
firewall-cmd --zone=public --permanent --add-port=8000/tcp
firewall-cmd --reload

Beside remote debugging, you can debug your app locally within eclipse if you added tomcat in eclipse.
just start server and wright click your app and from debug menu hit debug on server.
If you have multi module maven project, debug on server just shown up for module with war packaging.
If you want debug a class that runs at startup by self, like methods that annotated with #PostConstruct, remember that you should increase server timeout.
To do that, go to server view and double click on tomcat and in server preview tab, increase the timeout value.

Related

Netbeans Debugging Java web application (without main)

How can I use the Netbeans visual debugger in a Java Web Application ? I have no main method in my project.
Is it possible?
I'm doing the following steps:
Start Apache Tomcat in Debug Mode.
Right click on Project -> Debug
Web Application is correctly launched. But execution doesn't stop and wait on the selected breakpoints.
I created setenv.bat file in tomcat\bin folder with these commands:
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_92
set JRE_HOME=C:\Program Files (x86)\Java\jre1.8.0_92
CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
JPDA_OPTS="-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n"
In Attach Debugger I specified SocketAttach, dt_socket, Host=localhost, Port=8000

Debug remote java application using Intellij

I am trying to debug remote java application in Intellij but unable to get sucess, The steps which I am doing is
Run>Edit Configuration> Defaults> Remote
What is/are I am missing?
First, create a new Remote debugging configuration:
ALT + U + R
Take note of the port which IntelliJ plans to listen in Tomcat. On my setup the port is 8000, which is the default and this should be fine for your setup as well. To change the Tomcat JPDA port, edit catalina.bat (or whatever your startup script is) and find a line which looks like the following:
set JPDA_ADDRESS=8000
Second, make sure that you are building your project with javac -g .... The -g option means that debugging information will be turned on in your Spring WAR.
Finally, when you start Tomcat, use catalina jpda start, which will tell Tomcat to connect the debugger to port 8000. When you want to connect to IntelliJ then use ALT + SHIFT + F9 and select your remote configuration from the menu. Of course, you should add some breakpoints, without which it doesn't make much sense to be in debug mode.
You don't need to edit the defaults. You need to use the [+] button to create a new Remote run configuration and specify its settings.

Intellij IDEA remotely debug java console program

I have a remote server with Java running to which i have SSH access.
I am writing an app on the local machine, building it with maven.
Is it possible to set the IDE Itellij IDEA to run my project remotely? The idea is : to build the jar, copy it to the server, and debug the process ( or something like that).
It would be nice of you to share the settings i need to setup.
This is what I usually do to debug my remote app.
Run the server on debug mode
This can be adding this particular line when you run your application server
-Xdebug -Xrunjdwp:transport=dt_socket,server=n,suspend=n,address=9999
for JDK above 1.4, you can use this
-agentlib:jdwp=transport=dt_socket,server=n,suspend=n,address=9999
After that, run your application server
SSH Tunneling
I'm not 100% sure that you can access to your application's port directly if you're using ssh connection (well, maybe there is a way ;) ). So, first we need to expose the port for debugging that we set on first step by running this command.
ssh -f user#personal-server.com -L 9999:personal-server.com:9999 -N
Setting up the IDE
You can follow the step that #SSJVegito has said, which basically, is to point the debugger to the port 9999. Open the debug configuration in your Idea, then Change the circled value to 9999. Then, happy debugging :D
I think it's possible, I was able to do such a thing with a local server I was using to deploy my application. And since you have access to the server, it should work for you as well.
What you need to do in IntelliJ is create a Remote configuration. To do that, open IntelliJ and next to the run button (on the left), you should have your configurations, designated by a down arrow. Click that arrow and click Edit Configurations. A new window should open. Click the + sign in the upper left corner and the select Remote. A new window should appear. We now need to set the host and the port which the server uses for debugging (if you are using tomcat, it is usually 8000; in tomcat, you can locate it by opening the catalina.bat file with a text editor and looking for the JPDA_ADDRESS property, which allows you to change the port). Give your configuration a name and press Apply.
Afterwards, you need to run your configuration in Debug mode. You need to select it from the configuration list and press the Debug button located to the right of the Run button.
More details here:
http://blog.trifork.com/2014/07/14/how-to-remotely-debug-application-running-on-tomcat-from-within-intellij-idea/comment-page-1/
http://eclipse.org/jetty/documentation/current/debugging-with-intellij.html
http://www.javaranch.com/journal/200408/DebuggingServer-sideCode.html
I hope this helps.

Tomcat v7.0 Server setup for ports 8086 and 8009

I am new in java programming. I have write a small web program using struts. I ve already install apche tomcat 7 server. I configure this server to my project. Tomcat server error showing When I run this program. The error is
Starting tomcat v7.0 server at localhost encountered a problem
Several ports (8086, 8009) required by Tomcat v7.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s)
You need to find out which process are running over that port and if it is not needed you can kill them
or you can change your tomcat's configuration to start the Tomcat instance on another port
for this you need to edit your server.xml file located at TOMCAT_DIR/conf/
how to solve this Port 8080 required by Tomcat v7.0 Server at localhost is already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).
You can change your tomcat ports
you can change the port details in {TOMCAT_HOME}\conf\server.xml
You've another instance of Server already running. You can confirm this by going to http://localhost:8080 in your webbrowser and check if you get the specific 404 error page and Tomcat default home page. Both are equally valid evidence that Tomcat runs fine.
You need to shutdown it.
Open the task manager and kill all java and/or javaw processes.
Now open your project and run your application.
Open Window -> Preferences -> Server
If Tomcat is not installed go through these steps otherwise skip step
In “Servers” area, right click -> New -> Server.
You will find Tomcat vx.x Server under “Apache” folder as shown
below.
Select “Tomcat v6.0 Server” and click Next.
Select the Tomcat Root folder which has bin folder and click Next.
There are no resources to be configured and hence click Finish.
The configured Apache Tomcat Server will be displayed in the
“Servers” view.
If Tomcat is already installed then go through these steps:
In "Server" area, right click -> Add and Remove...
Under this, the project need to be configured add the project from
"Available:" box to "Configured:"
Click finish to run the server.

How to debug a JSP tomcat service using eclipse?

I would like to debug my separately running JSP/Struts/Tomcat/Hibernate application stack using the Eclipse IDE debugger. How do I setup the java JVM and eclipse so that I can set breakpoints, monitor variable values, and see the code that is currently executing?
I just Googled it. :)
http://bugs.sakaiproject.org/confluence/display/BOOT/Setting+Up+Tomcat+For+Remote+Debugging
Many more on google.
Effectively, set your JPDA settings:
set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket
bin/catalina.bat jpda start
Then, in Eclipse, Run->Debug Configurations...->Remote Applications.
Follow these steps:
Add the following arguments to the java command that is used to launch Tomcat (on Windows, I think this is in TOMCAT\bin\catalina.bat)
-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n
In Eclipse, create a 'Remote Java Application' debug configuration and set the port to 8787 and the host to the name (or IP address) of the machine where Tomcat is running. If Tomcat is running on the same machine as Eclipse, use 'localhost'.
In the 'source' tab of the debug configuration, add any projects that you want to debug into
Start Tomcat
Launch the debug configuration you created in step 2
Eclipse should now stop at any breakpoints that you've set in the projects you added in step 3.
Notes:
You can change the port to any other available port if for some reason you can't use 8787
If you want Tomcat to wait for the remote debugger to start, use 'suspend=n' in the command above to 'suspend=y'
You could do what they suggest, or use this Eclipse plugin, which makes it easier to configure Tomcat to begin with: Eclipse Tomcat Plugin
When launching tomcat via this plugin, it starts in debug mode by default, you must explicitly disable debugging mode if you want it to not allow Eclipse to connect a remote debugger.
For Tomcat 5.5 on Windows:
Edit bin/startup.bat
Find the line that reads:
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
Replace it with these lines:
set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket
call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%

Categories