How to use class from JAR in eclipse

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

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

".;" at the beginning were there, so I left them. Then I added jars to the project from their project location C: \ Eclipse \, and they appeared as reference libraries. However, when I try to create an instance of a class from cans, it does not recognize it. I also can not import the jar (import jar1).

After I tried to add the lib folder to the project, I added banks there. After that, I added them again as references (so that they do not appear twice in reference libraries), however I still cannot use inner classes. Any help would be greatly appreciated.

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

SOLUTION In my project, I had a package called TestProject, and it seems that these banks need a default package. After removing the TestProject package and using defaultPackage, everything worked correctly after adding external JARs, as described below.

+4
source share
4 answers

In eclipse, right-click on the project-> Propeties-> Java Build Path-> Add External JARs (add a JAR if the jar is inside the project folder), and then select the jar file. Now you can use the inner classes of the added jars. Eclipse imports them when you start using them.

+5
source

I have the same problem as you have today, and not a single answer from the Internet can solve it. However, I fixed it finally.

In fact, there is nothing wrong with setting up, importing these banks correctly through the "Add External JARs". The real problem is the location / package of your java code. I found that you should put your .java file in the default package. For example, you will get errors if you put your Java code in a package, for example com.xxx.yyy.ccc, below is an image that shows the correct location / package that you should use (see WTF.java). After that you can start the program.

enter image description here

However, here's how I fixed my problem, I'm not sure if this could work for everyone.

+7
source

Why don't you use these two JAR files: 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 will no longer be able to access 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 cannot 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 accept Princeton COS 226 or Coursera, Algorithms, part I or II, you should use the default package check for our libraries to facilitate evaluation.

Showing my test success: Showing my test success

+5
source

If you have a folder with JAR files in the project:

  • Right-click on the project> Build Path> Configure Build Path;
  • On the Libraries tab, click Add JAR, locate and select the JAR files you want to use.

If you have JAR files elsewhere outside the project:

  • Right-click on the project> Build Path> Configure Build Path;
  • On the Libraries tab, click Add External JARs, locate and select the JAR files you want to use.
+4
source

All Articles