Sunday 19 January 2020

Remote debugging of Java Application with Eclipse


Remote debugging is a two-step process.

Step 1: Run the application in debug mode
Below statement runs application in debug mode and listens at port 8000.

java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n {Application}

Step 2: Connect to the application running on port P.

Run the application in debug mode
ActionEx.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ActionEventEx {
 JTextArea displayArea;
 JFrame frame1;

 ActionEventEx() {

  JButton click = new JButton("Click Me");
  JButton submit = new JButton("Submit");

  /* Initialize Display Area */
  displayArea = new JTextArea();
  displayArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(displayArea);
  scrollPane.setPreferredSize(new Dimension(450, 150));

  /* Initialize Frame */
  frame1 = new JFrame("Sample Frame");
  frame1.pack();
  frame1.setVisible(true);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame1.getContentPane().add(click, BorderLayout.WEST);
  frame1.getContentPane().add(submit, BorderLayout.EAST);
  frame1.getContentPane().add(scrollPane, BorderLayout.CENTER);
  frame1.setSize(800, 300);

  click.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    displayArea.append("\nAction Command is " + e.getActionCommand());

    /* Get the Time Stamp when the Action Occurs */
    long timeStamp = e.getWhen();
    Date day = new Date(Long.parseLong("" + timeStamp));
    displayArea.append("\n time is " + day);

    /* Get the Modifiers */
    displayArea.append("\n Modifiers are " + e.getModifiers());
    displayArea.append("\n Entire param String is" + e.paramString() + "\n");
   }
  });

  submit.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    displayArea.append("\nAction Command is " + e.getActionCommand());

    /* Get the Time Stamp when the Action Occurs */
    long timeStamp = e.getWhen();
    Date day = new Date(Long.parseLong("" + timeStamp));
    displayArea.append("\n time is " + day);

    /* Get the Modifiers */
    displayArea.append("\n Modifiers are " + e.getModifiers());
    displayArea.append("\n Entire param String is" + e.paramString() + "\n");
   }
  });
 }

 public static void main(String args[]) {
  new ActionEventEx();
 }
}

Compile ActionEx.java
javac ActionEventEx.java

Run the application on port 8000.

java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n ActionEventEx


Once you execute above command, application starts listening on dt_socket at address: 8000.
$java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n ActionEventEx
Listening for transport dt_socket at address: 8000


Application launch below gui window.


Step 2: Connect to the application running on port 8000.

Open the source code in Eclipse (You can use any other IDE, procedure is similar).

Right click on the file -> Debug As -> Debug Configurations…


Remote Java Application -> New Configuration.

Give Host as localhost and Port as 8000.

Click on Debug button. You can see that application is connected to the port 8000.


Add debug points in ‘addActionListener’ methods.

Click on any button in the ‘Sample Frame’, you can see that the debug point hits.

Previous                                                    Next                                                    Home

No comments:

Post a Comment