• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

same classname, different package

 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Brown
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use fully qualified classnames (which you have to), you don't need the import statements. Try removing them.
Did that help?
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What directory is the file "A.class" in?
It should be in the "p1" subdirectory of the directory that contains "P.java".
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class and the file MUST have exactly the same name, including the same capitalization.
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I didn't declare the class with public.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand now, thanks a lot, Ron.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Palak Agarwal,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic