Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编译器会检测到Eclipse引用的库jar,但不会在运行时检测到_Java_Eclipse_Jar - Fatal编程技术网

Java 编译器会检测到Eclipse引用的库jar,但不会在运行时检测到

Java 编译器会检测到Eclipse引用的库jar,但不会在运行时检测到,java,eclipse,jar,Java,Eclipse,Jar,我通过以下步骤将jar文件添加到我的项目中: Right click on your project Select Build Path Click on Configure Build Path Click on Libraries and select Add External JARs Select the jar file from the required folder Click and Apply and Ok 这很好地导入了我的jar文件,Java编译器可以很好地检测到对新库的

我通过以下步骤将jar文件添加到我的项目中:

Right click on your project
Select Build Path
Click on Configure Build Path
Click on Libraries and select Add External JARs
Select the jar file from the required folder
Click and Apply and Ok
这很好地导入了我的jar文件,Java编译器可以很好地检测到对新库的引用

但当我运行应用程序时,它崩溃了,说它无法识别库:

线程“main”java.lang.NoClassDefFoundError中的异常:org/json/simple/JSONObject 位于com.ebook.Main.Main(Main.java:14) 原因:java.lang.ClassNotFoundException:org.json.simple.JSONObject 位于java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) 位于java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) 位于java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 还有一个
导入这个jar(
json simple
)时我做错了什么?

如果您正确添加了jar文件,那么您应该检查com.ebook.Main.Main(Main.java:14)初始化相关的导入语句是否正确。然后清理并重新生成。希望这能解决您的问题。

当您运行jar文件时,如果您使用库的,则需要设置类路径:
java-cp C:\foo\bar\JsonLibrary.jar您的\u jar\u file.jar

可能会帮助您设置类路径


注意:它在Eclipse中工作的原因是Eclipse自动为您处理类路径,但编译后您需要自己处理。

您需要将-classpath arg添加到java中command@SharonBenAsher什么值应该与此标志一起传递?我正在阅读文档,他们提到了目录和zip/jar文件的
类搜索路径,但我不确定这是什么意思。java-cp:注意“:”是一个分隔符。如果您在windows中,请使用“;”。例如,java-cp/Users/kk/eclipseworkspace/JsonSampleProject/json-simple-1.1.1.jar:。sample.json.SimpleJson