wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes information regarding the import... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "information regarding the import..." Watch "information regarding the import..." New topic
Author

information regarding the import...

Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Hello,

I just want to know that if i write ,

import java.awt.*;
import javax.swing.*;....etc..

What will happen? I mean when i write import to my java program, it will import the package swing & awt as shown above in example...

Where the exactly this package are located? I mean where the java compiler will look out for this package....?


Waiting for your reply,

Thanks in advance,

Siddharth
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The compiler looks for imported classes in any directory or jar listed in your classpath. The examples you gave ship with the JDK so you don't have to add anything to the classpath for those. If you import from other packages, say an open source project you download or even your own projects, you may have to add jars and directories to the classpath.

Classpaths seem to confuse nearly everybody at first. Feel free to post back if that opened more questions than it answered.

As a style point, many people prefer to import individual classes rather than using the * star. It's a little more work but provides good documentation of just what classes you are using and help avoid surprises where two imports might give ambiguous paths to the same classname.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
larry lan
Greenhorn

Joined: Mar 16, 2005
Posts: 2
in your classpath,the JDK can do what you say any thing
I think so.
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Originally posted by Stan James:
It's a little more work but provides good documentation of just what classes you are using and help avoid surprises where two imports might give ambiguous paths to the same classname.


That was exactly the problem I had recently. Two different packages - same class names imported. It took me a long time to realise that Java didn't know which class to use. In the end, I used the actual path of the class from the JDk in the code itself, which makes it easier to read if you are using it only once in a long piece of code.
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thanks for your reply,

But I am still confused with the term "CLASSPATH". I mean i have set my path to "c:\jdk1.3\bin". But the i have set my classpath to that particular folder where i have saved my .java file...

If u r saying that compiler will look out the .jar in you classpath but there isn't any .jar file in my classpath..

I am agree with your answer when i am working with servlet that if i want to compile servlet then i have to specify the classpath of servlet.jar..

But if it is a normal .java program then what is the need of classpath...

Thank You,
Siddharth
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Siddharth,

Don't get PATH and CLASSPATH confused. The PATH environment variable is a list of directories (or folders) that the operating systen searches when it is instructed to execute a program.

The CLASSPATH is list of folders that Java uses to search for classes that it may need to load when running a program. In your case, you say that your .java file is in a folder that is specified on your classpath - that's fine. If you used the default settings when compiling your .java file, then your .class file will also reside in this same folder. If you run your program with your current directory set to this folder, your program should run fine.

Now, change your current directory to something different and try and run your program - it should still run as Java will look for your class and should find it in the folder you specified in your classpath.

Regards,
JD
[ March 17, 2005: Message edited by: John Dell'Oso ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: information regarding the import...