I am just jumping back to Java after a 4 years break.
Here's my query: I am playing around with a simple class of mine. It is compiled for the package My.Pckg and is called MyClass. I now place the .class file in the lib\ext dir of the jre (including directory structure).
Now I have another class (AnotherClass) that instantiates an instance of MyClass ( MyClass mc = new MyClass(); ). I import the package "import My.Pckg.*;" in the java file.
But ... when I compile AnotherClass.java I reaceive the message that the passsage does not exist.
OK, it works if I pass the lib\ext path to "javac" via the "-classpath" option. But shouldn't the java-compiler *know* where the ext dir is? Or are my system variables broken, but then nothing would work?
But shouldn't the java-compiler *know* where the ext dir is? Or are my system variables broken, but then nothing would work?
What am I doing wrong?
You need to separate your code from sun supplied library. /lib is exclusively for sun libraries. classpath is the means to separate your classes.
In future if you need to upgrade your jre what will happen to your code. Putting in a separate directory under lib doesn't mean that you are separating your code from jre - here you are still relying on sun supplied directory structure. [ December 22, 2005: Message edited by: jiju ka ]
Jiju is right. Don't put your code in the extensions directory. If you do, then what will happen is this: first you will forget you did that. Then you will download a new version of the JRE. At this point you have a new extensions directory that doesn't contain your code and you will be baffled why things don't work any more.
However the answer to your question is that your class was in a package. The rules for classpath say that the directory containing the class's package must be in the classpath, not the directory containing the class itself.
Joined: Mar 21, 2001
Thanks Jiju, thanks Paul,
of course you are both correct about updating the jre and not putting your own classes into the jre\lib\ext dir.
BUT when learning Java this is what most books advice you to do, to just test your classes quickly.
I know that the classpath must only contain the dir that the package is in and not the dirs of the package itself. I can get everything to work inside AND outside the lib\ext dir with that info.
BUT the spec and all java beginner books say it is possible to just plonk the classes (without jars) in the lib\ext dir. I have seen that others have had this problem before me and that others have not been able to reproduce this behaviour either.
I am just befuddled about this whole business with the lib\ext dir. BUT I will now keep my hands off it and ONLY work with the classpath option and plonk my classes where I won't forget them.