[英]Use reference libraries in Eclipse for Java
在此之前,我确切地说我在这个论坛和其他论坛上找到了这个主题,但是这些论坛上给出的解决方案都没有对我有用。
I am using Eclipse on Ubuntu and I am trying to import some.jar for my java project, but I am not able to make it works, here is a screen of the issue:
如您在屏幕右上角所见,okhttp.jar 已添加到构建路径中,并且两个导入语句(通过按“ctrl + shift + o”生成)正确遵循 Package 中出现的内容探索上左边。
I have also tried to do it without eclipse (putting okhttp.jar and the class in the same folder and compiling the file with javac -cp okhttp.jar Oracle.java
), but even if its compile, I get this error when I try执行java Oracle
:
Exception in thread "main" java.lang.NoClassDefFoundError: com/squareup/okhttp/OkHttpClient
at Oracle.<clinit>(Oracle.java:20)
Caused by: java.lang.ClassNotFoundException: com.squareup.okhttp.OkHttpClient
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
有没有人知道是什么导致了这个问题?
我看到您已将其添加为模块,但我不确定这是否正确。 并非每个 jar 都是一个模块。 将其添加为简单的 jane 依赖项 - 采用拼图的库并不多。 它应该出现在“类路径”部分。 右键单击该库将其删除,然后再次右键单击它并添加它,这次是作为类路径依赖项,而不是作为模块。
I have also tried to do it without eclipse (putting okhttp.jar and the class in the same folder and compiling the file with javac -cp okhttp.jar Oracle.java), but even if its compile, I get this error when I try执行 java Oracle
不同的问题。
你有 2 个“世界”。 编译时间和运行时间。
对于“正常”依赖项,您需要它在两种情况下都可用。 javac
和 eclipse 中的整个“会话”通常算作“编译时间”,但java MyApp
算作运行时间。
因此:
java -cp okhttp.jar:. Oracle
有效(在 windows 上使用;
而不是:
) - 您还需要在运行时指定 okhttp 在类路径中。 Oracle.class
contains JUST the code that was in the public class Oracle {}
section of your Oracle.java
file, not anything else. 它是一个中间产品,而不是准备好运行交接“只需单击它”可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.