[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

remote debugging in eclipse

Posted on 2014-08-01
24
Medium Priority
?
314 Views
Last Modified: 2014-08-28
Hi,

I am going through below link
http://javarevisited.blogspot.com/2011/02/how-to-setup-remote-debugging-in.html

I would like to try the remote debugging on eclipse i have on my laptop. How do i start and proceed. Please advise
0
Comment
Question by:gudii9
  • 14
  • 6
  • 4
24 Comments
 
LVL 21

Assisted Solution

by:Amitkumar P
Amitkumar P earned 800 total points
ID: 40236063
You will need to run your program outside eclipse. You can do the following:

1. create a simple program, compile it and then execute it from command prompt and add below params while executing the command.

Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y  suspend=y

So for example your program name is Test.java
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y  suspend=y Test

2. Keep the program running in command prompt and follow the steps to setup remote debugging in Eclipse mentioned in that web site.

3. Open that program (Test.java) file and set the breakpoint. You will see whenever control goes to that line, debugger will stop and you can then proceed further.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40242362
3. Open that program (Test.java) file and set the breakpoint. You will see whenever control goes to that line, debugger will stop and you can then proceed further.

When i put  breakpoint eclipse automatically stops control at the break point right. How above point 3 is related with below point number 1, 2. please advise

1. create a simple program, compile it and then execute it from command prompt and add below params while executing the command.

Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y  suspend=y

So for example your program name is Test.java
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y  suspend=y Test

2. Keep the program running in command prompt and follow the steps to setup remote debugging in Eclipse mentioned in that web site.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40242869
when i tried as attached getting error. Is the example is working fine for you.
please advise
RemoteDebug.docx
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:Amitkumar P
ID: 40245321
Probably, your program is completing its execution before you launch the debugger. I will get you an example.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40245327
ok. sure
0
 
LVL 21

Expert Comment

by:Amitkumar P
ID: 40245348
Just remove suspend=y from the command and run it.

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y   Test

Attaching the document for your reference.
RemoteDebugWorking.docx
0
 
LVL 7

Author Comment

by:gudii9
ID: 40247902
My java file is as below

package com.mycompany.app;

/**
 * Hello world!
 *
 */
public class App {

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
       
        int count = 0;
        while (true) {
              count++;
              
              if (count%1000==0) {
                System.out.println( "Count :: " + count);
              }
        }
    }

}




The folder path where App.java residing is
C:\gpprojects\sample-project\src\main\java\com\mycompany\app


I was able to compile as below

C:\gpprojects\sample-project\src\main\java\com\mycompany\app>javac App.java

C:\gpprojects\sample-project\src\main\java\com\mycompany\app>


But i was not able to run



C:\gpprojects\sample-project\src\main\java\com\mycompany\app>java App
Exception in thread "main" java.lang.NoClassDefFoundError: App (wrong name: com/
mycompany/app/App)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

C:\gpprojects\sample-project\src\main\java\com\mycompany\app>


I tried below command
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y com.mycompany.app.App

that also did not work.

Please advise
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40247905
But i was not able to run

Do...
cd C:\gpprojects\sample-project\src\main\java

Open in new window

and then try either of the following...
java com.mycompany.app.App
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y com.mycompany.app.App

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40247908
i wonder why javac command worked but not java command. I do see the .class file (C:\gpprojects\sample-project\src\main\java\com\mycompany\App.class)in that path created also
0
 
LVL 7

Author Comment

by:gudii9
ID: 40247914
It worked now as you mentioned when i ran from java folder



Count :: 403349000
Count :: 403350000
Count :: 403351000
C:\gpprojects\sample-project\src\main\java>java -Xdebug -Xrunjdwp:transport=dt_s
ocket,address=8001,server=y com.mycompany.app.App


When i try to launch remote debuggin from eclipse getting error as below as attached
Failed to connect to remote VM. Connection refused.
Connection refused: connect


How to fix it. please advise
RemoteDebug0807.docx
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40247967
Ok, so after you run the following command...

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y com.mycompany.app.App

You should see the following message...
Listening for transport dt_socket at address: 8001

Open in new window

And the program won't be counting yet, it is just sitting there waiting for you to connect.

And then you goto Eclipse and launch the Remote Debug session using the parameters that you have shown above, and the program will start running (at least up to the first breakpoint if you have any set). But then you can do all normal debugging operations.
0
 
LVL 21

Expert Comment

by:Amitkumar P
ID: 40247972
Agree with mccarl.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40250196
when i ran java command on the command prompt by CDing to
C:\gpprojects\sample-project\src\main\java\com\mycompany\app
I see
Listening for transport dt_socket at address: 8001

I got above line in the command prompt.

Now on eclispe side i am having the issue

And then you goto Eclipse and launch the Remote Debug session using the parameters that you have shown above, and the program will start running

are my parameters
Host:localhost
Port:8001
are correct?
what is 8001 means. it should match with command prompt  port (ie Listening for transport dt_socket at address: 8001). For example if command prompt says
Listening for transport dt_socket at address: 8009
then i should givve in my eclipse parameters as below right

Host:localhost
Port:8009

