14

How do you turn the debug on and off within your Java program ? How do you turn the debug on and off without recomipiling the java program?

Abinash Sinha
  • 840
  • 6
  • 18

4 Answers4

18

A setting to the Java virtual machine allows debuggers e.g. jdb to attach. See http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html

This is the important bit:

Running MyClass in a JVM with debugging enabled:

java -agentlib:jdwp=transport=dt_shmem,address=jdbconn,server=y,suspend=n MyClass

Using the jdb debugger

jdb -attach jdbconn 

Note: These option settings are for a connection JVM <-> debugger on the local machine via shared memory, other useful settings allow to connect to a JVM on a remote machine via network sockets.

mvw
  • 5,075
  • 1
  • 28
  • 34
  • 2
    Possible things the original poster really had in mind: 1.) Turning on and off the logging output 2.) compiling with adding debug information (similar to C/C++). – mvw Jul 13 '13 at 13:24
  • It is great that you are trying to help the asker out. However, leaving an answer with only a link can be harmful in some cases. While your answer is good now, if the link were ever to die, your answer would lose it's value. So it will be helpful if you summarize the content from the article in your answer. See [this](http://goo.gl/wQTjc) question for clarification. – pascalhein Jul 13 '13 at 13:39
2

There are two things you have to consider:

  • you need only to compile your code once in order to have debug information; and by default, source file and line number debug information are generated (documentation);
  • the ability to debug or not is controlled when you invoke the JVM.

For Oracle's JVM, this set of options will allow to plug in a debugger implementing JDWP (Java Debug Wire Protocol) on port 12345 (TCP):

-Xdebug -Xrunjdwp:server=y,suspend=n,transport=dt_socket,port=12345

Note the suspend=n; if you do suspend=y, the JVM will not run unless you actually connect a debugger...

Finally, a good link explaining the nooks and crannies behind JDWP, JVM[DPT]I: here

Here is also a tutorial for jdb, already mentioned by other answers.

Community
  • 1
  • 1
fge
  • 119,121
  • 33
  • 254
  • 329
1

Use jdb to debug from the command line.

That being said, I have no idea what "turn the debug off and on" means.

Ingo
  • 36,037
  • 5
  • 53
  • 100
0

Without using IDE to debug

1)you can write your java program with Assertions. When ever you want you can enable / disable them.

2) you can use logs configured with log4j.properties. In your java program you can specify info and debug logs whenever you want you can simple configure in log4j.properties when you want to display debug or info logs etc...

Akshat
  • 29
  • 5
  • ow do we use the logger?? – Abinash Sinha Jul 14 '13 at 05:38
  • 1
    easily you can configure log4j-1.3alpha0.jar , log4j.properties file and b) org.apache.log4j.Logger logs = Logger.getLogger(MyJavaFile.class); logs.info(" this is info log"); logs.debug(" this debug logs"); (all the System.out.pritnln() can be used in log.info() example logs.info(" user name "+username); ect...) in log4j.properties you can specify : log4j.rootLogger=INFO or log4j.rootLogger=DEBUG – Akshat Jul 14 '13 at 05:40