简体   繁体   English

将 Eclipse 中的参考库用于 Java

[英]Use reference libraries in Eclipse for Java

before anything, I precise that I have found the subject on this forum and others, but none of the solution that were given on those forum have worked for me.在此之前,我确切地说我在这个论坛和其他论坛上找到了这个主题,但是这些论坛上给出的解决方案都没有对我有用。

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: 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: 在此处输入图像描述

As you can see on the top-right of the screen, okhttp.jar has been add to the build path, and the two import statements (generated by pressing "ctrl + shift + o") correctly follow what appear in the Package Explore on the left.如您在屏幕右上角所见,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 to execute java Oracle : 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

Do any one have an idea about what is causing this issue?有没有人知道是什么导致了这个问题?

I see you have added it as a module , and I'm not sure that's right.我看到您已将其添加为模块,但我不确定这是否正确。 Not every jar is a module.并非每个 jar 都是一个模块。 Add it as a plain jane dependency - not many libraries are adopting the jigsaw thing.将其添加为简单的 jane 依赖项 - 采用拼图的库并不多。 It should be appearing in the 'Classpath' section.它应该出现在“类路径”部分。 Right click the library to remove it, then right click it again and add it, this time as classpath dependency, not as a module.右键单击该库将其删除,然后再次右键单击它并添加它,这次是作为类路径依赖项,而不是作为模块。

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 to execute java Oracle 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

Different issues.不同的问题。

You have 2 'worlds'.你有 2 个“世界”。 Compile time, and Run time.编译时间和运行时间。

For 'normal' dependencies, you need it to be available in both cases .对于“正常”依赖项,您需要它在两种情况下都可用。 javac , and your entire 'session' in eclipse generally counts as 'compile time', but java MyApp counts as run time. javac和 eclipse 中的整个“会话”通常算作“编译时间”,但java MyApp算作运行时间。

Thus:因此:

java -cp okhttp.jar:. Oracle

works (use ; on windows instead of : ) - you need to specify that okhttp is on the classpath at runtime as well.有效(在 windows 上使用;而不是: ) - 您还需要在运行时指定 okhttp 在类路径中。 Oracle.class contains JUST the code that was in the public class Oracle {} section of your Oracle.java file, not anything else. Oracle.class contains JUST the code that was in the public class Oracle {} section of your Oracle.java file, not anything else. It's an intermediate product and not a ready to run handsoff 'just click it' executable.它是一个中间产品,而不是准备好运行交接“只需单击它”可执行文件。

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

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