5

I have an issue with NoClassDefFoundError. I am using interfaces, and no class definition should be available:

package code;
public interface Constants {...}

The class implementing this interface compiles without any errors, and a JAR file has been built, but at runtime it gives me an error.

import ...;
import code.*;
public class MultiDoc extends LanguageAnalyser implements Constants{}

Constants contains only a list of constants.

I read some posts pointing to CLASSPATH as a cause of this problem, but I have the code package in my CLASSPATH. If I didn't have it, it would produce a compilation error. So, the problem should be something else.

The runtime error is:

java.lang.NoClassDefFoundError: code/Constants

What's the solution?

Pops
  • 30,199
  • 37
  • 136
  • 151
Marcus
  • 151
  • 2
  • 2
  • 8
  • 4
    `The "Constants" only contains a list of constant variables.` That's called the [Constant Interface anti pattern](http://en.wikipedia.org/wiki/Constant_interface) :) – Thomas Jul 12 '11 at 12:11
  • And, what precisely is a "constant variable"? ;) – Hot Licks Jul 12 '11 at 12:12
  • @Daniel - a variable, where the javaDoc tells you: **HANDS OFF** ;) – Andreas Dolk Jul 12 '11 at 13:51
  • @alef - rolled back the tag changes - I was pretty confused by one of your comments, thought GATE was some kind of other language that uses java byte code (like scala or so). Now I know that it is just a java framework for nlp. *And it has a main method!!* – Andreas Dolk Jul 12 '11 at 14:31

6 Answers6

9

Check the static initialization of this class. Look here: what is the difference between NoClassDefFoundError and ClassNotFoundException.
java.lang.NoClassDefFoundError: code/Constants does not mean that the Constants class is not in the CLASSPATH. In fact it is quite the opposite. It means that the class was found by the ClassLoader, however when trying to load the class, it ran into an error reading the class definition. This typically happens when the class in question has static blocks or members which use a Class that's not found by the ClassLoader.

Community
  • 1
  • 1
zacheusz
  • 8,750
  • 3
  • 36
  • 60
  • No, it doesn't use any other classes, it only contains a list of constants like this : public static final String STFDPARSE_LINKPASLIST_FEATURE_NAME = "linkpaslist"; – Marcus Jul 12 '11 at 13:15
2

Note that there are two different exceptions that sound very similar: ClassNotFoundException and NoClassDefFoundError.

The first exception occurs in the simple case that the JVM looks for "packageB.ClassA" and can't find it in the search path. There are a few other cases, I suspect, but fairly rare.

The second exception occurs mostly when an appropriately-named class file is found, but for some reason it can't be used. There are two primary reasons for this:

  1. The class is in, eg, "packageA/ClassA.class" but internally is named "packageB/ClassA" (or the package name was accidentally omitted in the source, or the class file is in the wrong directory, given its package name).
  2. While loading the class, there was some sort of error, usually an error initializing static storage.
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
1

From your question I see that the compiled Constants interface resides in a jar which is different from the jar/location of the implementing classes. So you should be able to execute your application like this:

java -cp /path-to-jar/JarContainingConstants.jar my.application.Main

(replace the names with your real names)

If you've added the other classes to another jar and then if you made that jar executable and then if you tried to run it with java -jar MyApplication.jar, then any the classpath defined outside the executed jar is ignored. But the above command shoud work with any jar (executable or not).


from comment

[alef@localhost ~]$ java -cp /../code-misc.jar /.../MultiDoc.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: /.../MultiDoc/MultiDoc/jar 
Caused by: java.lang.ClassNotFoundException: .home. 
  ... .MultiDocDateMathcer.jar 
  at java.net.URLClassLoader$1.run(URLClassLoader.java:217) 
  ... 
  at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
Could not find the main class: /.../MultiDoc/MultiDocr.jar. 
Program will exit.

You call is definitely incorrect. Do it like this (UPDATE):

java -Dgate.home=/my/new/gate/home/directory -cp gate.jar;code-misc.jar;MultiDoc.jar gate.Main
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • It gives this error in terminal: [alef@localhost ~]$ java -cp /../code-misc.jar /.../MultiDoc.jar Exception in thread "main" java.lang.NoClassDefFoundError: /.../MultiDoc/MultiDoc/jar Caused by: java.lang.ClassNotFoundException: .home. ... .MultiDocDateMathcer.jar at java.net.URLClassLoader$1.run(URLClassLoader.java:217) ... at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: /.../MultiDoc/MultiDocr.jar. Program will exit. I have that code-misc.jar in my CLASSPATH and it contains code package which has Constants.class file. – Marcus Jul 12 '11 at 13:37
  • Actually it should not work this way, I am using GATE (a NLP tool) and it does not have main function, instead of main it has execute() which exactly works as a main method in normal java code. GATE has its own environment, and its own gate.jar file which contains its own version of java library + some NLP specific written libraries. It works like a JAVA editor GUI but with extra NLP related features. I am using it since 4 months ago and it has been working fine, but I never tried interfaces before. – Marcus Jul 12 '11 at 14:07
  • @alef - if you start it with `java` then you **need** a class that has a main method. Maybe the GATE framework provides a class with a main method and you contribution/plugin provides that `execute()` method. Believe me, there is a `main` method somewhere. Examine the `MANIFEST.MF` file inside `MultiDoc.jar`, there should be a pointer. – Andreas Dolk Jul 12 '11 at 14:19
  • I think GATE overrides the CLASSPATH with its own paths, disregarding my environment variables. – Marcus Jul 12 '11 at 16:36
  • I found a solution. I just changed GATE build.xml file and added my own environment CLASSPATH as a parameter there. Thank you guys :-). – Marcus Jul 12 '11 at 16:55
  • But still I would appreciate if you give me the link to gate document where u find that interesting command. – Marcus Jul 12 '11 at 16:56
  • @alef - http://gate.ac.uk/ - menu *Documentation*, first entry *user guide*. just googled for *java nlp gate*, the keywords you provided in your comments. – Andreas Dolk Jul 13 '11 at 07:39
1

If you have somedirectory/code in your classpath, then that's wrong. You always need the base directory in your classpath (in this case it would be somedirectory). Java itself will search those roots for a directory called code containing a file called Constants.class.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

You might have it in your CLASSPATH when compiling but this does not guarantee that it is in the CLASSPATH when you run it. How do you run it?

morja
  • 8,297
  • 2
  • 39
  • 59
  • See Andreas_D's answer. Actually with linux you have to put a colon between the jars: `java -cp /.../code-misc.jar:/.../MultiDoc.jar my.application.Main` – morja Jul 12 '11 at 13:57
  • If thats not working you should check if your code-misc.jar is correct. Do you use the same jar for compiling? – morja Jul 12 '11 at 14:04
0

Any Interface must declared inside class.

public class Calbacks {
    public interface IBaseFragmentInterface {
        void NotifyMainActivity();
    }
}

*I very long find resolution of this problem, but i find resolution independently by the method of scientific poking

Andrew Zaitsev
  • 323
  • 3
  • 4