“外部库(external libraries)” 的 Eclipse 部分。

huangapple go评论72阅读模式
英文:

Eclipse external libraries

问题

以下是翻译好的内容:

我在使用Eclipse中的外部库时遇到了问题。每当我尝试启动程序时,都会抛出NoClassDefFound异常。显然我做错了什么。有人能重新为我解释一下如何将它们正确地包含到项目中吗?(是的,我已经在这方面做了很多研究,并且有人建议我使用Maven依赖,但我不想这样做。我还找到了一些与我的问题更相关的主题,但没有任何帮助。所以,如果有人能够解释具体应该做什么,我会非常感激。)

我正在使用最新版本的Eclipse和Java 8(通常是13,但那并没有解决我的错误)。它只在运行时抛出异常,因此在没有任何错误的情况下编译。

项目设置:http://prntscr.com/udsxts

清单文件:

Manifest-Version: 1.0
Main-Class: me.sxlver.consoleclient.Main
Class-Path: lib/java-json.jar lib/snakeyaml-1.26.jar

异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
        at me.sxlver.consoleclient.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

(我只是在创建一个JSONObject的实例)

Java Build Path:http://prntscr.com/udtgjn

库:http://prntscr.com/udtqas

英文:

I am currently experiencing issues whilst using external libraries in Eclipse. Whenever I am trying to launch my program it throws a NoClassDefFound exception. I am obviously doing something wrong. Could someone re-explain for me how to properly include them into your project? (Yes I have already done a lot of research on this and got recommended to use Maven dependencies which I don‘t want. Also I have found some threads that were more relevant to my issue but nothing helped. So yeah, I would really appreciate it if someone could explain what exactly to do.)

I am using the newest ver of eclipse together with java 8 (usually 13 but that did not fix my error). It only throws the exception on runtime so, it compiles without any errors.

Project setup: http://prntscr.com/udsxts

Manifest file:

Manifest-Version: 1.0
Main-Class: me.sxlver.consoleclient.Main
Class-Path: lib/java-json.jar lib/snakeyaml-1.26.jar

Exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
        at me.sxlver.consoleclient.Main.main(Main.java:20)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

(I am just creating an instance of JSONObject)

Java Build Path: <http://prntscr.com/udtgjn>

Libraries: <http://prntscr.com/udtqas>

答案1

得分: 0

我怀疑这可能是由于构建路径设置不正确造成的。我通常的做法是:

  1. 进入 Window -> Preferences -> Java -> Build Path -> User Library -> New Library -> 添加 jar(你的 lib 文件)。
  2. 右键点击你的项目 -> Build Path -> Add Libraries -> Add User Libraries -> Done
英文:

I suspect this is due to the build path was setup incorrectly. What I normally do is:

  1. Go to Window -> Preferences -> Java -> Build Path -> User Library -> New Library -> Add jar (your lib files).
  2. Right click on your project -> Build Path -> Add Libraries -> Add User Libraries -> Done.

答案2

得分: 0

Run菜单中,选择Run Configurations...

选择Dependencies选项卡,然后点击Add External JARs按钮

将会出现一个文件选择窗口。导航到相关的JAR文件位置并选择它。

英文:

From the Run menu, select Run Configurations...

“外部库(external libraries)” 的 Eclipse 部分。

Select Dependencies tab, then click on button Add External JARs

“外部库(external libraries)” 的 Eclipse 部分。

A file chooser window will appear. Navigate to the location of the relevant JAR and select it.

答案3

得分: 0

对我而言,有效的方法是:右键单击项目 -> 构建路径 -> 添加外部存档...

英文:

For me, what worked was: Right-click project -> Build Path -> Add External Archives...

huangapple
  • 本文由 发表于 2020年9月9日 01:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63799163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定