LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2011, 11:02 PM   #16
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0

Sure

Code:
[jordan@Jordan-Laptop ~]$ ls -l /usr/share/java/mysql-connector-java.jar
lrwxrwxrwx. 1 root root 31 Jun 12 18:29 /usr/share/java/mysql-connector-java.jar -> mysql-connector-java-5.1.15.jar
[jordan@Jordan-Laptop ~]$ 
[jordan@Jordan-Laptop ~]$ ls -lL /usr/share/java/mysql-connector-java.jar
-rwxr-xr-x. 1 root root 817285 Feb 10 02:12 /usr/share/java/mysql-connector-java.jar
[jordan@Jordan-Laptop ~]$ 
[jordan@Jordan-Laptop ~]$ ls -l /usr/share/java/mysql-connector-java-5.1.15.jar
-rwxr-xr-x. 1 root root 817285 Feb 10 02:12 /usr/share/java/mysql-connector-java-5.1.15.jar
I have downloaded the connector straight from the MySQL site and have tried to use that one. It works fine on Windows but I get the same Class Not Found error on my laptop running Fedora 14 and my server running Ubuntu 10.04
 
Old 06-14-2011, 12:23 AM   #17
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Maybe the mysql .jar file is corrupt?

If you used "ftp", for example, you need to make sure to do a binary transfer.

A shot in the dark ...
 
Old 06-14-2011, 04:02 AM   #18
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
For Ubuntu, try using the one that comes from their repository (via Synaptic Manager) versus using the one from the MySQL website. Similarly for Fedora, you should use "yum".
 
Old 06-14-2011, 07:11 AM   #19
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
Maybe the mysql .jar file is corrupt?

If you used "ftp", for example, you need to make sure to do a binary transfer.

A shot in the dark ...
I downloaded it via HTTP, plus the jar runs just fine on Windows in the same directory as my-jar.jar

Quote:
Originally Posted by dwhitney67 View Post
For Ubuntu, try using the one that comes from their repository (via Synaptic Manager) versus using the one from the MySQL website. Similarly for Fedora, you should use "yum".
I installed the MySQL connector using the package manager of each machine and those didn't work either, got the Class Not Found Exception.

 
Old 06-14-2011, 08:29 AM   #20
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by jordanwb View Post
I downloaded it via HTTP, plus the jar runs just fine on Windows in the same directory as my-jar.jar



I installed the MySQL connector using the package manager of each machine and those didn't work either, got the Class Not Found Exception.

Back to the version of Java, which do you have running under the Ubuntu system?

At my home, I have Ubuntu 10.04 and 10.11; on both machines I have the openJDK installed, along with the MySQL connector, which I obtained from the package manager. On both systems, I defined the CLASSPATH as we have already discussed, and everything works fine.

If on your Ubuntu system, if you have performed any deviations from the basic steps I took, then that might explain why you are having issues. I would suggest that you remove the relevant packages in their entirety, then reinstall using the package manager.
 
Old 06-14-2011, 09:00 AM   #21
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
Code:
root@ubuntu-server:/home/jordan# apt-get -qq install openjdk-6-jre-headless libmysql-java
root@ubuntu-server:/home/jordan# exit
jordan@ubuntu-server:~$ dir
public_html settings.conf my-jar.jar
jordan@ubuntu-server:~$ dir /usr/share/java/
libintl.jar  mysql-5.1.10.jar  mysql-connector-java-5.1.10.jar  mysql-connector-java.jar  mysql.jar
jordan@ubuntu-server:~$ export CLASSPATH=./:/usr/share/java/mysql-connector-java.jar; echo $CLASSPATH
./:/usr/share/java/mysql-connector-java.jar
jordan@ubuntu-server:~$ ls -l /usr/share/java/mysql-connector-java.jar
lrwxrwxrwx 1 root root 31 2011-06-14 09:54 /usr/share/java/mysql-connector-java.jar -> mysql-connector-java-5.1.10.jar
jordan@ubuntu-server:~$ ls -lL /usr/share/java/mysql-connector-java.jar
-rw-r--r-- 1 root root 754057 2010-01-26 03:02 /usr/share/java/mysql-connector-java.jar
jordan@ubuntu-server:~$ ls -l /usr/share/java/mysql-connector-java-5.1.10.jar
-rw-r--r-- 1 root root 754057 2010-01-26 03:02 /usr/share/java/mysql-connector-java-5.1.10.jar
jordan@ubuntu-server:~$ java -cp $CLASSPATH -jar ./my-jar.jar
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        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)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at Server.loadMysqlDriver(Server.java:29)
        at Server.<init>(Server.java:23)
        at Server.main(Server.java:42)
