1

I never truly understood java libraries and would love if someone could clarify my understanding:

  1. When I need a package, which doesn't come by default with java, then I google it and download the binaries. i.e. "HttpClient" then I download httpcomponents-client-4.2.2-bin.zip from apache.org
  2. I first need to import the .zip file to netbeans, and then use the import command (which normally is automatically suggested).

I still have some questions/problems.

HttpClient class seems to be part of the apache client library, but I am told to import sun.net.www.http.HttpClient why? (even if i do it doesn't solve rest of the problems)

Then trying to use HttpClient httpclient = new DefaultHttpClient(); says that DefaultHttpClient symbol cannot be found, but the library has already been added to Netbeans.

Looking at this similar example http://www.vogella.com/articles/ApacheHttpClient/article.html some of the imports don't come with the package I downloaded.

This happens pretty often to me, and sometimes I manage to fix it without really understanding. Atm I am trying to implement this answer https://stackoverflow.com/a/3325065/960086

Community
  • 1
  • 1
Juan
  • 521
  • 1
  • 11
  • 28
  • 3
    Did you not consider using Maven? :) Adding and managing dependencies is really simple. Check out this: http://maven.apache.org/ – pidabrow Jan 04 '13 at 16:05
  • 8
    you should probbly import the *.jar, not the *.zip distribution. you will find the jars inside the *.zip under the /lib subdirectory – radai Jan 04 '13 at 16:07
  • 4
    You need to learn how packages work. – SLaks Jan 04 '13 at 16:07
  • I will check out Maven, thanks for the suggestions. @radai I think thats my mistake, would you want to answers to get check? SLaks thats exactly what I am trying to do. – Juan Jan 04 '13 at 16:13
  • 5
    Using Maven is only going to hide the real answer - you need to add the jar files in the zip to the classpath of your application and you need to learn about classpaths and packages. – blank Jan 04 '13 at 16:16
  • @Juan - and deny Nick Holt the credit for writing all that below? thanks, but i'll pass :-) – radai Jan 04 '13 at 17:29
  • @Bedwyr That said, what Maven can do (once you learn to use it) is create a bundle with all the `.jar`s you need in a single directory. (With the [assembly plugin](http://maven.apache.org/plugins/maven-assembly-plugin/).) Or create a "simple" executable .jar with all dependencies in it. (With the [shade plugin](http://maven.apache.org/plugins/maven-shade-plugin/).) Or who knows what else with the third-party plugins. So while it won't solve the OP's problem without him having to learn how the whole mess works conceptually, he probably wants to learn to use Maven anyway. – millimoose Jan 04 '13 at 18:15

1 Answers1

4

OK, there's a few things you need to understand with regards to how the JVM is able to create instances of classes and I'll start at the bottom.

When an JVM starts it needs to load the classes that will be used to run the application. To do this there's 3 class-loaders in play - the bootstrap class-loader, the extensions class-loader and the system class-loader. The first 2 load classes from jar files in the $JAVA_HOME/lib and $JAVA_HOME/lib/ext respectively. They're basically what's needed to get the JVM running - don't worry about those for now. The third class-loader, the system class-loader, is what you're interested in. It's this system class-loader that uses the classpath to find the classes that you are adding to the JVM to form your application.

There's much more to class-loaders but that will do for now.

So the system class-loader uses the classpath to load classes. In its simplest form the classpath is nothing more than a reference to the directory that the Java compiler output the classes it created to. Within that directory you will find a hierarchy of directories that represent the package structure you used when you wrote classes (the package declaration at the top of the .java file used to organize your classes within the context of your application) and the .class files that contain the compiled classes. When a class is needed by the JVM, the system class-loader parses the directory structure representing the package and loads the class (or throws a ClassNotFoundException if it cannot find it).

Now, one of Java's strengths is the sheer number of 3rd party libraries available. Obviously sharing code by copying directories about isn't going to work and so Java Archive or JAR file is used. A JAR is nothing more than a zip file with a couple of standard directories plus the same directories that represent package structure and the .class files that I mentioned previously. Of course it picks up the extension .jar but any tool capable of opening a zip file will open a JAR. To use a JAR you simply add it to your classpath just as you would a directory and as with the directory, the system class-loader will parse the structure it contains to load classes as need be.

That said, you rarely download just a JAR when you decide to use a 3rd party library, as in your case with HttpClient. Obviously there's more than just the classes to any 3rd party library and most will include documentation, examples and maybe even the source code. However to use the 3rd party library the process is the same, you need to extract the JAR containing the classes you want to use and add that JAR to your classpath.

That's almost the full story - as the number of 3rd party libraries being used by typical applications grew so did the problem of managing these libraries and so Ant with Ivy and more recently Maven became popular. In addition to their build capabilities both provide a way to declare the 3rd party libraries that your application depends on and streamline the process of downloading those libraries and adding them to you classpath. But what they are doing is just the same as if you were to download libraries and add the to the classpath manually.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • A minor aside on JAR files: the "standard directories" and other metadata files are entirely optional. A `.zip` of classfiles work just as well. – millimoose Jan 04 '13 at 18:10