aspose file tools*
The moose likes Beginning Java and the fly likes Package problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package problems" Watch "Package problems" New topic
Author

Package problems

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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 ?

Please help.
Thanks


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
I ve tried under eclipse and i didn't have any problems other than the constructor in PClass is not visible since it's in package scope

Dan


Si altas son las torres, el valor es alto - Alberti
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
Yes, the constructor needs to be declared public in order to be accessible by MyMainClass.

The problem is one of the classpath, which Eclipse handles differently from the command line tools which it seems the poster uses. Try the following:

- Open a command prompt in the JavaTrial directory.
- Type "javac *.java package1/*.java" to compile
- Type "java -classpath . package1.MyMainClass" to run

If you want to compile just MyMainClass, type "javac -classpath . package1/*.java".


Ping & DNS - my free Android networking tools app
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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!

Layne

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 ]

Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package problems