Is that understanding is correct?
(at least up to the first breakpoint if you have any set). But then you can do all normal debugging operations.
since i have put the same program App.java (C:\Users\ganesha\kepler_workspace\junit workspace\JMock Example\src\eePackage\App.java) in eclipse when i click debug does it trigger App.java in eclipse (ie from path C:\Users\ganesha\kepler_workspace\junit workspace\JMock Example\src\eePackage\App.java) or from path C:\gpprojects\sample-project\src\main\java\com\mycompany\app\App.java (the one i used above in command prompt scenario?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40250198
what it means by error message--->failed to connect to remote VM. connection refused?

Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40250209
i see

Listening for transport dt_socket at address: 8001

in my command prompt after that java command(what is meaning of those options wwith java command?
)

Now when i click debug it triggered above command prompt remote debug which is sitting and waiting.

That all make sense to me now.

My other question is i put break point at line 11 but my program never stopped counting numbers on the command prompt as attached. How do i stop the control at line 11. please advise
brkPoint.jpg
0
 
LVL 7

Author Comment

by:gudii9
ID: 40250225
other attachmet
queueAdmin2.jpg
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40252238
My other question is i put break point at line 11 but my program never stopped counting numbers on the command prompt as attached. How do i stop the control at line 11. please advise
I can only assume that it is because you are using different code in Eclipse compared to what you are running on the command line and trying to debug. eg... on the command line you are starting "com.mycompany.app.App" from a project called "sample-project" but in Eclipse you seem to be referring to a project called "RemoteDebug" (and I can't see exactly what code is in that project). You need the exact same code for both the remote program that you are trying to debug and the Eclipse debug session that you are launching.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40252286
I see both files code same not sure where is the issue

\gpprojects\sample-project\src\main\java\com\mycompany\app code is as below


package com.mycompany.app;

/**
 * Hello world!
 *
 */
public class App {

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        
        int count = 0;
        while (true) {
        	count++;
        	
        	if (count%1000==0) {
                System.out.println( "Count :: " + count);
        	}
        }
    }

}



elipse APP.java is as below(C:\Users\ganesha\kepler_workspace\junit workspace\JMock Example\src\eePackage\App.java)

package eePackage;

/**
 * Hello world!
 *
 */
public class App {

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        
        int count = 0;
        while (true) {
        	count++;
        	
        	if (count%1000==0) {
                System.out.println( "Count :: " + count);
        	}
        }
    }

}

Open in new window

0
 
LVL 36

Expert Comment

by:mccarl
ID: 40252293
The code that you posted above mentions "JMock Example" as the project name but in your screenshots for the Eclipse launch configuration it mentions "RemoteDebug" as the project name.

Also, even the two pieces of code that you posted above are *different*. Yes they are close but they are not EXACTLY the same. Look at the very first line of each file, the package is different.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40252312
The code that you posted above mentions "JMock Example" as the project name

File locations are different. One is inside eclipse workspace (C:\Users\ganesha\kepler_workspace\junit workspace\JMock Example\src\eePackage\App.java) other one which outside eclipse workspace(C:\\gpprojects\sample-project\src\main\java\com\mycompany\app\App.java ---> one i ran from command prompt). Are both supposed to be in the same location?.

I mean App.java i refer from command prompt and eclipse should be the same?

Please advise
0
 
LVL 36

Expert Comment

by:mccarl
ID: 40252320
They don't necessarily need to be the exact same location (because obviously this would often be difficult when doing remote debugging for real, ie. when not just connecting to 'localhost') BUT they do need to at least be the same code. As I've been trying to tell you a couple of times now...

com.mycompany.app.App DOES NOT EQUAL eePackage.App

Yes, the class names are the same but the package is part of the fully qualified class name, and the fact that they are different makes the two classes different, even if they have the same content, they are still different.

One normal way to approach remote debugging is... you develop code in Eclipse, compile it in Eclipse, and then move the class files that are generated (and any libraries required) to your remote machine in someway (one way would be to generate a JAR file). Then you would run the code on that remote machine (like instructed above) and then have Eclipse connect to it to remotely debug it. But the point here is that it is 100% the same code that you are attempting to run as you are debugging with.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40252361
Oh that makes more sense.

How i know where my eclipse is putting App.class(generated from C:\Users\ganesha\kepler_workspace\junit workspace\JMock Example\src\eePackage\App.java )
So that i can put in some remote location and run from command prompt.

Then When i put a break point in eclipse it should stop control in the command prompt i guess right?
Please advse
0
 
LVL 36

Accepted Solution

by:
mccarl earned 1200 total points
ID: 40252375
How i know where my eclipse is putting App.class
If you right-click on your Project within Eclipse, and then select "Build Path  >" and then "Configure Build Path..." to bring up the Project's Build Path properties. Then click on the "Source" tab and down the bottom it should tell you what the "Output Folder" is.

For your above example, I will guess that it will still be the default and so it should be... "C:\Users\ganesha\kepler_workspace\junit workspace\JMock Example\bin". So theoretically, if you want to test this locally (like you were doing above) before trying to do it truly remotely, you could go to a command prompt and do...
cd "C:\Users\ganesha\kepler_workspace\junit workspace\JMock Example\bin"
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y eePackage.App

Open in new window

This will start the App exactly as it was compiled by Eclipse, in debug mode, ready to connect to it from Eclipse's remote debug option.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40253792
Oh. I will try and let you know
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question