9

I have a java project in which the file system is as follows: I have 3 directories: bin, src, and lib.

  • src contains my *.java files
  • bin contains my *.class files (compiled using the files in src)
  • lib contains a few *.jar files imported by most of the src files

I am learning how to use jdb but every time I try and use the list command it just says that the source file cannot be found. I am running the following command from within my bin directory:

jdb -classpath ../lib/*:. -sourcepath ../src envelope.Envelope

where my main method is contained within the Envelope class which is part of the envelope package, what am I doing wrong?

tk421
  • 5,775
  • 6
  • 23
  • 34
brnby
  • 1,433
  • 1
  • 19
  • 35

3 Answers3

7

I know that this one is very old, but maybe someone will be still interested in it

Let's say we have file

package mypackage;

public class Main {
  public static void main(String [] arg) {
    System.out.println("Hello world");
  }
}

and layout of the project follows

jdb_test/
├── src
│   └── mypackage
│       └── Main.java
└── target
    └── mypackage
        └── Main.class

class file was compiled using:

javac -sourcepath src -d target src/mypackage/Main.java
# if you have multiple files, you can always do
find . -name "*.java" -exec javac -sourcepath src -d target {} \;

Then, being inside jdb_test directory, we can call

jdb -sourcepath src -classpath target mypackage.Main

and debug the code

Initializing jdb ...
> stop in mypackage.Main.main
Deferring breakpoint mypackage.Main.main.
It will be set after the class is loaded.
> run
run mypackage.Main
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint mypackage.Main.main

Breakpoint hit: "thread=main", mypackage.Main.main(), line=5 bci=0
5        System.out.println("Hello world");

main[1] list
1    package mypackage;
2
3    public class Main {
4      public static void main(String [] arg) {
5 =>     System.out.println("Hello world");
6      }
7    }
main[1]
Oo.oO
  • 12,464
  • 3
  • 23
  • 45
1

I ran into same issue as SO, and in addition to the provided solution, I want to point out, you need to execute JDB from correct directory. If your class belongs to any package, e.g. class MainActivity belongs to package org.gradle.samples, then your project folder contains the following directory:

yourProject/
├── src
│   └── main
│       └── java
│           └── org
│               └── gradle
│                   └── samples
│                       └── MainActivity.java

Then you have to execute jdb from <pathToYourProject>/src/main/java, where JDB -sourcepath is <pathToYourProject>/src/main/java as well.

The list command will not work if you execute JDB from a different location, even if you have defined the correct -sourcepath. Or even if you change the the source path within the JDB console via use command.

Hölderlin
  • 424
  • 1
  • 3
  • 16
0

There is an on-the-fly alternate way.

"sourcepath" is also a command within jdb itself. So let's say you've already attached/set breakpoint/run, and are getting this error and don't want to start over.
Just type sourcepath /path/to/srcdir and it should be happy from there on.

Daemon42
  • 187
  • 2
  • 8