This is a rather basic question related to packages but somehow I tend to forget how it is done.
I have the following hierarchy on my Win2K desktop.
D:/Documents and Settings/Administrator/Desktop/JavaTrial D:/Documents and Settings/Administrator/Desktop/JavaTrial/package1
The sub-folder package1 has a file named PClass.java which is as follows: package package1; import java.io.*;
The folder JavaTrial has a single java file named MyMainClass.java which is as follows :
So I am trying to call a class within package1 from outside it.
However, when I compile the files, PClass.java compiles without errors while compiling MyMainClass.java gives the following errors:
---------- Compile Java File ---------- MyMainClass.java:2: package package1 does not exist import package1.*; ^ MyMainClass.java:13: package package1 does not exist package1.PClass obj = new package1.PClass(); ^ MyMainClass.java:13: package package1 does not exist package1.PClass obj = new package1.PClass(); ^ 3 errors
It says package1 does not exist. Why ? Whats wrong ?
Ulf is making it more complicated than necessary. The main point is that you need to be sure that you change to the JavaTrial directory before typing any commands. You should also be sure that your system does NOT have the CLASSPATH variable set. If you do both of these, then you can compile and run your program with the following commands:
The Java compiler will first search for a file named PClass.class in order to satisfy the reference in MyMainClass.java. However, if it cannot find this file, it will look for PClass.java and compile it to create PClass.class. As long as the command prompt is at the correct directory (i.e. JavaTrial) it should be able to find the files it needs.
If you are still encountering problems, please describe how you are compiling these files. Please include which directory your command prompt is at.
Good luck and keep coding!
p.s. Once you get this working, you will see at least one more compiler error. Dani's suggestion above should fix it.
p.p.s. You also need to include a package statement at the top of PClass.java. It should look like this:
[ January 14, 2006: Message edited by: Layne Lund ] [ January 14, 2006: Message edited by: Layne Lund ]