java "ClassNotFoundException" error
Asked Answered
H

4

6

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();
    }
}
H answered 19/5, 2012 at 10:45 Comment(1)
I'm a total Java noob, and was getting mixed up between options for the javac and java commands. My issue was that I was using the -cp flag on the compile, but not on the execute. My code worked fine when I used the option on both: javac -cp .:/my/full/path/myjar.jar test.java; java -cp .:/my/full/path/myjar.jar testAirfoil
W
10

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
Wakeful answered 19/5, 2012 at 10:55 Comment(2)
Yes, this worked for me. Although there should be a beginning slash mark in front of "usr/". Here: java -cp .:/usr/share/java/scribe-1.3.0.jar FacebookProgH
You're welcome. I can tell you from a programmer's eye that learning through experience is far more valuable than knowledge gained from books and tutorials. I'm glad it came to use today. As for the slash before usr , well i'm not so familiar with the linux directory structure , so i'm sorry for that.Wakeful
A
1

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

Amieeamiel answered 19/5, 2012 at 10:50 Comment(1)
This answer deserves more upvotes.Airfoil
O
1
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

Obrian answered 19/5, 2012 at 10:54 Comment(0)
C
0

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
Castilian answered 19/5, 2012 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.