RobertDeNiro
RobertDeNiro

Reputation: 63

Java classpath library(jar)

I don't know how to compile and run Java program with another library. I read many threads about this, but none of them helped. Most of them just talking around it and stuff instead of providing a decent example. Although trying for the past week, I couldn't make it to work.

Imagine I have the following structure (~ is home directory):

Graph.java starts with:

import org.math.plot.Plot2DPanel;

public class Graph { ... }

It (org.math.plot) corresponds to .jar structure. So from what I read (if I run this from ~ directory):

javac -classpath ./jmathplot.jar Graph.java

this should work. But it doesn't. It just generates bunch of .class files from jmathplot.jar and when trying to run it I get NoClassDefFoundError. If I extract "org" folder from jmathplot.jar and put it next to Graph.java it works flawlessly (which is expected); you don't even need to specify classpath.

I think of classpath as a guidance to JVM where to look for files. Specifying jmathplot.jar instructs JVM to look for /org/math/plot structure in jmathplot.jar.

Where am I wrong? Could someone provide me a correct way to compile and run this Graph.java? I would greatly appreciate it. Thanks!

Upvotes: 2

Views: 1530

Answers (3)

gilad hoch
gilad hoch

Reputation: 2866

after you get all your .class files compile into /path/to/some/dir, you can run it with:

java -cp "~/jmathplot.jar;/path/to/some/dir/*" com.package.YourMainClass

Upvotes: 1

LuGo
LuGo

Reputation: 5045

Try with a forward slash and without the dot. Like this: javac -cp \jmathplot.jar Graph.java

Upvotes: 0

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

Why didn't you use an IDE (Eclipse, Netbeans...) which can do the work for you?

Upvotes: 0

Related Questions