I have exceptions thrown in my java code when an error occurs. I then run the code using jdb so when the exception occurs, I can see the state that the code is in and debug. For all exceptions that I throw, I put in a useful string message. However, when jdb catches the exception, it doesn't print this string along with it. How do I print this string?
I have googled and searched and read the documentation but I can't figure out how.
If I have the test class:
public class Test{
public static void main(String[] args){
throw new IllegalArgumentException("How do I view this string through jdb?");
}
}
And run it through jdb:
$ jdb Test
Initializing jdb ...
> run
run Test
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started:
Exception occurred: java.lang.IllegalArgumentException (uncaught)"thread=main", Test.main(), line=3 bci=9
main[1]
Check out the JDB "catch" command, for example "catch IllegalArgumentException". Per the JDB docs, it will cause the debugger to break when the exception is thrown so that you can probe into the details. This document on debugging does a nice job explaining some of the downsides to being dependant on "print" statements, and also references the JDB "catch" functionality.
Related
Summary
1. I am import java.io.console,i am not getting any error in my code,
code is correct, I thought, It's getting some error in eclipse
software... Error :
Exception in thread "main" java.lang.NullPointerException at
loops.Escapey.main(Escapey.java:9)
Coding:
package loops;
import java.io.Console;
public class Escapey {
public static void main(String[] args) {
Console ar = System.console(); // creating a new object for console
String name = ar.readLine("how old are you ?"); //reads a user input
System.out.printf("%s - pretty age",name);
}
}
Ouput :
- I except the output how old are you ?? 18 18 - pretty age
But the actual output is getting error... Exception in thread "main"
java.lang.NullPointerException at loops.Escapey.main(Escapey.java:9)
I assume you are running this into some IDE. Since System.console returns the attached console, if you run it into IDE, it will return NULL. The better approach is to use Scanner class. If you really want to use System.console, you will have to test it on some console. If on Mac, run the terminal. On Linux any of the terminal apps would work.
If we run this in IDE (Intellij), it is throwing null pointer exception. Since this is related to console, I tried to execute this in command line and it works fine.
Execute these steps in terminal or command line and it will work
1) javac Escapey.java
2) java Escapey
Trying out Apache procrun I see a behaviour that I do not understand. It boils down to the main method of the Java program throwing an exception. This is not logged anywhere and the Windows service does not stop. To investigate further I changed the main method to
public static void main(String[] args) throws Exception {
if (args.length<10000000) {
throw new Exception("one exception right away");
}
...
}
When I install this as a service with prunsrv.exe and then start it, it starts without problems and produces no log output whatsoever. In particular the service does not stop.
For reference, here is how the service is installed with procrun:
& $procrun "//$operation//$service" `
--DisplayName="$service" `
--Description="$service" `
--DependsOn="$depends" `
--Startup=auto `
--Install="$procrun" `
--Jvm="$JVM" `
--Classpath="$cp" `
--Environment="PATH=$env:JAVA_HOME\bin" `
--JavaHome="$env:JAVA_HOME" `
--StartPath="c:\Search" `
--JvmOptions="-Xmx512M;-Xms512M;-Djava.awt.headless=true" `
--StartMode="jvm" `
--StartClass="$classname" `
--LogPath="c:\Search\std-logs" `
--LogPrefix="procrun-$service" `
--LogLevel="Debug" `
--StdError="c:\Search\std-logs\stderr-$no0" `
--StdOutput="c:\Search\std-logs\stdout-$no0" `
--StartParams="(unused)"
I would have expected that the service stops right away and I find some log output in the stderr/stdout files, but nothing. Can anyone explain this?
EDIT: Oh my, it seems like procrun swallows java.lang.Error (in my case java.lang.noClassDefFoundError) instead of screaming, shouting and exiting.
After I wrapped a catch(Throwable) around the main, logging whatever popped out and then exiting without re-throwing, I saw the problem. I got a java.lang.NoClassDefFoundError.
Bottom line: procrun seems to swallow error type exceptions and does not even terminate if one is thrown in the called Java program.
Created an issue: https://issues.apache.org/jira/browse/DAEMON-344
I have the ubiquitous HelloWorldApp.java file
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
I run:
javac HelloWorldApp.java
then I run:
jdb HelloWorldApp
I get:
Initializing jdb ...
>
I type:
stop at HelloWorldApp.main:7
where prompted
then I get
Deferring breakpoint HelloWorldApp.main:7.
It will be set after the class is loaded.
>
I type:
run
where prompted
then I get
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Hello World!
The application exited
I didn't type anything on that last prompt it just exited without breaking. My question is why did it output those Throwable lines and why didn't the debugger stop at the breakpoint I gave it?
I just checked the syntax for stop in JDB Documentation
stop in <class-name>.<method-name> Stop on entry to the given method.
stop at <class-name>:<line-number> Stop at the given line.
I think you command for stop should be either one of the following
stop in HelloWorldApp.main
stop at HelloWorldApp:7
Give it a try to see that fixes your issue!
I realize that when eclipse says:
<terminated> main [java application] C:\program files\java\jre1.8.0_25\bin\javaw.exe
That the program ran and terminated. My problem is that it doesn't really run, as soon as I hit run it immediately shows this message. The program doesn't run at all. Anyone know how to stop javaw.exe from terminating the program immediately?
public class main {
public static void main(final String[] args) throws Exception {
Twitchbot bot = new Twitchbot();
bot.setVerbose(true);
bot.connect("irc.twitch.tv", "6667", "oauth:*********************************");
bot.joinChannel("#donnie64");
}
}
That's because your program did run, and It did exit, all without error or output. It creates a Twitchbot, sets it to verbose, connects, joins the channel, then the program ends after that because it has no more instructions, loops, or threads to wait for. The only thing it says is
<terminated> main [java application] C:\program files\java\jre1.8.0_25\bin\javaw.exe
because that's what Eclipse is supposed to say when a program ends.
If there was an error, it should've printed something in the console in red letters like:
Exception in thread "main" java.lang.RuntimeException
at testing.NFATest.main(NFATest.java:45)
or whatever exception it threw. (NullPointerException, ArrayIndexOutOfBounds, etc)
I have a java program which is a compiler and executor for Jasper Reports and is called via shell script on our reports server.
I just fixed a bug caused by a missing dependancy, but it took me a while to figure out what was going wrong. Normally, problems caused by the compile process are captured, recorded to a log file and emailed to the relevant person. As this was a NoClassDefFoundError, it basically exited the program and failed in a silent manner from the users perspective.
Is there any way for me to capture errors like this one so that they can also be emailed away? I have the authority to modify the executing shell script.
Typically errors are not caught by application code and are thrown to JVM level where they are printed to STDERR. So, your way to track this error is to redirect STDERR to file:
java -cp YourMain 1>stdout.log 2>stderr.log
You can also put both STDOUT and STDERR together:
java -cp YourMain 1>&2 2>wholelog.log
There is a lot of reference about stream redirection in web. You can take a look there if my examples do not satisfy you. And it a depends on your OS.
Just can catch the error i.e.
try {
numericDefinition = new net.sf.cb2xml.def.BasicNumericDefinition(
binName, binarySizes, SynchronizeAt, usePositive, floatSynchronize, doubleSynchronize
);
} catch (NoClassDefFoundError e) {
System.out.println("Class Not Found: " + e.getMessage());
}
You do need to be very careful of your coding though, it is easy to get NoClassDefFoundError thrown at class initialisation time and not get into to the try .. catch block.
The NoClassDefFoundError will be thrown the first time a class is refereneced which could be when could when a class uses a class which uses a class which uses a class ... which uses a class that references a class that does not exist.
The following may fail with NoClassDefFoundError at class initialization because of the import.
import net.sf.cb2xml.def.BasicNumericDefinition; // could cause the NoClassDefFoundError
...........
try {
numericDefinition = new BasicNumericDefinition(
binName, binarySizes, SynchronizeAt, usePositive, floatSynchronize, doubleSynchronize
);
} catch (NoClassDefFoundError e) {
System.out.println("Class Not Found: " + e.getMessage());
}