Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print all java exceptions on the terminal?

Tags:

java

cmd

I've noticed that whenever an exception is thrown on the terminal, I often get an abbreviate failure trace such as this:

java.lang.NoClassDefFoundError: javafx/application/Application
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
Exception in thread "main" 

What I want to know is how to print all of the trace, including those ... 13 more.

EDIT: This post has been identified as a possible duplicate of Print full call stack on printStackTrace()? . I did read the latter but didn't find an answer to my question, I only found information on why it happens.

like image 851
Maslor Avatar asked Jul 24 '15 10:07

Maslor


People also ask

What are the ways to print exception in Java?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How many ways we can print exceptions in Java?

In Java, there are three methods to print exception information. All of them are present in the Throwable class. Since Throwable is the base class for all exceptions and errors, we can use these three methods on any exception object.

How do you print Stacktrace?

Just use new Throwable(). printStackTrace() method and it will print complete stack trace from where a method is called, into the console.

What are the methods used to print exception messages?

The Throwable class provides the following three methods to print the exception message: Using printStackTrace Method. Using getMessage() Method. Using toString() Method.


1 Answers

You can pass the Throwable object (the Exception in your case) to a method like this:

static void printFullTrace(Throwable throwable){
    for(StackTraceElement element: throwable())
        System.out.println(element);
}

The truth is that you are already seeing the whole stack trace in those lines, because a part of it is repeated and so omitted for brevity. You can understand better the mechanism here.

like image 71
enrico.bacis Avatar answered Sep 30 '22 05:09

enrico.bacis