Results 1 to 14 of 14
  1. #1
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default ClassNotFoundException!! Oh BOY!!

    Just trying to get my machine to pickup my Jdbc connector. I get the ClassNotFoundException: com.mysql.jdbc.Driver error when printing the stack trace().

    Any thoughts? (BTW yes I did put the .jar file on the path).

    Java Code:
    import java.sql.*;
    
    public class ListStudentInfo
    {
        public static void main(String[] args)
        {
            ResultSet students = getStudentResultSet();
            
            try
            {
                while (students.next())
                {
                    String msg; 
                    Student st = getStudent(students);
                    
                    msg = st.name + " ";
                    msg += st.email + " ";
                    System.out.println(msg);
                }
                
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            
        }
        
        private static ResultSet getStudentResultSet()
        {
            Connection con = getConnection();
            try
            {
                Statement s = con.createStatement();
                String select = "select name, email from studentInfo order by name;";
                ResultSet rows;
                rows = s.executeQuery(select);
                return rows;
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        
        private static Connection getConnection()
        {
            Connection con = null;
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost/Students";
                String user = "root";
                String pw = "chicote";
                con = DriverManager.getConnection(url, user, pw);
            }
            
            catch (ClassNotFoundException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            return con;
        }
        
        private static Student getStudent(ResultSet students)
        {
            try
            {
                String name = students.getString("name");
                String email = students.getString("email");
                return new Student(name, email);
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        
        private static class Student
        {
            public String name;
            public String email;
            public Student(String name, String email)
            {
                this.name = name;
                this.email = email;
            }
        }
    }
    ~MSP430 Lover~

  2. #2
    Join Date
    Aug 2011
    Location
    salt Lake City, Utah
    Posts
    10
    Rep Power
    0

    Default

    Are you using an IDE or the command line to compile?

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

    Default

    I did put the .jar file on the path
    It needs to be on the classpath. The PATH is for the OS to find commands. The classpath is used by java to find class definitions.

    What is in the .jar file? Does it contain the com.mysql.jdbc.Driver class?

  4. #4
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by nathandelane View Post
    Are you using an IDE or the command line to compile?
    Thanks, I am using IDE (BlueJ 3.0.4).
    ~MSP430 Lover~

  5. #5
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Thanks Norm, always a pleasure. You know, I had a feeling that might have been the issue. CLASSPATH isn't in my environment variables as an option. Should I create?
    ~MSP430 Lover~

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

    Default

    Probably not. Use the java command's -cp option.
    Setting the OS's environment variable can have unexpected results down the line when you don't want where it points to be used.

    There is an line you can add to the manifest file that will add a jar file to the classpath for the code in that jar.
    Add a Class-Path: line to your jar file. For example:

    Class-Path: DocumentViewerWParser.jar
    Last edited by Norm; 08-25-2011 at 09:39 PM.

  7. #7
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Hmmm. At the command line I did a java -cp<full path of jdbc connector jar file>. It waited and then threw the help options out at me. Also, I tried to use the jar tool in order to get the contents of the jar file and console told me it wasn't recognized as a command. On that do I have to add it to the path before I can use the jar tools?
    ~MSP430 Lover~

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    At the command line I did a java -cp<full path of jdbc connector jar file>. It waited and then threw the help options out at me.
    You do have to tell Java what class to run (via its main method).

    db

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

    Default

    java -cp thejarfileshere;. thestartingclassnamehere

  10. #10
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    The problem is I don't know what entry class of the jar file is because I can't open up the jar file to inspect. Am trying some work-arounds right now. Once I have that figured out I will attempt the command line syntax you just gave me...
    ~MSP430 Lover~

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

    Default

    You could add the Class-Path: line to the manifest file in your jar file.

  12. #12
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Well I am able to view the contents of the jar file and opened up the manifest file as a text file. However, I can't add save any lines to it.
    ~MSP430 Lover~

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

    Default

    You to go to the source of the contents of the jar file and change it there and then rebuilt the jar file.

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

    Default

    Hang on.
    What are you trying to run?
    What jar file are you messing around with?
    Is that jar file your jar file, or are you messing around with the jdbc jar?

    I'm asking, because your original post mentions no jar file for your code, only for the JDBC driver...and that you were trying to run your code via BlueJ, which didn't imply you were running your jar file, but trying to execute through BlueJ.

Similar Threads

  1. ClassNotFoundException...
    By providence in forum JDBC
    Replies: 2
    Last Post: 02-02-2011, 07:45 PM
  2. ClassnotFoundException
    By balaji shan in forum Java Servlet
    Replies: 8
    Last Post: 01-19-2011, 04:47 PM
  3. ClassNotFoundException
    By meprasobh in forum New To Java
    Replies: 2
    Last Post: 10-17-2010, 04:32 PM
  4. classNotFoundException
    By jyothi.priyanka in forum NetBeans
    Replies: 5
    Last Post: 08-24-2010, 11:13 AM
  5. ClassNotFoundException
    By lgpublic in forum Advanced Java
    Replies: 5
    Last Post: 04-23-2010, 04:05 PM

Posting Permissions

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