3

I'm trying to execute jar file from command line and still getting NoClassDefFoundError exception but the exception message ends with "... 13 more". Means lines I guess.

So how to get full exception message from command prompt? I'm using IntelliJ 14 as development enviroment, but debug and compilation shows no errors and ends successfully. (This is artifact build)

I was even trying to write that error message to file in try/catch block, but my guess is that happens before the main method code executes, because no file is created or System.out.println() is not executed.

Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34
user2977027
  • 105
  • 1
  • 10
  • The error message should be enough to tell you which class is missing. That's the reason why the last 13 lines are cut off: they don't matter. – Marged Oct 08 '15 at 09:10
  • In short NoClassDefFoundError will come if a class was present during compile time but not available in java classpath during runtime. Take a look here: [javarevisited](http://javarevisited.blogspot.my/2011/06/noclassdeffounderror-exception-in.html) – Rad Oct 08 '15 at 09:17
  • Possible duplicate of [Print out full erorr message from java via commandline](http://stackoverflow.com/questions/6804419/print-out-full-erorr-message-from-java-via-commandline) – showp1984 Oct 08 '15 at 09:21
  • The important part (which you didn't show us) is the rest of the line after `NoClassDefFoundError`, which shows the name of the class that cannot be found. The stack trace lines are mostly irrelevant, except to learn why it is needed, i.e. which class/method is trying to use the missing class. – Andreas Oct 08 '15 at 09:21

3 Answers3

1

You already have the full stack trace. The "... 13 more" message is simple suppressing redundant/repeated information.

Let me illustrate:

public static void main(String[] args) throws Exception { a(); }
private static void a() { b(); }
private static void b() { c(); }
private static void c() { d(); }
private static void d() { try { e(); } catch (Exception e) { throw new RuntimeException(e); } }
private static void e() { f(); }
private static void f() { g(); }
private static void g() { try { h(); } catch (Exception e) { throw new IllegalStateException(e); } }
private static void h() { i(); }
private static void i() { j(); }
private static void j() { throw new UnsupportedOperationException("Test"); }

This code throws an exception deep in a call stack. The exception is caught and wrapped in other exceptions, so the output will be:

Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: java.lang.UnsupportedOperationException: Test
    at stackoverflow.Main.d(Main.java:11)
    at stackoverflow.Main.c(Main.java:10)
    at stackoverflow.Main.b(Main.java:9)
    at stackoverflow.Main.a(Main.java:8)
    at stackoverflow.Main.main(Main.java:6)
Caused by: java.lang.IllegalStateException: java.lang.UnsupportedOperationException: Test
    at stackoverflow.Main.g(Main.java:14)
    at stackoverflow.Main.f(Main.java:13)
    at stackoverflow.Main.e(Main.java:12)
    ... 5 more
Caused by: java.lang.UnsupportedOperationException: Test
    at stackoverflow.Main.j(Main.java:17)
    at stackoverflow.Main.i(Main.java:16)
    at stackoverflow.Main.h(Main.java:15)
    ... 8 more

The 5 lines suppressed for the IllegalStateException call stack are the same as the lines from the RuntimeException call stack.
The 8 lines suppressed for the UnsupportedOperationException call stack are the same as the lines from the other two call stacks.

Without suppression, the output would be:

Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: java.lang.UnsupportedOperationException: Test
    at stackoverflow.Main.d(Main.java:11)
    at stackoverflow.Main.c(Main.java:10)
    at stackoverflow.Main.b(Main.java:9)
    at stackoverflow.Main.a(Main.java:8)
    at stackoverflow.Main.main(Main.java:6)
Caused by: java.lang.IllegalStateException: java.lang.UnsupportedOperationException: Test
    at stackoverflow.Main.g(Main.java:14)
    at stackoverflow.Main.f(Main.java:13)
    at stackoverflow.Main.e(Main.java:12)
    at stackoverflow.Main.d(Main.java:11)
    at stackoverflow.Main.c(Main.java:10)
    at stackoverflow.Main.b(Main.java:9)
    at stackoverflow.Main.a(Main.java:8)
    at stackoverflow.Main.main(Main.java:6)
Caused by: java.lang.UnsupportedOperationException: Test
    at stackoverflow.Main.j(Main.java:17)
    at stackoverflow.Main.i(Main.java:16)
    at stackoverflow.Main.h(Main.java:15)
    at stackoverflow.Main.g(Main.java:14)
    at stackoverflow.Main.f(Main.java:13)
    at stackoverflow.Main.e(Main.java:12)
    at stackoverflow.Main.d(Main.java:11)
    at stackoverflow.Main.c(Main.java:10)
    at stackoverflow.Main.b(Main.java:9)
    at stackoverflow.Main.a(Main.java:8)
    at stackoverflow.Main.main(Main.java:6)

That is just a lot of useless redundant waste of output, and gets a lot worse in real applications where the call stacks are much deeper.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

If what you want is solve your NoClassDefFoundException, we could need your stacktrace.

Usually a NoClassDefFoundException thrown when you're executing from the command line is due to a mistake in your classpath definition (-cp paramater in your java -cp list_of_jars package.MainClass).

If you're using Maven to build your project for instance (or any othher building tool such as Ivy, gradle, ...), be careful to all the transitive dependencies added to build your project.

For instance, if your project have a dependency to myLib1.jar, but myLib1 has a dependency to myLib2.jar, you'll have to execute java -cp /myLib1.jar:/myLib2.jar:/myProject.jar mypackage.MyMainClass.

Well, for more help, we need the stacktrace of your exception, your command line, and the section of your Maven POM if you're using maven (or the list of your declared dependencies provided to your building tool)

Cheloute
  • 783
  • 2
  • 11
  • 27
0

The way jars are built has a role to play in the NoClassDefFoundError. The jars which do not package the dependency classes rely on these dependencies to be supplied at run-time, when the JVM is launched. These dependencies are called Run Time Dependencies. The other way is to package all the dependent classes along with the jar being built. This is called an Uber jar or fat jar sometimes.

NoClassDefFoundError occurs when a particular class file is not found in the jar being run as well as all the jars available in the classpath with which JVM is launched. The reason you are not able to get an exception stack trace is because this errors occurs even before the class is loaded by the ClassLoader because the class is not found in the jars available in the classpath.

When the jar is a uber jar - which bundles all the dependency classes into the jar, this exception should not be thrown.

If maven is used to build the project, here is an example of how you can build uber jar using Maven Assembly Plugin.

    <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>fully qualified name of the main class </mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>install</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
deepak marathe
  • 409
  • 3
  • 10