简体   繁体   中英

Using external libraries in Java

Here's the error I keep getting at runtime:

[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException

Note, this is a runtime error, not a compile-time one. Both tasks in my build.xml have an identical classpath set, and the compile task runs fine every single time:

<path id="classpath">
    <fileset dir="lib" includes="*.jar" />
</path>

<target name="compile">
    <mkdir dir="build/classes"/>
    <javac
        srcdir="src"
        classpathref="classpath"
        includeantruntime="false"
        destdir="build/classes"
    />
</target>
...
<target name="run" depends="clean,compile,jar">
    <java
        jar="build/jar/${project.name}.jar"
        fork="true"
        classpathref="classpath"
        >
        <sysproperty key="java.library.path" path="${path.lib}/windows"/>
    </java>
</target>

Trying to run the jar via command-line manually yields the same result:

java -cp .:lib/*.jar -Djava.library.path=lib/windows -jar build/jar/JUtopia.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException

Note that the library jarfile is ok:

bash-3.1$ jar -tf lib/lwjgl.jar | grep LWJGLException
org/lwjgl/LWJGLException.class

And the native libraries are in place:

bash-3.1$ ls lib/windows/lwjgl.dll
lib/windows/lwjgl.dll

The question: where the blazes have I gone wrong? I've been beating at this problem for nearly 3 days. Any help would be much appreciated.

Full result stack:

clean:
   [delete] Deleting directory C:\Users\mkumpan\Projects\JUtopia\build

compile:
    [mkdir] Created dir: C:\Users\mkumpan\Projects\JUtopia\build\classes
    [javac] Compiling 12 source files to C:\Users\mkumpan\Projects\JUtopia\build\classes

jar:
    [mkdir] Created dir: C:\Users\mkumpan\Projects\JUtopia\build\jar
      [jar] Building jar: C:\Users\mkumpan\Projects\JUtopia\build\jar\JUtopia.jar

run:
     [java] Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
     [java]     at JUtopia.<init>(Unknown Source)
     [java]     at JUtopia.main(Unknown Source)
     [java] Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
     [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
     [java]     ... 2 more

PS: Note, I'm using Console2 with bash in a windows environment for my commandline work, thus the windows natives yet linux shell syntax. Using vanilla cmd to run the jar yields the same result.

-jar...

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. - reference

try setting the Class-Path in the JAR

Alternatively try running without the -Jar option, by specifying the main class on the command line

One of the possible causes is that while loading the class LWJGLException it also references another class which can't be found on the classpath. Hence the reported error is sometimes not clear.

Important here is thet you have this NoClassDefFoundError and not ClassNotFoundException which is the error you assume you are having: it cannot find the class LWHLException, yes it can ! But it cannot load it....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM