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?
-
4This is what logging was made for. You can easily and simply set the logging state.Hovercraft Full Of Eels– Hovercraft Full Of Eels2013-07-13 13:15:18 +00:00Commented Jul 13, 2013 at 13:15
-
2Unclear. What do you want to do exactly? How do you want to debug?fge– fge2013-07-13 13:15:38 +00:00Commented Jul 13, 2013 at 13:15
-
2I agree with @HovercraftFullOfEels. Use logging and set log level to whatever you want.Mohammad Banisaeid– Mohammad Banisaeid2013-07-13 13:27:21 +00:00Commented Jul 13, 2013 at 13:27
-
You can't turn on and off debugging at compile time, so it's not something you have to avoid. (You can turn off debugging information, but you rarely need to do this)Peter Lawrey– Peter Lawrey2013-07-13 14:24:29 +00:00Commented Jul 13, 2013 at 14:24
4 Answers
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.
2 Comments
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.
Comments
Use jdb to debug from the command line.
That being said, I have no idea what "turn the debug off and on" means.
1 Comment
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...