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

Importing Packages & Classpath

Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
I am working in C:/Myjava/

I have a package MyPack and a compiled public class Balance in it.

I go back to MyJava and create a NewClass that imports MyPack.* and calls a method on a Balance-type object reference. (All members in Balance are public)

NewClass does not compile if I write the import statement as
import MyPack.*;
returns error:

bad class file c:/MyJava/MyPack/Balance.class
class file contains wrong class MyPack.Balance
Please remove or make sure it appears in the correct subdirectory of classpath: Balance b=new Balance ("The balance is "+22000);

The error pointer shows at the type Balance in the above line

But if I write the import statement as

import MyPack.Balance;

everything works fine!!! ???

My Path and classpath respectively are as follows:

c:\j2sdk1.4.2\bin;c:\j2sdk1.4.2\jre\bin;c:\MyJava;c:\MyJava\Mypack
c:\j2sdk1.4.2\lib;c:\j2sdk1.4.2\jre\lib;c:\MyJava;c:\MyJava\MyPack

What is the problem?

Help help help please anybody. I am struck with such a simple program




Sue

SCJP 1.2
SCWCD 1.4
SCBCD 5.0
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Why be stuck? It's better to import the specific class used rather than the entire package anyway. Use


if it works fine, it's the better way to do it anyway. Oh and by the way did you include the package statement first in your other class, such as.


one more thing to consider, I don't think it's standard to capitalize package names. In fact you should read a little on naming packages, they usually go something like this

[ November 04, 2004: Message edited by: Hentay Duke ]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
I cannot be entirely sure here but what I think you are experienceing is the the javac translator finding another method "Balance" and declaring it to be the one you THINK you are invoking. I think you are susceptable to this type of error because you have used the wildcard (*) in your import statement and this gives the translator free reign to find the first "Balance" method and use that (even if it is in another package). Because you are not fully qualifing your method invokaction.


It's kind of a long shot I know. I suppose you could test it by naming your Balance method to something you know will be unique (e.g. you name or something like that).
Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
Hentay, what if I have 3 or 4 classes in MyPack and want to import all? Then shouldn't I be using * I do have the package statement and I got this example right out of the complete reference so I don't thing the naming should cause a trouble here. But you are right about it though. Thanks

And Jeff right now Balance is the only class in MyPack. I had another class in the package, with an intent to use it later in the pgm, but I deleted everything else in MyPack just to clear this "Balance" thing out first
[ November 04, 2004: Message edited by: Sue Mi ]
Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61
hi,

where do you have the source file of Balance?? Ensure, it doesn't get compiled again when you try to compile the NewClass.... i.e... remove Balance.java from MyJava folder...

Uma...
Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
Uma,

I have done that too, there is nothiing else other than Balance.class in MyPack
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by NightWalk MI:

My Path and classpath respectively are as follows:

c:\j2sdk1.4.2\bin;c:\j2sdk1.4.2\jre\bin;c:\MyJava;c:\MyJava\Mypack
c:\j2sdk1.4.2\lib;c:\j2sdk1.4.2\jre\lib;c:\MyJava;c:\MyJava\MyPack

This might be part of the cause of the problem. You don't need c:\MyJava or c:\MyJava\MyPack in the PATH. The PATH is used to find executable files, such as javac.exe and java.exe when you are not in the same directory as they are.

Also, you should have c:\MyJava in the CLASSPATH, but not c:\MyJava\MyPack. The JVM uses the classpath as the base for finding classes. It then searches for packages as subdirectories from that base. I suspecte that the JVM is getting confused since it can find the Balance class through two different entries in the CLASSPATH, and apparently it is choosing the wrong one (c:\MyJava\MyPack).

Anyway, try removing c:\MyJava\MyPack from the CLASSPATH to see if that fixes your issues with "import MyPack.*".

With that said, I agree whole-heartedly with everyone else. The preferred method is to import only the specific classes you wish to use rather than all of the classes in a package (e.g. with import package.*).

HTH

Layne


Java API Documentation
The Java Tutorial
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
"what if I have 3 or 4 classes in MyPack and want to import all? Then shouldn't I be using *"

I'd still import the 3 or 4 classes individually. Generally speaking there are going to be more classes than that in any package you come across (except perhaps your own when you're starting out) and it would be very rare indeed to be using every single class in a package. The naming convention certainly isn't going to be your problem, I was just pointing it out to make you aware.
Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
Layne, I tried out your suggestions too again. Infact there was no c:/myjava/mypack in classpath initially. I just tried to check if it will work this way. Since you all say the common practice is to import individual classes, I guess I'll just stick with that for now. But the question still bugs me. When everything is okay, shouldn't it work irrespective of whether I import an individual class or a whole package?

Anyways thanks everyone of you for your help.
 
 
subject: Importing Packages & Classpath