[英]Jar dependency can't be found in run-time
I'm trying to compile a jar file with my java program using javac -cp dependency.jar *.java
and then run it using java -cp dependency.jar mainPrgram
The program compiles just fine, and also runs fine, until a function call是使用依賴項制作的。
我得到的例外如下
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at DFS.readMetaData(DFS.java:250)
at DFS.touch(DFS.java:111)
at CommandLine.<init>(CommandLine.java:41)
at CommandLine.main(CommandLine.java:132)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
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)
... 4 more
嘗試了不同的解決方案
為什么我在 Java 中收到 NoClassDefFoundError?
但似乎沒有一個工作
更新:代碼在 Eclipse 中運行良好,但不能從命令行運行
使用 eclipse 運行后, .classpath
具有以下內容:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path=""/>
<classpathentry kind="lib" path="C:/Users/MyPC/Documents/projects/MusicPlayer/gson-2.8.6.jar"/>
<classpathentry kind="output" path=""/>
</classpath>
您的程序已針對 jar 成功完成。 但是 jar 本身具有運行所需的依賴項。 您需要將所有依賴項添加到 class 路徑中,例如java -cp dependency.jar:subdependency.jar Main
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.