Abhishek Sagar
Abhishek Sagar

Reputation: 1326

Runnig jar dependant java class file

java.lang.NoClassDefFoundError

I have a java program which is dependant on two jar files. i compile the program using command :

javac -classpath jar1.jar:jar2.jar myprog.java and it compiles successfully.

But when i try to run the program using command : java -cp jar1.jar:jar2.jar myprog , it is throwing the java.lang.NoClassDefFoundError . please help , where am i wrong ?

i am using ubuntu 10.04.

Actual error :

Exception in thread "main" java.lang.NoClassDefFoundError: userapps/SelectionTask_classes/SelectionTask
Caused by: java.lang.ClassNotFoundException: userapps.SelectionTask_classes.SelectionTask
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: userapps/SelectionTask_classes/SelectionTask. Program will exit.

where SelectionTask is my class file generated after successful compilation.

Upvotes: 0

Views: 1976

Answers (3)

eternaln00b
eternaln00b

Reputation: 1053

Look at the error, it's complaining about not being able to find "userapps.SelectionTask_classes.SelectionTask", which hints at the last entry in your classpath not beign specified correctly. Now, assuming that the JAR files you have specified are in the following directory structure:

./hadoop-0.20.1-core.jar

./lib/hadoopdb.jar

./userapps/SelectionTask_classes/

You would run:

java -cp hadoop-0.20.1-core.jar:lib/hadoopdb.jar:userapps/SelectionTask_classes/ myprog

Upvotes: 0

maximdim
maximdim

Reputation: 8169

You need to add directory where your compiled java program (*.class) file is located. If it's in current directory then you could run it like this (notice "." which denotes current working directory):

java -cp .:jar1.jar:jar2.jar myprog

Upvotes: 0

Jesper
Jesper

Reputation: 206876

Make sure that the current directory is also in the classpath. Try running with:

java -cp .:jar1.jar:jar2.jar myprog

if your compiled class file myprog.class is in the current directory.

(This assumes that your program is not in a package).

Upvotes: 1

Related Questions