I have the following code:
import org.apache.commons.lang.exception.ExceptionUtils;
public void myMethod() {
try {
// do something
} catch (Exception e) {
System.out.println(ExceptionUtils.getStackTrace(e)); // prints "java.lang.NullPointerException"
System.out.println(ExceptionUtils.getFullStackTrace(e)); // prints "java.lang.NullPointerException"
e.printStackTrace(); // prints "java.lang.NullPointerException"
}
}
The output that I would like to see is a full stacktrace with line numbers and the hierarchy of classes that failed. For example,
Exception in thread "main" java.lang.NullPointerException
at org.Test.myMethod(Test.java:674)
at org.TestRunner.anotherMethod(TestRunner.java:505)
at java.util.ArrayList(ArrayList.java:405)
This code is being run inside a larger app which also has log4j, but I'm hoping to be able to get the exception into a string so I can send it as an email to the java developers.
Does anyone have any ideas about how I can capture the full stack trace to a string? I can't use Thread.currentThread().getStackTrace() since this app runs on Java 4. What might be blocking the above code from printing the full stacktrace?
this is beacause java does some code optimiziation when run in server mode (java -server)
to skip this. use -XX:-OmitStackTraceInFastThrow
in java args
see link below for details:
http://jawspeak.com/2010/05/26/hotspot-caused-exceptions-to-lose-their-stack-traces-in-production-and-the-fix/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With