0

I know this has been asked many times but I can't figure it out. :(

I have a big project and the structure is like:

Project - build
        - Config
        - src
        - tst
        - build.xml
        - webapp
        - eclipse-bin

There is a java file under tst/com/myspace/gateway/content/restlet/SanityTest.java In the java file:

package com.myspace.gateway.content.restlet;
...
public class SanityTest extends ContentGatewayRestletResourceTestCase {
...
public static void main() {
}
}

After the whole project compiles, the class file of SanityTest is under build/private/classes/tests/com/myspace/gateway/content/restlet/SanityTest.class

I went into build/private/classes/tests and ran "java -cp . com.myspace.gateway.content.restlet.SanityTest" but got the NoClassDefFoundError.

Please help me!

PS: Stack trace:

Exception in thread "main" java.lang.NoClassDefFoundError: com/myspace/gateway/content/ContentGatewayTestCase
    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$000(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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
    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$000(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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
Caused by: java.lang.ClassNotFoundException:         com.myspace.gateway.content.ContentGatewayTestCase
    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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 24 more
Could not find the main class: com.myspace.gateway.content.restlet.SanityTest.  Program will exit.
StackJay
  • 61
  • 1
  • 9
  • wow I need to re-structure my code... – StackJay May 14 '15 at 07:45
  • NoClassDefFoundError can happen even if you have the class correctly in classpath. Reason can be failing static code or any othrr exception while classloader tries to load the class – Nitin Dandriyal May 14 '15 at 07:49
  • Can you give copy the full error including stack trace into your question? Use the "edit" button please, and properly format it. – RealSkeptic May 14 '15 at 07:50
  • Why do you roll back formatting changes? Do you think it looks better unformatted? – Stefan van den Akker May 14 '15 at 07:57
  • Be careful where you're running your command line <-cp whatever path> If I were using maven to build my project and say it would put my classes into two separate folders, one called ./classes/ and another ./test-classes/ to run my Main file I would have to somehting of the form: D:\Data\myUser\Documents\NetBeansProjects\DeleteMe\target>java -cp ./classes/;./test-classes/ launcher.Main | Lisbon <--- Program output public class Main { Launcher public static void main(String[] args) { System.out.println(new pt.MostBeautifulCity().getName()); } } – 99Sono May 14 '15 at 08:05
  • By the way - I would recommend that you discard the old-school way of building projects in an IDE where you select the "project" nature - jar - war whatever. Eclipse loves that you do that. Just don't! use maven and if the IDE hates maven, discard the IDE. – 99Sono May 14 '15 at 08:12
  • The project is built primarily using Ant I think. Looks like com.myspace.gateway.content.ContentGatewayTestCase is missing. I'm looking into that... – StackJay May 14 '15 at 08:14

1 Answers1

0

You ran

java -cp . com/myspace/gateway/content/restlet/SanityTest

You should have run:

java -cp . com.myspace.gateway.content.restlet.SanityTest

Read this Q&A for more details:


UPDATE

According to the stacktrace, the problem is that Java cannot find the ContentGatewayTestCase class. Presumably, that is a class that your SanityTest class depends on. However, the source code snippet shows that SanityTest extends ContentGatewayRestletResourceTestCase. You need to figure out if the ContentGatewayTestCase dependency is correct:

  • If it is, the corresponding ".class" file needs to be in the same directory as your "SanityTest.class" file.

  • Either way, you probably have a problem with the way you are compiling your code.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I did run java -cp . com.myspace.gateway.content.resetlet.SanityTest. My mistake in the OP, I will update it. – StackJay May 14 '15 at 07:54