1

I'm a programmer with strong C++ background, but new to Java world. Now I have a question about debugging.

How to debug a pre-compiled Java .class file using Eclipse IDE? And I find it is really not an easy task to find the answer by Googling.

In order for my question to be really precise and no misleading, I expound it as follows.

I have two .java source files below:

$
├── src             
│   └── mypkg1      
│       └── Timeline.java 
└── test                  
    └── mypkg1            
        └── TimelineTest.java

In the root folder($), I can compile and run TimelineTest.java using two commands:

javac -classpath ./src test/mypkg1/TimelineTest.java

java  -classpath ./src:./test mypkg1.TimelineTest       

source tree and java compile and run

Now I have $/test/mypkg1/TimelineTest.class with Java byte code in it.

Then how can I do source-level debugging on the TimelineTest.class?

I tried, but no success yet. I created a Eclipse project as $/test/mypkg1/.project, then create a Debug configuration for it as below. But, when start debugging, I got red error message:

Error: Could not find or load main class TimelineTest

Caused by: java.lang.NoClassDefFoundError: mypkg1/TimelineTest (wrong name: TimelineTest)

Eclipse blame error on starting debugging

Then what's the correct way to do it?

The key requirement here is, I should not be bothered to do verbose Eclipse project settings, mouse clicking here and there hundreds of times. What I want is quick loading the binary and start debugging. You know, it is definitely true in C++ world. For example, using Visual Studio IDE, File → Open Project → Select an EXE file as project-file, that is the way you tell VS IDE to debug that EXE. After the "project" is opened, press F10, the debugger stops the debuggee at first line of main(), just that simple.

How can I achieve the same with Eclipse? Or, some other feasible Java debugger recommended?

== Timeline.java ==

package mypkg1;

public class Timeline {

    private int fetchCount;

    public void setFetchCount(int fetchCount) {
        this.fetchCount = fetchCount;
    }

    public int getFetchCount() {
        return fetchCount;
    }
}

== TimelineTest.java ==

package mypkg1;

public class TimelineTest {

    public static void main(String[] args) {

        Timeline timeline = new Timeline();
        int expected = 5;

        timeline.setFetchCount(expected);
        int actual = timeline.getFetchCount();

        System.out.println("Fetch = "+actual);
    }
}

2 Answers 2

1

You can run your code with remote debugging enabled and attach your Eclipse debugger to that running application.

Here is quite detailed explanation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It is a feasible way. But, the first time I tried it, I encountered so many buggy behaviors. youtu.be/C_CP4jnz5Co
0

You have a few issues here. You don't need to look at remote debugging for this.

You need to edit the project properties (right-click on project name in explorer and select "Properties"). You need to edit the "Java Build Path". In the "Source" tab, you need to have two source directories, "src" and "test".

When you do that, it will discover that you have two source files, one in each folder and package. You can expand the folders to see them, or you could go to the "Navigate" menu and select "Open Type" (and note the keyboard shortcut to get there faster) and enter the name of your class. That will open the class in the code editor.

At that point, assuming that the class has a "main" method with the proper signature, you can right-click in the code editor and select "Debug As", and select "Java Application". You'll probably want to set a breakpoint on the first line of your "main" method before you do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.