Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java "ClassNotFoundException" error

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();
    }
}
like image 423
Steve Quezadas Avatar asked May 19 '12 10:05

Steve Quezadas


3 Answers

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
like image 128
EnKrypt Avatar answered Sep 19 '22 12:09

EnKrypt


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

like image 32
Ashwinee K Jha Avatar answered Sep 20 '22 12:09

Ashwinee K Jha


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

like image 28
Attila Avatar answered Sep 22 '22 12:09

Attila