Results 1 to 10 of 10
  1. #1
    tBKwtWS is offline Member
    Join Date
    Jun 2011
    Location
    Amsterdam
    Posts
    3
    Rep Power
    0

    Question [SOLVED] ClassNotFoundException: com.mysql.jdbc.Driver

    Dear reader,

    I hope I picked the correct board for this topic, I know how much weight some people can place on that, if not, I' m sorry.

    I started learning java 2 weeks ago and have been playing around with pircbot.
    My application works fine when I run it in eclipse, but when I try to run it from the terminal I get the following error message:
    Java Code:
    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 javaRot.bot.db.<init>(db.java:15)
    	at javaRot.bot.javaRotMain.main(javaRotMain.java:7)
    Some more info on me:
    As you know a am beginner with java.
    The same goes for mysql and JDBC, which I all have been playing around with for 2 weeks now.
    I am running ubuntu linux, which I am a little more experienced with. I have been using it for a year now. And recently installed 11.04.

    Versions:
    Eclipse SDK - Version: 3.5.2
    JDBC - mysql-connector-java-5.1.16
    Java - JDK Java 6 runtime

    As suggested I searched the web and this forum on solutions before posting (about 1 hours last night and 2 hours today) and I did find some solutions. Steps taken:

    • In eclipse, I already had the added connector in the (Java build path > libraries) (I tried adding them as external and internal jars, ofc not both at the same time), but I found out I should have also added it to the Classpath, so I rightclicked my project in eclipse and picked: Run as> Run Configurations > Classpath. Under User entries I added the connector jar (again I tried it first as internal and later as external jar)
    • Copied "mysql-connector-java-5.1.16-bin.jar" to "/usr/share/java"
    • Installed libmysql-java on ubuntu
    • set the classpath in the ubuntu terminal: "CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar", but I also tried the connector I downloaded and copied there: "CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.16-bin.jar" and the connector I downloaded in it's original location (before I copied the connector to the "/usr/share/java dir") "CLASSPATH=$CLASSPATH:/media/MultiSave/colocations/locahost/java/JavaRot/lib/mysql-connector-java-5.1.16-bin.jar"


    Additional info:
    I saw you can check if you set the classpath correctly by typing "java com.mysql.jdbc.Driver" in the terminal.
    First the result was something like not found or unable to find and now the result is:
    Java Code:
    mah@mah-Domestique://media/MultiSave/Colocations/localhost$ java com.mysql.jdbc.Driver
    Exception in thread "main" java.lang.NoSuchMethodError: main
    So that tells me the classpath is correct, but then again it' s not as when I try to run my app ("java -jar JavaRot.jar" I get the same old:
    Java Code:
    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 javaRot.bot.db.<init>(db.java:15)
    	at javaRot.bot.javaRotMain.main(javaRotMain.java:7)
    Now I know there are countless articles about this and I could spend another week reading them all trying to find my solution, so my request is, please point me toward the documentation I need so I can read up an how to properly get my application set up to run from the terminal.

    I am pretty sure that my application code is fine, since it does work properly when it's run from eclipse, but here are the 2 classes you will be intrested in:

    javaRotMain.java
    Java Code:
    package javaRot.bot]
    
    import java.sql.ResultSet;
    
    public class javaRotMain {
    	public static void main(String[] args) throws Exception {
    	        db dbAction = new db();                                       // Line 7
                    ResultSet settingsResult = dbAction.readAll("botSettings");
                    while (settingsResult.next()) {
            	        String network = settingsResult.getString ("network");
                       	String url = settingsResult.getString ("url");
                     	String nick = settingsResult.getString ("nick");
                     	String ident = settingsResult.getString ("ident");
                    	String homeChan = settingsResult.getString ("homeChan");
                    	String netChan = settingsResult.getString ("netChan");
                    	int connects = settingsResult.getInt ("connects");
                    	String lastConnect = settingsResult.getString ("lastConnect");
            	
                    	javaRot RotBot = new javaRot(url, network, nick, ident, homeChan, netChan, connects, lastConnect); // TOADD homePass, netPass 
                   }
            System.out.println("''''''''''''' OK JavaRot: Bot started");
    	}
    }
    name=db.java
    Java Code:
    package javaRot.bot;
    
    
    
    import java.sql.Connection;
    
    import java.sql.DriverManager;
    
    import java.sql.PreparedStatement;
    
    import java.sql.ResultSet;
    
    import java.sql.SQLException;
    
    
    
    public class db {
    
    	private Connection con;
    
    	public db() throws ClassNotFoundException, SQLException {
    
    		if (con == null) {
    
    			System.out.println("||||||||||||| JDBC: Not connected, connecting.");
    
    			String dbUrl = "jdbc:mysql://localhost/tbk_bot";
    
    			Class.forName("com.mysql.jdbc.Driver");            // Line 15
    			
    
    			con = DriverManager.getConnection(dbUrl,"user", "pass");	
    
    		}
    
    	}
    
    	public ResultSet readAll (String table) throws SQLException {
    
    		// Kan efficienter 2 regels op 1.
    
    		System.out.println("||||||||||||| SELECT * FROM `" + table + "`;");
    
    		ResultSet result = null;
    
    		PreparedStatement statement = con.prepareStatement("SELECT * FROM `" + table + "`;");
    
    		result = statement.executeQuery();	
    
    		
    
    		return result;
    
    	}
    
    	public ResultSet read (String table, String variable, String value) throws SQLException {
    
    		System.out.println("||||||||||||| SELECT * FROM `" + table + "` WHERE `" + variable + "` = '" + value + "';");
    
    		ResultSet result = null;
    
    		PreparedStatement statement = con.prepareStatement("SELECT * FROM `" + table + "` WHERE `" + variable + "` = '" + value + "';");
    
    		result = statement.executeQuery();
    
    		
    
    		return result;
    
    	}
    
    	public void write (String action, String table, String values) throws SQLException {	
    
    		System.out.println("||||||||||||| " + action + " `" + table + "` SET " + values + ";");
    
    		java.sql.Statement st = con.createStatement();  //(action + " " + table + " SET " + values + ";");
    
    		int val = st.executeUpdate(action + " `" + table + "` SET " + values + ";");
    
    		
    
    	}
    
    }
    Thanks in advance,

    Maarten Alberic 'tBKwtWS'.
    Last edited by tBKwtWS; 06-15-2011 at 07:11 PM. Reason: solved

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Nice job of showing the problem. You should be giving lessons to the other OPs.

    Do you know which jar file the missing class is in?
    when I try to run my app ("java -jar JavaRot.jar" I get the same old:
    A couple of ways to work on this. I don't know your IDE so can't suggest how to use it.
    One way would be to set the Class-Path: entry in the manifest file to point to jar file with the missing driver.
    Another is to manually reference the starting class in the java command and put all jar files in the classpath:

    java -classpath JavaRot.jar;JARWITHMISSINGDRIVER.jar;. JavaRot.bot.javaRotMain

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    java -jar ignores any preset CLASSPATH and relies entirely on the classpaths defined in the MANIFEST of the jar file.
    Which is why java com.mysql.jdbc.Driver gives the expected result (it fonds Driver, but can't run because Driver has no main()), but your jar file throws the exception.

    So, set your manifest correctly.

  4. #4
    tBKwtWS is offline Member
    Join Date
    Jun 2011
    Location
    Amsterdam
    Posts
    3
    Rep Power
    0

    Default

    Yes the missing class is referred to on line 15 in db.java: Class.forName("com.mysql.jdbc.Driver"); // Line 15


    I tried your suggestion of manually referencing and issued to the terminal in the correct directory: "java -classpath JavaRot.jar; /lib/mysql-connector-java-5.1.16-bin.jar; JavaRot.bot.javaRotMain" in the directory of the files but that just replied a list of possible options for java, so that makes me think the syntax is incorrect the same goes for:

    • java -classpath JavaRot.jar;/lib/mysql-connector-java-5.1.16-bin.jar;. JavaRot.bot.javaRotMain
    • java -classpath JavaRot.jar;/lib/mysql-connector-java-5.1.16-bin.jar; JavaRot.bot.javaRotMain
    • java -classpath JavaRot.jar; /lib/mysql-connector-java-5.1.16-bin.jar; JavaRot.bot.javaRotMain
    • java -classpath /media/MultiSave/Colocations/locohost/JavaRot.jar; /media/MultiSave/Colocations/localhost/lib/mysql-connector-java-5.1.16-bin.jar; JavaRot.bot.javaRotMain
    • java -classpath /media/MultiSave/Colocations/locohost/JavaRot.jar; /media/MultiSave/Colocations/localhost/lib/mysql-connector-java-5.1.16-bin.jar; /media/MultiSave/Colocatoins/localhost/JavaRot.bot.javaRotMain
    • java -classpath /media/MultiSave/Colocatoins/localhost/JavaRot.jar; /media/Multisave/Colocatoins/localhost/lib/java -classpath JavaRot.jar;/lib/mysql-connector-java-5.1.16-bin.jar; JavaRot.bot.javaRotMain
    • java -classpath /media/MultiSave/Colocatoins/localhost/JavaRot.jar; /media/Multisave/Colocatoins/localhost/lib/mysql-connector-java-5.1.16-bin.jar

    I feel I have tried all the possible variations of the line you gave me.

    Thanks norm and tolls I have been reading up on the manifest file, and noticed that I should edit it by package sealing, I saw that option in eclipse when I was exporting my project to a .jar file and will be experimenting with after reading some more about the manifest file, you have certainly pointed me in a direction I had not considered, thank you!
    I' ll post back to inform you if this topic can be closed or the issue persists.

    I already undertook some steps I found in other peoples posts/solutions, were those correctly pointless and can I undo them, or is it a good idea to keep some of them, like the install of libmysql-java?
    Last edited by tBKwtWS; 06-15-2011 at 07:06 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Here's my console on WinXP when I execute the java command with bad/missing files:
    PHP Code:
    D:\Norms\Norms Tools>java -classpath notfile.jar mineclass
    Exception in thread "main" java.lang.NoClassDefFoundError: mineclass
    Caused by: java.lang.ClassNotFoundException: mineclass
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: mineclass.  Program will exit.
    On Linux is the - a special character?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    On Linux-Unix that separator character is a colon (:) , not a semi-colon (;) as you did in all your attempts.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    You can't use -cp (or a CLASSPATH variable) with an executable jar.
    Quoting from the font of all java knowledge (in the -jar section):
    When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
    So write your manifest properly, and stop faffing with classpaths outside that.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    You can't use -cp (or a CLASSPATH variable) with an executable jar.
    Have you tried it? Does the presence of a manifest file in the jar prevent it being on the classpath and a source of class files?

    This works for me. GuiBuilder.jar is an executable jar file:
    PHP Code:
    D:\JavaDevelopment\runtime>dir Gui*
     Volume in drive D is Work
     Volume Serial Number is 1463-F5AE
    
     Directory of D:\JavaDevelopment\runtime
    
    12/30/2007  05:36 PM            58,058 GuiBuilder.jar
    09/23/2008  06:38 PM                85 GuiBuilderTest.jar.bat
                   2 File(s)         58,143 bytes
                   0 Dir(s)   1,532,899,328 bytes free
    
    D:\JavaDevelopment\runtime>java -classpath GuiBuilder.jar GuiBuilder   <<<<<<<<<< NORMAL execution

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    No.
    You misunderstand.
    Using the executable jar as an executable jar (ie using java -jar) means you cannot use any other classpath.

  10. #10
    tBKwtWS is offline Member
    Join Date
    Jun 2011
    Location
    Amsterdam
    Posts
    3
    Rep Power
    0

    Lightbulb Solution

    Tolls his suggestion was correct. And after searching the web for the term manifest I found the solution.
    As my largest frustration in the last 2 weeks was finding solved topics without a solution I will now write a small guide on how to add a manifest file in eclipse:

    1. Right-click your project and choose export and ofc jar file.
    2. Click next until you reach the "JAR Manifest Specification" screen.
    3. Here pick "Generate the manifest file" and select the option "Save the manifest in the workspace".
    4. Event though you have not edited the manifest yet, go ahead and finish the export.
    5. Open the manifest file with gedit or another text editor and add a line similar to mine: "Class-Path: lib/mysql-connector-java-5.1.16-bin.jar lib/pircbot.jar".
    6. Repeat steps 1 and 2, and now pick "Use existing manifest from workspace".
    7. Finish exporting.


    You could also skip the first 5 steps and make your own manifest file.

    And just when I finished typing this 15 mins ago, my cat decided to jump on my power button, ifc after moving the cd case that I put on the button to prevent just that. Giving me some more frustration after feeling hte relief of seeing my code work.
    I' d just like to add that no animals were harmed during the creation of RotBot, excluding those deaths resulting in our horribly clean power generation facilities.


    Quote Originally Posted by Norm View Post
    Nice job of showing the problem. You should be giving lessons to the other OPs.
    I am free now I solved my problem with your help... (joke)

    Oh and lets not forget: Kind thanks to all who assisted!
    Last edited by tBKwtWS; 06-15-2011 at 07:26 PM. Reason: thanks added

Similar Threads

  1. Replies: 10
    Last Post: 03-20-2012, 08:23 AM
  2. Replies: 4
    Last Post: 03-31-2010, 12:08 PM
  3. Replies: 0
    Last Post: 03-01-2010, 01:03 PM
  4. java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
    By kishan.java in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 10-19-2009, 05:34 PM
  5. Replies: 0
    Last Post: 03-21-2008, 01:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •