aspose file tools*
The moose likes Beginning Java and the fly likes same classname, different package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "same classname, different package" Watch "same classname, different package" New topic
Author

same classname, different package

Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
I wrote two programs with the same classname and saved them in different directories:
(1)current-directory/p1-->A.java

(2)current-directory/p2-->A.java

My question is that how to use the 2 classes in the program "P.java"?
current-directory-->P.java

I tried many ways and I got the error messages like: "cannot resolve symbol" or "reference to A is ambiguous" or "p1.A is already defined in a single-type import". What did I miss? Please help, thanks a lot.
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Your code, package and directory structure are fine. My guess is you do not have the current directory in your CLASSPATH. Try adding it then recompile.
mei l.h.
Greenhorn

Joined: Nov 12, 2002
Posts: 11
I agree with Brown


MM
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
If you use fully qualified classnames (which you have to), you don't need the import statements. Try removing them.
Did that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
Thanks for all your replies.
I set "classpath=.", remove the first two import lines in P.java.

And I STILL got the following error messages:

I tried to use javac -classpath" and got the same error messages.
Why? I thought I understood what the package is and how to use it, but now I'm confused!
[ November 13, 2002: Message edited by: Claire Yang ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
What directory is the file "A.class" in?
It should be in the "p1" subdirectory of the directory that contains "P.java".


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Maybe it is wrong, but you need to write the CLASSPATH in upper case letters. So please set in your environment the CLASSPATH=.;
If you do this, then you don't need javac -classpath, you simply need javac.
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
Thanks, Ron & Rene.
I changed "classpath=." to "CLASSPATH=." and got the same error messages.
The structure of all the files and classes is drawn as the following:

What's wrong with the programs?
[ November 14, 2002: Message edited by: Claire Yang ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Nothing is wrong with it. The files are exactly where they should be, and the program runs fine on my system.
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
Ron, thanks a lot for testing my programs. I finally found the "suspect" that caused all these problems. For testing the programs, I wrote another file "p1.java" with classname P1: ( p1.java is in the same directory of P.java)

"p1.java" can run completely but cause "P.java" compile error. If I changed "p1.java" to "P1.java", then each program runs fine. Since "p" & "P" are not the same to java, how come "p1.java" will affect "P.java"? Could you explain that to me? Or I still miss something? But I'm sure for one thing: not to use the ambiguous filename & classname to test programs!
[ November 14, 2002: Message edited by: Claire Yang ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The class and the file MUST have exactly the same name, including the same capitalization.
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
But I didn't declare the class with public.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
That makes no difference. The class name and file name must match in spelling and capitalization, always. The same goes for package names and directory names.
Claire Yang
Ranch Hand

Joined: Aug 30, 2002
Posts: 57
I understand now, thanks a lot, Ron.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11477
    
  16

Palak Agarwal,
Your post was moved to a new topic.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: same classname, different package