How to use a class from JAR in eclipse

29,509

Solution 1

In eclipse, right click on a project->Propeties->Java Build Path->Add External JARs (Add JARs if the jar is inside the project's folder) and then choose your jar file. From now you can use the inner classes of the jars you added. Eclipse will import them when you'll start using them.

Solution 2

I've got the same problem as you today, And no answer from the web can solve it. However, I fixed it at last.

In fact, there is nothing wrong with the setup, it is right to import those jars through "Add External JARs". The real problem is the location/package of you java code. I found that you have to put your .java file in the default package. For example, you will get errors if you put your java code in a package like com.xxx.yyy.ccc, below is an image which shows the right location/package you should use(see WTF.java). After doing that, you program will be able to run.

enter image description here

However, that is how i fixed my problem, i'm not sure that could work for everyone..

Solution 3

Why don't you use these two JARs—— stdlib-package.jar and algs4-package.jar.

And below the code page(http://algs4.cs.princeton.edu/code/)

Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar or algs4.jar. Why not?

A. The libraries in stdlib.jar and algs4.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use these package versions: stdlib-package.jar and algs4-package.jar.

Warning: if you are taking Princeton COS 226 or Coursera, Algorithms, Part I or II, you must use the default package verison of our libraries to facilitate grading.

Showing my test success: Showing my test success

Solution 4

If you have a folder with your JAR files into the project:

  1. Right click on the project>Build Path>Configure Build Path;
  2. At the tab "Libraries" click on Add JARs, search and select the JARs files you want to use.

If you have yours JAR files any other place outside the project:

  1. Right click on the project>Build Path>Configure Build Path;
  2. At the tab "Libraries" click on Add External JARs, search and select the JARs files you want to use.
Share:
29,509
checho
Author by

checho

Updated on January 13, 2020

Comments

  • checho
    checho over 4 years

    I have two jar files - jar1 and jar2. Both of them are located in C:\Eclipse projects\ and I have added the paths to both of them to the Environment Variable CLASSPATH as follows

    .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Eclipse projects\stdlib.jar;C:\Eclipse projects\algs4.jar
    

    the ".;" at the beginning were there so I left them. Then I added the jars to the project from their location C:\Eclipse projects\ and they showed up as Referenced Libraries. However, when I try to instantiate a class from the jars it does not recognize it. I am also not able to import the jar (import jar1).

    After I tried adding a lib folder in the project and I added the jars there. After, I added them as references once again (so not they appear twice in the Referenced Libraries), however, I am still not able to use the inner classes. Any help will be much appreciated.

    UPDATE: Something must be wrong on my end. None of the suggestions worked for me. Here is a video with all the steps: screencast.com/t/gC81YzCsLY0e

    RESOLUTION In my project I had a package called TestProject and it seems that those jars needed a default package. After deleting the TestProject package and using a defaultPackage everything worked correctly after adding external JARs as explained below.

  • checho
    checho over 11 years
    please see the video in the initial question
  • checho
    checho over 11 years
    please see the video in the initial question
  • mannyee
    mannyee about 9 years
    I'm only a few hours away from the exercise submission deadline... you won't believe how tremendously happy I'm to see your solution work for me as well!!!
  • Akoder
    Akoder almost 7 years
    I have lost almost half day for just importing a jar in Android studio, although the same jar was working fine yesterday. I tried your solution i.e. keeping classes directly under default package, but still you didn't mention how to import the class 'WTF' inside project.