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

information regarding the import...

 
Siddharth Ramavat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
larry lan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your classpath,the JDK can do what you say any thing
I think so.
 
K Riaz
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic