views:

72

answers:

4

Hello,

I would like to use an external library (e.g. google's guava) for my java program. I use eclipse IDE, so I downloaded guava's jar (and sourced) and followed http://www.vogella.de/articles/Eclipse/article.html#classpath_jar to add it to eclipse and to the builpath of my project. This works fine: I can run the program from eclipse and from the runnable jar I export from eclipse, but I get an error when I try to run directly from the bin/ dir, as I used to do before:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Joiner

What should I do?

thanks, Dave

+1  A: 

If you're running the class file directly from the project bin directory then you may have to specify the classpath manually: C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses MyClassHere

Poindexter
I can't get it to work, now it can't even find my main class. Where exactly should I point the class path? Is the root of all classes ok? The bin of each package?
David B
You point the classpath at the guava library. You go into your bin directory as usual and run with the same command, just adding the `-classpath C:\libs\guava.jar` to it
Poindexter
thank you Poindexter
David B
actually, this doe snot work for me. From the bin/, I can run java myPackage.myClass but once I add -classpath with the external jar myPackage.myClass is not found. I also tried adding myPackage.myClass path to the cp (separated by ';') but this didn't help.
David B
A: 

To run the program on console as precise as possible with when you run it from Eclipse, you need to run it from the root directory of the project (not from bin) and don't forget to mention the classpath (http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/tooldocs/windows/classpath.html)

So for example on root you will run:

java -classpath lib/guava.jar;bin packageName.className

nanda
thank you nanda
David B
A: 

Have you tried java -cp guava.jar ... ?

Sanjay Manohar
A: 

Youll have to tell Java where to find the library:

java -cp <path-to-lib-jar>;myJar.jar my.package.MyMainClass

or if you wanna use a jar file you can set the library path in the MANIFEST check here for an explanation.

smeg4brains
You can't use -classpath (or -cp) with the -jar option. It will be ignored.
erickson
right, thx for the comment, i corrected the example
smeg4brains