HavelTheGreat
HavelTheGreat

Reputation: 3386

Unsure about reason behind NoClassDefFoundError

I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.

Here is my D:\Test1.Java file

public class Test1 { 

    static {
        //System.loadLibrary("HeyLand");
        System.load("D://HeyLand.dll");
    }

    public native void displayHeyLand();

    public static void main (String[] args) {
        Test1 t = new Test1();
        t.displayHeyLand();
    }
}

After compiling, attempting to run D:\Test1.classresults in the following:

D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
        at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class.  Program will exit.

Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.

Perhaps I'm just missing something incredibly simple, I am very newbie with Java.

Any help is greatly appreciated!

Upvotes: 0

Views: 73

Answers (2)

Reimeus
Reimeus

Reputation: 159864

The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)

java -cp . Test1

Upvotes: 1

java.manish.2015
java.manish.2015

Reputation: 1071

Java normal syntax for executing class file is 
Java [<options>....} <class-name> [<arguments>....]


For example 

java com.package.name.Test1

here how compiler works

1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.



Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
    I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name 

    For example:
    java com.package.name.Test1 instead of 
    java Test1 

2. File name or pathname rather then class name
   As I noticed output file is in different location. That why class file path was different.
   java Test1.class
   java com/package/name/Test1.class

3. Typo


also I noticed you are using 
  static {
        //System.loadLibrary("HeyLand");
        System.load("D://HeyLand.dll");
    }


 Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs

Upvotes: 0

Related Questions