简体   繁体   English

在Java中使用外部库

[英]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: 我的build.xml中的两个任务都有相同的类路径集,并且编译任务每次运行都很好:

<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: 尝试通过命令行手动运行jar会得到相同的结果:

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: 请注意,库jarfile可以:

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. 我在这个问题上打了将近3天。 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. PS:请注意,我在Windows环境中将Console2与bash一起使用以进行命令行工作,因此Windows本机用户使用Linux Shell语法。 Using vanilla cmd to run the jar yields the same result. 使用香草cmd运行罐子会得到相同的结果。

-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 尝试在JAR中设置类路径

Alternatively try running without the -Jar option, by specifying the main class on the command line 或者,通过在命令行上指定主类,尝试不使用-Jar选项运行

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. 可能的原因之一是,在加载类LWJGLException时,它还引用了在类路径上找不到的另一个类。 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 ! 这里重要的是,您有这个NoClassDefFoundError而不是ClassNotFoundException,这是您假设遇到的错误:它找不到类LWHLException,可以! But it cannot load it.... 但是它无法加载...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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