Maslor
Maslor

Reputation: 1910

How to print all java exceptions on the terminal?

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.

Upvotes: 13

Views: 4690

Answers (4)

enrico.bacis
enrico.bacis

Reputation: 31484

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.

Upvotes: 4

The standard Windows console (cmd) is very short of features, you need to install a console on steroids like ones that are out there, so you'll be able to have a larger scrollback. Take a look into the alternatives listed here.

Personally I use cmder, that is not in the list, but you must test them all and peek which fit you better.

Upvotes: -1

sakthisundar
sakthisundar

Reputation: 3288

You can print the whole stack Traces by below method. Reading the Java Doc for printStackTrace will tell why is this designed this way.

void printAllTraces(Throwable throwable) {
       StackTraceElement[] stackTraces = throwable.getStackTrace();
       for (StackTraceElement stackTrace: stackTraces) {
          System.out.println(stackTrace); 
       }
}

Upvotes: 0

Recoba20
Recoba20

Reputation: 324

StackTraceElement[] stackTraces = Thread.currentThread().getStackTrace();
for (StackTraceElement element : stackTraces) {
    System.out.println(element);
}

Upvotes: 0

Related Questions