Win a copy of The Java Performance Companion this week in the Performance forum!
    Bookmark Topic Watch Topic
  • New Topic

an error about import p1.*

 
Mike Lin
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi ,all,I meet a confused compile question today.
create two java source file named T1.java and T2.java in the same directory,then comiple them.
T1.java

T2.java

compile T1 first ,then compile T2.Nothing exceptions happen.
But change

to


in T2.java,a compile error happens.compile messages like these:


Yet ,what's the difference between import p1.* and import p1.T1


EDIT by mw: Broke up really long error message line in code tags.
[ October 10, 2006: Message edited by: marc weber ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Report post to moderator
Both cases had compile error on my machine.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Report post to moderator
This is a little sticky, but I'll try it.

"A single-type-import declaration imports a single type by giving its canonical name, making it available under a simple name..." (JLS - 7.5.1) So with import p1.T1; the canonical name is easily resolved, and makes T1 available under its simple name.

But a "type-import-on-demand [i.e. "wildcard"] declaration allows all accessible (�6.6) types declared in the type or package named by a canonical name to be imported as needed." (JLS - 7.5.2) So with import p1.*; the compiler needs to go looking for what you mean by the simple name T1. It starts in the current directory (denoted by "."). And indeed, there is a T1.class in the current directory (p1).

But now the compiler says...
bad class file: ./T1.class
class file contains wrong class: p1.T1


The compiler is finding T1 in the current directory (./T1). But this considered a "bad class file" because T1 is really in a package called p1, and what the compiler thinks it's found (./T1) isn't matching what that file actually contains (p1.T1).

So the compiler makes a suggestion...
Please remove or make sure it appears in the correct subdirectory of the classpath.


Question: Have you started working with "classpath" yet?
[ October 10, 2006: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please continue under the cross-post in the beginners forum, and please don't cross-post.
 
    Bookmark Topic Watch Topic
  • New Topic