• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package problems

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic