• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does java locate package members when importing them?  RSS feed

 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to understand how java imports package members because I want to import org.apache.commons.io.FileUtils which apparently is not on my linux system.

As an example, my code imports javax.xml.parsers.DocumentBuilder without complaining. My path is just this:


I'm on a linux system. Where does linux find the code for javax.xml.parsers.DocumentBuilder to import?

I get this error when trying to compile my program:


I see that I can download Commons IO from http://commons.apache.org/proper/commons-io/download_io.cgi but I don't understand what to do once I get the library.

I've googled around trying to understand how packages work and I find lots of information but not at the nuts-and-bolts level of where to download the code to and how to reference a package when compiling my code.

Thanks.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan Lederer wrote:How does java locate package members when importing them?

One word. Classpath

Consider a class packaged com.maneesh.superstuff.Magic. This means the actual class file will be located under the directory hierarchy com/maneesh/superstuff.
The JVM will try to hunt for this directory pattern under all possible locations defined in your classpath.

Just so that there is no confusion, importing means telling the JVM your code will be making use of the imported class(es) or package(es). It does not actually merge the import class with your class
 
Stan Lederer
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, ok. I was getting tripped up into thinking that I needed to have the jar file in some "hierarchical" path although I do understand that the paths are not hierarchical. All I needed to do was drop the jar file into some directory and then "export CLASSPATH=/path/to/jarfile.jar:."

Thanks very much for your help, Maneesh.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!