NoClassDefFoundError on an interface, not a class

前端 未结 6 763
-上瘾入骨i
-上瘾入骨i 2020-12-21 00:05

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

package code;
public interface Co         


        
相关标签:
6条回答
  • 2020-12-21 00:46

    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.

    0 讨论(0)
  • 2020-12-21 00:48

    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
    
    0 讨论(0)
  • 2020-12-21 00:55

    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.
    0 讨论(0)
  • 2020-12-21 01:01

    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.

    0 讨论(0)
  • 2020-12-21 01:06

    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?

    0 讨论(0)
  • 2020-12-21 01:09

    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

    0 讨论(0)
提交回复
热议问题