Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

remote debugging in eclipse

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
SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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.
Avatar of gudii9

ASKER

when i tried as attached getting error. Is the example is working fine for you.
please advise
RemoteDebug.docx
Probably, your program is completing its execution before you launch the debugger. I will get you an example.
Avatar of gudii9

ASKER

ok. sure
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
Avatar of gudii9

ASKER

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
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

Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
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.
Agree with mccarl.
Avatar of gudii9

ASKER

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?
Avatar of gudii9

ASKER

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

Please advise
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

other attachmet
queueAdmin2.jpg
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.
Avatar of gudii9

ASKER

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

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.
Avatar of gudii9

ASKER

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
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.
Avatar of gudii9

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

Oh. I will try and let you know