5

I'm a bit confused by JDB. Being fairly good at using GDB, I'm aware of the various simple commands used to control the execution, i.e. start, run, cont, step, stepi, break, etc. but there are a number of (in my opinion) unusual differences.

If I wish to start an application (as opposed to running it, so I can step it from the start), instinctively, I type jdb MyApp and then start. I get an unrecognized command error. Reading the JDB help gives me no indication at all of a start command or its equivalent. So, I try step - that doesn't work either; I am told that I need to start the Java VM with the run command!

Clearly, it must be possible, because there is a step command explained in the help. So, I figure that maybe I can type run and very quickly press ctrl+c to interrupt it as, in GDB, this stops the execution. Instead, it just quits JDB.

I've tried googling this, to no avail. I hope I'm not being totally blind stupid!

Rant over: how on earth do you just start running a java application in JDB so that you can step it, from the start?

Doddy
  • 1,311
  • 1
  • 17
  • 31

4 Answers4

6

I have just started to use JDB as a debugger. There are good instructions that can be found here. To answer some of your questions:

  1. To start jdb, at the command prompt, type jdb <name of main class>
  2. To start the program, type run
  3. To set a breakpoint, type: stop at <class>:<line no.>
  4. To step, type cont

As I said, I have just started to use jdb just now. There are, I am sure, better ways to do the above and I will post here should I get to know more myself.

HTH.

Sriram
  • 10,298
  • 21
  • 83
  • 136
  • on the CTRL-C: it looks to me like after starting the application with `run`, input to jdb is still active, i.e. just pressing enter should give a new jdb prompt and one can use the `suspend` command to suspend all threads – Andre Holzner Dec 03 '13 at 08:34
3

The problem is the VM hasn't launched, which is why you can't step into the first line. The solution is to start JDB with "-launch" as a command line argument. From there you can type "step" or "cont" to start debugging from the start.

The best bit is it only takes a second and doesn't require installing an IDE :-)

Mike B
  • 1,600
  • 1
  • 12
  • 8
0

I am reading this material as a never-ever user of Java, but a fairly accomplished user of gdb for c. I have had many, many occasions to need to step into the first executable line of source - so commonly that I almost always use the following sequence to start gdb.

Before issuing the "run" command, set a breakpoint at "main". Then "run" will load the program, but will break before executing the first step.

This may be totally inapplicable for Java, but I thought I would make the suggestion.

-4

I suggest you use an IDE to run the debugger. In the IDE you can start in debug by clicking the button next to run, add breakpoints by clicking on a line and view the state of threads and variables graphically. (even change them)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 4
    I'm not a fan of IDEs in my line of programming; surely if an IDE can do what I require, then `jdb` can, since the IDE is likely to be a front end to it? – Doddy Apr 12 '11 at 20:51
  • Surely it can, but why make things far more complicated than they need to be. Shouldn't it be about making the best use of the tools available to get the job done? What is your line of programming out of interest? – Peter Lawrey Apr 12 '11 at 21:42
  • Well most of my programming is on the subject of back-ends or purely number-crunching. IDEs annoy me really. I've used `GDB` on the command-line for a long time and it's fantastic; I have no desire to use an IDE whatsoever. To me, it just adds bulk to the writing process and doesn't make me feel like I've achieved much. So, in light of that, do you know the answer to my original question please? – Doddy Apr 16 '11 at 16:00
  • 1
    I developed for many years using vi and thought IDEs where for the 'soft' developers. Thats was 10 years ago. IDEs have improved alot since then and I think once you learn to use IDEs you will wonder how much time you wasted doing things the hard way. So I think the best thing I can do for you is to rethink your approach. BTW: I only develop large, performance critical server side systems. – Peter Lawrey Apr 17 '11 at 14:41
  • another case for using jdb vs. an IDE is when the development machine (e.g. Desktop) is not the execution machine (e.g. server) and the IDE isn't installed on the execution machine – Andre Holzner Dec 03 '13 at 08:22
  • 1
    @AndreHolzner You can do remote debugging of a server side application without installing additional software. You just need to add command line arguments to start in debug mode and it can accept remote debugging IDEs. – Peter Lawrey Dec 03 '13 at 08:47