• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

import mechanism

 
Ranch Hand
Posts: 44
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 44
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76887
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 44
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76887
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 44
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Campbell! Really appreciate it!
 
Campbell Ritchie
Marshal
Posts: 76887
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Phew! He understood it!

You’re welcome
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic