LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Java apps runs fine but gives error from jar file-NoClassDefFoundError (https://www.linuxquestions.org/questions/linux-software-2/java-apps-runs-fine-but-gives-error-from-jar-file-noclassdeffounderror-437517/)

lbyrd 04-21-2006 03:07 PM

Java apps runs fine but gives error from jar file-NoClassDefFoundError
 
Hello,
I seem to be having a problem creating a jar file that I can use to access a mysql data base. I am using Linux Fedora Core 3, jave jdk1.5.0_04, mysql DB and the j/connector from the mysql site (mysql-connector-java-3.1.12). I have the jar file for the connector in jdk1.5.0_04/jre/lib/ext/mysql-connector-java-3.1.12-bin.jar ( am not sure that is the correct place but I have tried several different locations to no avail). I am using the eclipse IDE to create the programs and everything runs great while in the ide. Eclipse uses the directory workspace for its work area and I have created a project othertest. Here is the directory listing for that project:
bin (this is the directory where the class files are kept)
ShowWxData.jar
ShowWxData.mf
src (this is the directory where the java files are kept)

the listing for the bin directory:
>ls -1 bin
ShowWxData.class
WxStationData.class

when I cd into the bin directory and run the java program all is good:
[lbyrd@localhost bin]$ java ShowWxData
Current Conditions as of 2006-04-21 12:51:35.0
Partially Cloudy
Temperature ouside 69.3 deg F with humidity at 59%
Winds from 201 deg. at 11mph
Last wind gust from 202deg. at 13mph
Forecast: Partly cloudy with little temperature change..
+ + + + + + + + + + + + + + + + + + +

However when I move up one directory and try to use the jar file, this is what I get:
[lbyrd@localhost othertest]$ java -jar ShowWxData.jar
Exception in thread "main" java.lang.NoClassDefFoundError: ShowWxData

Here is the contents of the jar file:
>jar -tf ShowWxData.jar
META-INF/
META-INF/MANIFEST.MF
bin/ShowWxData.class
bin/WxStationData.class
/usr/java/jdk1.5.0_04/jre/lib/ext/mysql-connector-java-3.1.12-bin.jar

here is the manifest file:
cat ShowWxData.mf
Manifest-Version: 1.0
Class-Path: . /usr/java/jdk1.5.0_04/jre/lib/ext/mysql-connector-java-3.1.12-bin.jar
Main-Class: ShowWxData

What I am doing wrong so the jar file does not work. Any help/hints are greatly appreicated.

Lyman

mrcheeks 04-21-2006 03:28 PM

How did you create your jar file? Do you need a bin directory or is ShowWxData in the bin package. If the class ShowWxData is in the bin package then the main-class should be : bin.ShowWxData.

ps : You do not need to put the mysql jar in the ext directory of your java virtual machine. You could add it to your project classpath in eclipse.

lbyrd 04-24-2006 08:58 AM

answer to questions
 
In eclipse I was exterimenting with splitting out the src and class files and that is why I have the bin directory. I have tried creating the jar file by hand and from withing eclipse with the same result. do you have a suggestion of the jar command that I should try?

I had added the mysql connectory jar file to the project which worked just fine but as I pointed out everything worked ok as long as I was running under eclipse but not from the jar file so I was trying everything I could think of to try to make it work.

Any and all suggestions are greatly appreciated.

Thanks
Lyman

lbyrd 04-24-2006 09:08 AM

You had the correct idea.
 
After I sent the other reply I tried a little experiment. I copied all the class files from the bin directory to the current directory and created the jar file again using the command
jar -cvmf ShowWxData.mf ShowWxData.jar *.class

and guess what it all works not no matter where I move or copy the jar file...

So do most people not use the split bin and src structure? could I have done something different on in eclipse to make the jar file?

Thanks so much for the help and the mentoring...

Lyman

mrcheeks 04-25-2006 11:46 AM

When i was using eclipse i used to split src and classes directory(bin by default) and it always worked. I always used the eclipse jar export tool while specifying a main class to run.

lbyrd 04-25-2006 11:50 AM

I tried eclipse export first as well
 
As the subject states I had tried the eclipse export tool first and that is what led me to the current problem. I still dont really understand why it didnt work. May I need to export the project ... I will try that ..

So much to learn, so little brain available...

Thanks for the help

Lyman


All times are GMT -5. The time now is 09:46 AM.