13

Debugging my code with Java's jdb. Am stuck at a point where my program expects command-line input, but jdb intercepts it as a jdb command.

How do I tell jdb to pass through text to the running program?

Version:

C:\Documents and Settings\*snip*>java -showversion
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)

Compile:

javac -g LZWDecompress.java

jdb.ini:

stop in LZWDecompress.decompress
run
monitor list

This is what happens:

Initializing jdb ...
*** Reading commands from C:\Documents and Settings\*snip*\jdb.ini
Deferring breakpoint LZWDecompress.decompress.
It will be set after the class is loaded.
> run LZWDecompress
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
> > >
VM Started: Set deferred breakpoint LZWDecompress.decompress
File to be decompressed: 

At the prompt above, I enter "test", and receive this response:

...
VM Started: Set deferred breakpoint LZWDecompress.decompress
File to be decompressed: test
Unrecognized command: 'test'.  Try help...
>

Here is the code from function main(...), written by our instructor, not me:

public static void main(String[] args) throws IOException {    
    short [] source; 
    int dlen; 
    int sz;
    byte [] decompressed;
    BufferedReader br;
    DataInputStream In;
    FileInputStream FI;
    FileOutputStream FO;
    InputStreamReader isr;
    String cfnm;
    String sfnm;

    source = new short[INPUT_FILE_IO_BUFFER_SIZE];
    decompressed = new byte[OUTPUT_FILE_IO_BUFFER_SIZE];

    isr = new InputStreamReader(System.in);
    br = new BufferedReader(isr);
    System.out.print("File to be decompressed: ");
    System.out.flush(); 
    sfnm = br.readLine();
    System.out.print("Name of the decompressed file: ");
    System.out.flush(); 
    cfnm = br.readLine();
    FI = new FileInputStream(sfnm);
    In = new DataInputStream(FI);
    FO = new FileOutputStream(cfnm);
    for (sz=0; true; ++sz) {
        try { source[sz] = In.readShort();
        } catch (EOFException e) { break;
        } catch (Exception e) { System.out.print(e.toString()); 
        }
    }
    dlen = decompress(source, sz, decompressed);
    FO.write(decompressed, 0, dlen);
    FO.close(); 
}  // end main()
TofuBeer
  • 60,850
  • 18
  • 118
  • 163
iokevins
  • 1,427
  • 2
  • 19
  • 29
  • Hey TofuBeer, thanks for the (edit) clarification. When you say "command line is not the same as console!" do you mean to say when the application is started, I am interacting with the console instead of the command line? I have not thought about this distinction before, so I appreciate you bringing it up. Thanks! :D – iokevins Dec 07 '09 at 23:35

1 Answers1

9

Hmmm...this post seems to indicate I need to run the program and then attach the debugger to it. For example:

% java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n LZWDecompress

% jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8000

Not immediately obvious how I get the program to stop after passing by the user input functions.

Is this the preferred way to do this?

Update: attach and configure jdb (for example, breakpoints) once the running program prompts for user input. Once jdb is configured, provide input to the running program. jdb then takes over program execution in the second window. :D

iokevins
  • 1,427
  • 2
  • 19
  • 29
  • 2
    Yes, this is the way -- but issue your second command in another terminal window/process, not the same one in which you ran your Java program. This lets you have one terminal process that has stdin/stdout devoted to your Java program, and another terminal process with stdin/stdout devoted to jdb. – delfuego Dec 05 '09 at 22:27
  • 1
    Thank you, I was looking for this and your post pointed me in the right direction :) -- Just adding in 2022 for future visitors that the syntax for this has changed. Consult the jdb manual for the new syntax (e.g. `java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n MyClass` and then `jdb -attach 12345`, assuming 12345 was the port reported on the terminal by the jvm before proceeding with execution of MyClass). I note that at the time of writing this, the linked post is updated to reflect this change. – Tasos Papastylianou Dec 21 '22 at 21:18