jordan@ubuntu-server:~$
I'm running version 6b20-1.9.7-0ubuntu1~10.04.1 of openjdk-6-jre-headless and version 5.1.10+dfsg-2ubuntu1 of libmysql-java. I may just decompress the jar and load it directly into my project and not worry about this Class Not Found nonsense

Last edited by jordanwb; 06-14-2011 at 09:01 AM.
 
Old 06-14-2011, 09:23 AM   #22
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It just dawned upon me that I was never conducting my tests like you were; that is, I was always loading a .class file, not a .jar.

When I attempt to load a .jar, then my app gracefully exits after an exception is caught.
Code:
$ java -jar db.jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
If I run the app such as this, then all is well:
Code:
java DB
Note I did not specify the CLASSPATH because it is already defined in my environment.


Here's my code again:
Code:
import java.sql.*;
import java.util.Properties;
import java.io.FileInputStream;

public class DB
{
   private Connection dbConn;

   public DB()
   {
      try {
         Properties settings = new Properties();
         settings.load(new FileInputStream("db_credentials.conf"));

         Class.forName("com.mysql.jdbc.Driver").newInstance();

         String url = "jdbc:mysql://" + settings.getProperty("mysql-location") +
                      ":" + settings.getProperty("mysql-port") +
                      "/" + settings.getProperty("mysql-database");

         dbConn = DriverManager.getConnection(url,
                                              settings.getProperty("mysql-user"),
                                              settings.getProperty("mysql-password"));

         if (dbConn != null)
         {
            System.out.println("Success!");
            dbConn.close();
         }
      }
      catch (SQLException e) {
         System.err.println("SQLException: " + e.getMessage());
         System.err.println("SQLState    : " + e.getSQLState());
         System.err.println("Vendor Error: " + e.getErrorCode());
      }
      catch (Exception e) {
         System.err.println(e.toString());
      }
   }

   public static void main(String[] args)
   {
      DB db = new DB();
   }
}
 
Old 06-14-2011, 09:30 AM   #23
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Finally... I found the solution using Google.

You need to update you manifest file to look something like:
Code:
Main-Class: <class name>
Class-Path: /usr/share/java/mysql-connector-java.jar
Update <class name> with your main/public class name. Then rebuild your jar, and test again.
 
Old 06-14-2011, 09:41 AM   #24
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
Code:
cat ./Manifest
Main-Class: Server
Class-Path: /usr/share/java/mysql-connector-java.jar
If I copy mysql-connector-java.jar into the same folder as my-jar.jar it works (which is okay I guess), but if its not, it gives the Class Not found error

Last edited by jordanwb; 06-14-2011 at 09:46 AM.
 
Old 06-14-2011, 09:56 AM   #25
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
So it sounds like the problem was:

1. You chose to put your program root in a .jar

2. The manifest in your .jar file had a hard-coded class path
 
Old 06-14-2011, 10:07 AM   #26
jordanwb
Member
 
Registered: Dec 2008
Posts: 34

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
You chose to put your program root in a .jar
Why is this a problem? And if this is a problem, it is a problem with BlueJ.

Quote:
Originally Posted by paulsm4 View Post
The manifest in your .jar file had a hard-coded class path
Again, why is this a problem?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
install mysql-connector-java redhat sssl Linux - Newbie 1 05-01-2009 08:38 PM
mysql java connector cuk Slackware 4 06-01-2008 10:48 AM
Mysql and mysql java connector, connection refused arubin Slackware 3 03-29-2008 01:41 AM
JDBC connection, mysql-connector-java fhleung Programming 4 08-16-2007 12:49 AM
Where to put mysql-connector-java folder dukehazord Linux - General 1 09-10-2004 11:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration