tags:

views:

822

answers:

5

I'm trying to use a open source java library to visualize nodes and edges in a graph, but I'm completely lost. Up until now I've never needed anything more than the default libraries, and everything online seems to assume I obviously know how to use a new library...

I have a bunch of jar files in a folder. Clicking on SOME of the jar files makes java swing windows pop open with graphs displayed. Clicking other jar files does nothing. I saw a few references to opening jar files...there doesn't seem to be an option to do this (i'm on windows, and it lets me open it with Java or with Firefox, both get me the same result). It looks like Eclipse references a few jar files in its projects, but I can't figure out where it's getting them from. If I figured THAT out, would I just stick the jar files in there with the other ones, or would that still not work?

And if I ever DO figure out how to use these files, does that mean that I have to include them if I transfer my java project to another computer? How would I go about doing that?

I'm starting to think reinventing the wheel might be worth it, just so that I can avoid having to do all this mess ^_^;;

+7  A: 

I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. Figuring out the classpath can be confusing, but you can just set it when you start your jvm. Your IDE may have options for it, too.

Most java problems are classpath problems.

Sam Hoice
+1 for "most java problems are classpath problems"...
rmeador
*ponder* Okay, I'll start an epic quest to figure out how to find out/set my class path.
Jenny
+another 1 for "most java problems are classpath problems." Words of great wisdom.
Carl Manaster
Yeah, I think I read that on stackoverflow.com :)
Sam Hoice
+2  A: 

Have you included those libraries in your classpath?

If you are using eclipse, you could

Project - > properties -> Java build path ->addJar.

And the Jar file should be placed in a directory inside your workspace (lib/ for example)

If you have to take your project to another computer, you could take these steps

  1. Before doing anything, export your project (as a Jar file, for example).
  2. Save it into your favorite drive (cd / usb drive/ diskette/ tape).
  3. On "the other" computer, you can import this project into your workspace
Tom
Okay, at the very least I can follow these steps ^_^;; I feel like a noob even though I've used java for years. Now that the jar files have been added to the eclipse project, I can just use the code as i would standard library stuff? Do I need import statements?
Jenny
import statements are a way of reducing ambiguety between classes with the same name but different packages. They are not like C's #include .Importing is not strictly neccesary, but its always better to use class names, and not fully qualified names when possible.
Tom
Since you're using Eclipse, the import statements come along almost automatically. As soon as you try to use a class from the external lib, and use control-space for name completion, it will add the import. Or if you typed the whole thing in, control-1 (quickfix) will offer to add the import.
Carl Manaster
Thanks! This has been really helpful!
Jenny
@Carl:Dont forget about ctrl + shift + o.
Tom
A: 

You use it by including it in the classpath of your java application, that way you can reference it from your code. Here is a starter document. The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). It is definitely a little complicated, but it is very worth knowing.

Yishai
Thanks for the reference! I have some vague nightmare memories about setting class paths years ago. It's kind of funny how simple Java can be until you actually want to DO something with it.
Jenny
A: 

You should have documentation for these Jars. Some sounds like examples, but one must be the core graph modelling and rendering Jar. Hopefully the examples have source included.

Just add that Jar to your project in Eclipse (e.g., in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. You can also use Eclipse to look inside the Jar file.

Unless there is no alternative, it probably isn't worth using a load of third party code that isn't documented at least on the API level, and without any source examples definitely not.

JeeBee
+1  A: 

In Eclipse, you need to add libraries to the project build path.

In general, you need to provide dependencies via the classpath mechanisms at compile time and runtime. The precise mechanisms vary, but, for example, if you used the javac compiler, you would provide your libraries on the command line:

javac -classpath C:\dir\lib1.jar;C:\dir\lib2.jar foo/MyClass.java

These dependencies would also be required to invoke the app:

java -classpath C:\dir\lib1.jar;C:\dir\lib2.jar;. foo.MyClass

This page gives some good info, though googling for the term "classpath" should provide alternative sources.

McDowell