File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes an error about import p1.* Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic

an error about import p1.*

Mike Lin
Ranch Hand

Joined: Oct 29, 2002
Posts: 48
Hi ,all,I meet a confused compile question today.
create two java source file named and in the same directory,then comiple them.

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


in,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 ]

SCJP1.4 <br />Best wishes!<br />中国人!
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Both cases had compile error on my machine.
marc weber

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
marc weber

Joined: Aug 31, 2004
Posts: 11343

Please continue under the cross-post in the beginners forum, and please don't cross-post.
I agree. Here's the link:
subject: an error about import p1.*
It's not a secret anymore!