21

Firstly this is a very n00b question. But being a junior dev I've never needed to import and work with other Java Frameworks. The standard library was always good enough for me to write the classes I needed to write.

But now getting exposed to more "advanced" concepts, I need to start working with external frameworks, e.g. JSON for Java, Apache's HttpClient for java and so on. And I'm looking for a basic understanding on how this works and how to go about importing these libraries so you can start working with the classes...

So my initial understanding is that each of these fraemworks will provide you with a .jar file that contains all the classes for the framework. Which you then import into your project and lo and behold you'll be able to use the classes/library in your project by just importing it e.g. 'import org.json.*;'

Is the understanding correct?

2 Answers2

7

Correct.

You just add the libraries to your classpath and are now able to use classes from these libs. How you add the libs to your classpath depends on your actual development environment. If you use Apache Maven for example, you just have to define the dependencies (libs) in your projects pom.xml and Maven downloads them automatically for you.

hth,
- martin

Martin Höller
  • 2,714
  • 26
  • 44
  • Thanks for pointing me at Maven, looks really helpful. I've only ever heard good things about it, maybe it's about time to start using it. –  Jul 23 '13 at 09:53
6

EDIT: The following only applies if you are not using automated build-tools like Maven or Ivy

Yes this is correct. To use a third party .jar file, download and place it in a convenient location (either system-wide or project-specific depending on your needs) and then include it in your classpath.

When executing from the command line do:

java -cp /path/to/library:. path.to.main

The :. is necessary so that the JVM will find your main method.

In an IDE you should be able to include the library in your classpath via the options menu.

Then you can just use the third party library like any other:

import name.of.library.class;
//Do something
diestl
  • 2,020
  • 4
  • 23
  • 37
  • Thanks! 2 Questions though, firstly when you do add these jars to your CLASSPATH then surely when you eventually compile your project and distribute it, it will fail to run on other systems since they also then should have the certain library in their CLASSPATH? And secondly, do 3rd party frameworks always provide their libraries in the form of jars or would they sometimes also provide straight .java files ? –  Jul 23 '13 at 09:48
  • 1
    ad 1) yes, others will need to include the libraries in their CLASSPATH, too. Maven provides the assembly plugin to cope with this problem. See http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven for more details. ad 2) A JAR is just a ZIP archive. Usually it contains compiled classes (.class files) but it can also contain sources (.java files). OpenSource projects do provide source and binary JARs for their projects most of the time. You can search Maven's Central Repository http://search.maven.org/ for example. – Martin Höller Jul 23 '13 at 10:01
  • @MartinHöller - Thanks again, all of this is making a lot more sense now. Basically what I'm asking is (2) What type of files should be in your CLASSPATH? the .java files, the .class files, or the .jar file (even if this is just a zip archive, what is inside that archive? .java or .class files? ) –  Jul 23 '13 at 10:08
  • I'm asking this because I got the JSON library for java and what they give you is just a folder filled with .java files so I'm just trying to find out what I should do with this. Add the .java files to the CLASSPATH, compile them first to .Class files then add them.. etc.. –  Jul 23 '13 at 10:10
  • You should be able to add the .java files (or the .jar containing them) to your classpath and compile as normal. – diestl Jul 23 '13 at 10:28