java "ClassNotFoundException" error

classnotfoundexception, classpath, java, scribe

Solution

The classpath has to point to BOTH the directory of the external library you are using AND the class you are trying to run itself. Try this:

Windows:

java -cp .;/usr/share/java/scribe-1.3.0.jar FacebookProg

Linux:

java -cp .:/usr/share/java/scribe-1.3.0.jar FacebookProg

By the way , to compile it you should have run this:

javac -cp /usr/share/java/scribe-1.3.0.jar FacebookProg

Problem

I am new to java programming and I am getting the much-maligned error "ClassNotFoundException" error. The strange thing is is that it compiles fine: ``` java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg ``` But when I try to run it, I get the following error: ``` steve@steve-ThinkPad-T61:~/facebook$ java FacebookProg Exception in thread "main" java.lang.NoClassDefFoundError: org/scribe/builder/ServiceBuilder at FacebookProg.main(FacebookProg.java:8) Caused by: java.lang.ClassNotFoundException: org.scribe.builder.ServiceBuilder at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 1 more ``` I checked online and it seems that java can't find the library at runtime that it was able to find at compile time. So tried the following variations: ``` java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg java -cp /usr/share/java/ FacebookProg export CLASSPATH="/usr/share/java"; java FacebookProf export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg ``` I checked several places on StackOverflow and google and still can't figure out why. I'm new to java, so there's probably a simple solution, but I can't find it. I am using Sun Java 1.6 64-bit on Ubuntu 11.04. The scribe-1.3.0.jar file is in "/usr/share/java" which, I believe, is the canonical place to put java packages. The barebones code is here (in case it matters): ``` import org.scribe.builder.*; import org.scribe.builder.api.*; import org.scribe.oauth.*; public class FacebookProg { public static void main (String args[]) { OAuthService service = new ServiceBuilder() .provider(FacebookApi.class) .apiKey("blah_blah") .apiSecret("blah_blah") .build(); } } ```

Original source