Question

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();
    }
}
Was it helpful?

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

OTHER TIPS

suppose that, the directory of your program is

$HOME

$HOME/lib/*.jar

you can write a script like:

for file in "$HOME/lib/*.jar"
    do
        if [ -f $file ]
        then 
            CLASSPATH=$CLASSPATH:$file
        else
            echo ignore $file
        fi
done

java  -cp $CLASSPATH FacebookProg
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg

This should work fine if you compiled the FacebookProg.class in same directory. You can try java -cp /usr/share/java/scribe-1.3.0.jar:/locationOfFacebookProg.class directory/ FacebookProg

This

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

means you are running the FacebookProg class, not compiling it.

If you leave the -cp ... out, you are leaving the vital classpath out, so the JVM cannot find the classes FacebookProg requires.

To compile, you need

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

(note the javac, instead of java to invoke the compiler)

To run, you already know how to.

Also, you have errors in the follwoing lines:

export CLASSPATH="/usr/share/java"; java FacebookProf     
export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg

The first misspells FacebookProg and does not have the required jar on the classpath, the second has the wrong path to the jar. Try

export CLASSPATH="/usr/share/java/scribe-1.3.0.jar"; java FacebookProg     

Also, make sure the jar is indeed located at /usr/share/java/scribe-1.3.0.jar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top