1

I am trying to use other jars for a program I am writing.

I installed all the required files and added them to CLASSPATH, but Java doesn't recognize the packages.

I put semicolons in between the jar locations but Java doesn't recognize the packages from the jar. Why does that happen?

For example, my classpath looks like:

.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Users\JOE\Downloads\easymock-3.2\easymock-3.2\easymock-3.2.jar;C:\Users\JOE\Downloads\cglib-3.1.jar;C:\Users\JOE\Downloads\objenesis-2.1-bin\objenesis-2.1.jar

but if I try to import package org.easymock.EasyMock for example, the package is not recognized.

3
  • Have you checked whether the class org.easymock.EasyMock exists inside easymock-3.2\easymock-3.2.jar or not ? Commented Mar 17, 2014 at 20:30
  • 2
    You should not be using CLASSPATH any more. Provide the libraries through the -cp parameter when starting your Java application. Commented Mar 17, 2014 at 20:33
  • I was changing the environment variable CLASSPATH. What else should I do? Commented Mar 20, 2014 at 1:29

2 Answers 2

1

If your trying to load dependencies from external jars, specify the paths using -classpath (or) -cp command line argument. It's not ideal to change the CLASSPATH environment variable for each and every program that you execute.

The default ClassPath for java program is dot (.) which means current directory

Remember that when you're using -cp/-classpath arguments, they'll override the default classpath settings, so you should explicitly add the default path as well like below.

On Windows

javac -cp pathToYourJar Main.java

while executing don't forget to add your current directory

java -cp .;pathToYourJar Main

To make things easier, I'd recommend to use an IDE like Eclipse/NetBeans/IntellijIDEA. If your already using Eclipse, add the jars to your Project's Build Path

Right Click on Project -- Properties -- Java Build Path -- Libraries -- Add External JARs

Sign up to request clarification or add additional context in comments.

1 Comment

I'm writing this comment after trying this but it didn't work. I've a project named Helloworld which consist only 1 java file, Helloworld/src/com/firstpackage/Test.java (windows 10. intellij idea). I don't have CLASSPATH environmental variable, i want to set classpath specifically for this project. running java com.firstpackage.Test inside Helloworld directory doesn't work and neither does the command java -classpath C:\Users\matuagkeetarp\IdeaProjects\Helloworld\src\com\firstpackage Test.java set classpath variable. Can you help?
0

I could not use CLASSPATH neither but found another solution to use my own library, using Bluej. I added the jar file using Tools->preferences->user libraries from config. The class is automatically used in all projects

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.