• Post Reply Bookmark Topic Watch Topic
  • New Topic

import mechanism  RSS feed

 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please tell me the mechanism behind import statement? I know that the statement "import javax.swing.JLabel;" means that we are using JLabel class in our program, but where does the compiler look for this JLabel.class file? I thought that it will be in inside swing folder or javax folder somewhere in jdk7 folder. But I didn't find any. Can anyone explain to me how the compiler does this? Any explanation would be appreciated. Thanks
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the java system classes are packaged in jar file named rt.jar which is located in your java installation directory. also the source of the class files will be in src.zip. when you run the java program the java runtime automatically scans pre-defined folders for these system files. this is the reason you don't specify path for the system classes in your classpath.
 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:the java system classes are packaged in jar file named rt.jar which is located in your java installation directory. also the source of the class files will be in src.zip.


Thanks for this explanation! That really answers my question.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the compiler uses the import statements. It looks first in your current package, then in rt.jar for the java.lang.package, then later for other packages in rt.jar or the rest of your folder structure or your classpath.
This means you can get serious confusion if you create your own classes with the same name as classes in the java.lang package.
 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer.

Campbell Ritchie wrote:It looks first in your current package, then in rt.jar for the java.lang.package, then later for other packages in rt.jar or the rest of your folder structure or your classpath.


Which folders did you mean by the rest of my structure folder?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folder structure, not structure folder.

You will have a folder, directory or whatever which contains your .java and your .class files. That folder will be in other folders, depending on the package names and structure you have chosen. That is what I meant. You know when you use a file explorer program, on Windows® Windows Explorer, for example, you can see a tree‑like structure when you click on the ⊞ symbols. That wil correspond to the package names and your .class files will sit in a particular directory. The compiler will look through that tree to find the classes you have imported.

If you don’t use package names, which is usual for beginners, you get a very simple folder structure: one folder!
 
Rod Taylor
Greenhorn
Posts: 25
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Campbell! Really appreciate it!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phew! He understood it!

You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!