Hi, I'm starting with Java and have an issue with how to use the classpath variable and where to put certain library. This is probably pretty easy but it's got to the point of driving me crazy.

Now, I'd like to have my library, let's call it "mylib", under C:
So, I created a directory called "mylib" and inside it put all compilation units. All of them have the "package mylib;" line of code as the first non-commented line.

I've been told that if I want to use this library in a program runing from any other directory, I need to import it with "import mylib.*;" and change the classpath variable.

What do I have to write on the classpath variable?

  • C:
  • C:\
  • C:\mylib

Any help is really welcome.

Short version: If you refer to a class in a package, the JRE will search all the directories in the current classpath for a folder or jar with the same name as the package, then search in there for the class. So your classpath must reference the folder/directory that immediately contains your "mylib" folder.

Thanks JamesCherrill. That's what I been trying to do but apparently I'm doing something wrong.
Perhaps you can give me a hand with an example:

The class I'm referring lives in:

C:\mylib\Pr.java;

Pr.java has the line of code:
package mylib;

In my programm I wrote the:
import mylib.*;

My classpath variable has got:
C:\

And still, is not working and frustating!
So, what am I doing wrong?

Please, please

Have you compiled your Pr.java class? You should have "Pr.class" file right in C:\ or if C:\mylib is in classpath then Pr.class can be inside of C:\mylib.

Thank you IPavel.
Pr.class is inside of C:\mylib.
C:\mylib is in the classpath.
And still not working...

If Pr.class is inside of C:\mylib, the classpath should be c:\ As you have it now, its looking for mylib.classwhatever inside C:\mylib - ie 1 too many mylibs.

commented: Just what I needed. Thanks +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.