简体   繁体   中英

Java CLASSPATH Not working?

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.

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

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