1

I am trying to do "javac Classname.java" from cmd prompt, and this Classname.java requires Jfreechart libraries/jars, and runs fine if compiled from Eclipse (because project package has jars imported).

But I want to run the file from cmd prompt and its not able to show me the output. It comes with errors like: ("package doesn't exist"), how to fix it? I need the class file and also run JNI commands to create header file.

halfer
  • 19,824
  • 17
  • 99
  • 186
David Prun
  • 8,203
  • 16
  • 60
  • 86

3 Answers3

3

You need to set the classpath.

You can do this in 2 ways. Either use the -classpath or -cp option:

javac -cp jar1.jar;path/to/jar2.jar Classname.java

Or, if you need it to persist, use the CLASSPATH environmental variable:

set CLASSPATH=path1;path2
javac Classname.java
Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
  • 1
    Right, but the -classpath option is the preferrable way. – asgs Sep 14 '11 at 21:34
  • @asgs, not really. Some apps have a classpath that is too big to fit in a command line, so they have to use environment variables. e.g. app servers. – Paul Grime Sep 14 '11 at 21:48
  • @Paul Grime: "-cp" *is* the way to go. In most modern versions of windows, it shouldn't be any problem to make it as long as you want and put it in a .cmd file. – paulsm4 Sep 14 '11 at 23:30
  • @Paul Grime Classpath being too big is never a problem, like paulsm4 said. The actual problem is setting the $classpath environment variable when you intend to run more than one application each of which requiring a different set of libraries, which might result in a clash. Also note -classpath is the more consistenly supported option than just -cp. – asgs Sep 15 '11 at 06:16
3

If you have already managed to run your code in Eclipse, then Eclipse can help you.

In the "Debug" view, you should have something like this remaining after you have run your code:

Screenshot of Debug view

If you right-click the bottom "terminated" text and select "Properties", you will get something like this:

enter image description here

You can copy the command line content and use that to run your app from the command line, or use it to set the classpath as the other answers have advised.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58
0

You just need to add the directory paths and/or .jar libraries to your "-classpath" command-line argument.

Depending on how many libraries you've got, you might well wind up with a .sh script (Linux) or .cmd file (windows) that looks something like this:

http://ubuntuforums.org/showthread.php?t=230258

java -cp jts.jar:jcommon-1.0.0.jar:jfreechart-1.0.0.jar:jhall.jar:other.jar:rss.jar -Xmx256M jclient.LoginFrame .

If you're on Windows, you'd use ";" as a separator (instead of *nix ":").

'Hope that helps!

paulsm4
  • 114,292
  • 17
  • 138
  • 190