简体   繁体   中英

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

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.

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 :

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. Add it as a plain jane dependency - not many libraries are adopting the jigsaw thing. 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

Different issues.

You have 2 'worlds'. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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