7

I did this before:

CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/tools.jar:/home/phoenies/jdk1.6.0_17/lib/dt.jar"

But today an article says I should do this:

CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib"

If I do so, will it search all the jar files in lib? So it's probably a shorter way?

1

5 Answers 5

14

Since you are using JDK6, you can use classpath wildcards: CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/*" will match all JARS inside lib/

Check out http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html there's a section called "Understanding class path wildcards"

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

2 Comments

very interesting. +1. However I would argue against the use of 'should' as there is no explicit ordering of the loaded files, so there could be unexpected behavior if you have multiple jars containing different versions of the same class in your directory.
You are right, in that case explicit jar loading is better. When you have a group of independent jars to load using wildcards is a good solution. Even better to have an ant script as duffymo suggested.
2

I think having a CLASSPATH environment variable is wrong for all but the easiest of "Hello, World" tutorials.

The right way is to set the CLASSPATH for every project when you compile and run. Every project is likely to be different, so this makes perfect sense.

IDEs ignore CLASSPATH environment settings; so do all Java EE app servers. It's a relic of Java 1.0. I don't have CLASSPATH set on any machine that I work on.

Learn to script it for the command line. Or use Ant. You'll be glad you did.

1 Comment

Oh, I thought it's just a convention.
1

Yes, it will search all jar files in lib if you do it the second way. It's pretty odd to see class path being set as specifically as in the first one. I suppose on a server where you wanted to be sure what jars were being loaded, that might be one way to restrict them, but you might run into issues with how long it can be if you had several jars.

2 Comments

O Really? The definition of a directory in classpath always used to be to find naked .class files.
Wrong. You have to list JARs individually.
0

Jar files need to be specified by name in the Classpath variable. One thing to note is that the commandline -classpath param is more versatile than the environment variable, as it allows you to set a classpath per application.

Comments

0

In Java 1.6+ you can set the classpath to a directory followed by /* to load all JAR files in that directory. Not just the directory name though - that's for loading class files in that directory and subdirectories.

Comments

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.