• Coding
  • Exception in thread "main" java.lang.NoClassDefFoundError

hello eveybody,

I have to use CRF package that I have downloaded. I use Ubuntu 11.10 as an OS.
I had installed JDK 6 and ANT 1.8.3. And I want to run the sample given in the README file.
There is a .sh file that I have to run, but first I have to set the CRF_HOME to the directory to where I have the program.
This is the .sh file:
# settings for CRF..
# Check if CRF_HOME is set
if [ ! ${CRF_HOME} ]; then
  echo "CRF_HOME is not set. Using default as current working directory."
  export CRF_HOME=.
fi
echo "Using CRF_HOME=${CRF_HOME}"
# Setting the classpath variable
for file in `ls $CRF_HOME/lib/*.jar`
do
 export CLASSPATH=$file:$CLASSPATH
done
For running the exemple, I proceed as follows:
amira@amira-VirtualBox:~$ export CRF_HOME=/home/amira/CRF
amira@amira-VirtualBox:~$ cd /home/amira/CRF
amira@amira-VirtualBox:~/CRF$ ./settings.sh
amira@amira-VirtualBox:~/CRF$ java iitb.Segment.Segment train -f samples/us50.conf
But I have the following response:

Exception in thread "main" java.lang.NoClassDefFoundError: iitb/Segment/Segment
Caused by: java.lang.ClassNotFoundException: iitb.Segment.Segment
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: iitb.Segment.Segment. Program will exit.

Could you help me to solve this problem.
can you show us the output of the following command?
ls -l /home/amira/CRF/lib
Also do you get any output after running the .sh script?
the output of the command: ls -l /home/amira/CRF/lib

total 1556
-rw-r--r-- 1 amira amira 1090194 2012-04-07 05:52 colt.jar
-rw-r--r-- 1 amira amira 367659 2012-04-07 05:52 CRF.jar
-rw-r--r-- 1 amira amira 118412 2012-04-07 05:52 CRF-Trove_3.0.2.jar
-rw-r--r-- 1 amira amira 8709 2012-04-07 05:52 LBFGS.jar

After running the .sh script, Ihave the following output:
Using CRF_HOME=/home/amira/CRF
So what the shell script has done is initializing the classpath for you but you didn't not use it. Please try to issue the following to run your class:

java -cp $CLASSPATH iitb.Segment.Segment train -f samples/us50.conf
can you please run the .sh script then show us the output of the following command:
echo $CLASSPATH
@Raziology
the output of the command "java -cp $CLASSPATH iitb.Segment.Segment train -f samples/us50.conf" is :

Exception in thread "main" java.lang.NoClassDefFoundError: train
Caused by: java.lang.ClassNotFoundException: train
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: train. Program will exit.
@rahmu

After running the .sh script, the output of the command
echo $CLASSPATH
is a blank line.
So far, you have managed to solve the dependency problems, and now, you still have to solve the main class problem.

Please run the following command again. ( I have added a "." right before "train", and the "T" is now capitalized)
java -cp $CLASSPATH  iitb.Segment.Segment.Train -f samples/us50.conf
I have a few notes however about the above command as well (Try the above before reading the following):
1- The Java packages naming conventions suggest that packages should be in written with lower casing (This is a convention and not a syntax limitation)

2- The Java classes naming conventions suggest that classes should be written with a first letter in upper case.

3- In the above command, you have this " iitb.Segment.Segment.Train"
This means that in your current working directory (From where you are issuing your java command), there should be a folder structure that's similar to the package name. i.e.
iitb ==> Parent Folder
Segment ==> Child of iitb
Segment ==> Child of Segment
Train ==>Child of Segment and the main class

In case the first command I gave you did not work, please take my notes into consideration and try to fine tune it. Let us know what happens.

Good luck.
I posted my reply before reading your reply about the output of the CLASSPATH variable.

Amira, could you please do the following:
# settings for CRF..
# Check if CRF_HOME is set
if [ ! ${CRF_HOME} ]; then
  echo "CRF_HOME is not set. Using default as current working directory."
  export CRF_HOME=.
fi
echo "Using CRF_HOME=${CRF_HOME}"
# Setting the classpath variable
for file in `ls $CRF_HOME/lib/*.jar`
do
 export CLASSPATH=$file:$CLASSPATH
 echo "Reached Here"
done
I added a
echo "Reached Here"
line in the shell script for us to know if this portion of code is being executed or not.

With the above change in mind, please tell us what's your output.
After adding
echo "Reached Here"
the run of .sh script give the following output:

Using CRF_HOME=/home/amira/CRF
Reached Here
Reached Here
Reached Here
Reached Here
Amira, did you try the suggestions from my previous post?
of course, I try the suggestion:
java -cp $CLASSPATH  iitb.Segment.Segment.Train -f samples/us50.conf
an it gives the following output:

Unrecognized option: -f
Could not create the Java virtual machine.
Can you please tell me what's the content of the directory from which you are issuing the Java command?
Issue
ls -all
amira@amira-VirtualBox:~/CRF$ ls -all
total 72
drwxr-xr-x 9 amira amira 4096 2012-05-01 09:23 .
drwxr-xr-x 30 amira amira 4096 2012-04-24 14:32 ..
drwxrwxr-x 3 amira amira 4096 2012-04-30 20:24 build
-rw-r--r-- 1 amira amira 2223 2012-04-29 22:37 build.xml
-rw-r--r-- 1 amira amira 2231 2012-04-25 14:42 build.xml~
-rw-r--r-- 1 amira amira 2223 2012-04-07 05:52 .#build.xml.1.4
drwxr-xr-x 5 amira amira 4096 2012-04-18 08:19 doc
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 lib
-rw-r--r-- 1 amira amira 1992 2012-04-07 05:51 LICENSE.txt
-rw-r--r-- 1 amira amira 1028 2012-04-07 05:51 README
-rw-r--r-- 1 amira amira 2400 2012-04-07 05:51 RELEASE-NOTES
drwxr-xr-x 5 amira amira 4096 2012-04-18 08:19 samples
-rwxr-xr-x 1 amira amira 470 2012-05-01 09:23 settings.sh
-rwxr-xr-x 1 amira amira 470 2012-05-01 09:22 settings.sh~
drwxr-xr-x 3 amira amira 4096 2012-04-18 08:19 src
drwxr-xr-x 3 amira amira 4096 2012-04-18 08:19 test
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 testdata
-rw-r--r-- 1 amira amira 2908 2012-04-07 05:51 third-party-license.txt
So amira, apparently this is your problem: I see no "iitb" folder as I have explained in one of my posts.
All you have is the "src" folder which I suppose contains your java classes (which are not compiled).

Could you please tell us more about the contents of the "src" folder.
the content of the src folder is:

amira@amira-VirtualBox:~/CRF$ ls -l src
total 4
drwxr-xr-x 11 amira amira 4096 2012-04-07 05:55 iitb
Great Amira, as I told you before, apparently this is your problem.

Please give us the structure of the ittb folder, and then from the command that you were issuing, you should expect to see a Segment folder, and then inside of it there should be another Segment folder, and then you should find your main class Train inside the second Segment folder.

Please do an "ls -l" inside each of the folders I mentioned, and give us the output. The solution is around the corner, so bear with me a little more.
amira@amira-VirtualBox:~/CRF/src$ ls -l iitb
total 36
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 AStar
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 BSegment
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 BSegmentCRF
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 CRF
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 KernelCRF
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 MaxentClassifier
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 Model
drwxr-xr-x 2 amira amira 4096 2012-04-18 08:19 Segment
drwxr-xr-x 2 amira amira 4096 2012-04-07 05:55 Utils
amira@amira-VirtualBox:~/CRF/src/iitb$ ls -l Segment
total 64
-rw-r--r-- 1 amira amira  1241 2012-04-07 05:52 AlphaNumericPreprocessor.java
-rw-r--r-- 1 amira amira 17483 2012-04-07 05:52 DataCruncher.java
-rw-r--r-- 1 amira amira   710 2012-04-07 05:52 LabelMap.java
-rw-r--r-- 1 amira amira   103 2012-04-07 05:52 package.html
-rw-r--r-- 1 amira amira   209 2012-04-07 05:52 Preprocessor.java
-rw-r--r-- 1 amira amira 16649 2012-04-07 05:52 Segment.java
-rw-r--r-- 1 amira amira   364 2012-04-07 05:52 TrainData.java
-rw-r--r-- 1 amira amira   467 2012-04-07 05:52 TrainRecord.java
train is a method in the main of Segment.java
Ah great.
So what you have in your source folder is the .java classes and NOT the .class files.

You need to compile all of your source files using "javac", do you know how to do that?
No, I don't know.
Could you tell me how to compile all of the source files?