Results 1 to 14 of 14
- 08-25-2011, 07:13 PM #1
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~
- 08-25-2011, 07:47 PM #2
Are you using an IDE or the command line to compile?
- 08-25-2011, 07:59 PM #3
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.I did put the .jar file on the path
What is in the .jar file? Does it contain the com.mysql.jdbc.Driver class?
- 08-25-2011, 08:25 PM #4
- 08-25-2011, 08:26 PM #5
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~
- 08-25-2011, 08:35 PM #6
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.jarLast edited by Norm; 08-25-2011 at 08:39 PM.
- 08-25-2011, 09:06 PM #7
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~
- 08-25-2011, 09:21 PM #8
You do have to tell Java what class to run (via its main method).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.
db
- 08-25-2011, 09:26 PM #9
java -cp thejarfileshere;. thestartingclassnamehere
- 08-25-2011, 09:42 PM #10
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~
- 08-25-2011, 09:44 PM #11
You could add the Class-Path: line to the manifest file in your jar file.
- 08-25-2011, 10:33 PM #12
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~
- 08-25-2011, 10:45 PM #13
You to go to the source of the contents of the jar file and change it there and then rebuilt the jar file.
- 08-26-2011, 09:47 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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
-
ClassNotFoundException...
By providence in forum JDBCReplies: 2Last Post: 02-02-2011, 06:45 PM -
ClassnotFoundException
By balaji shan in forum Java ServletReplies: 8Last Post: 01-19-2011, 03:47 PM -
ClassNotFoundException
By meprasobh in forum New To JavaReplies: 2Last Post: 10-17-2010, 03:32 PM -
classNotFoundException
By jyothi.priyanka in forum NetBeansReplies: 5Last Post: 08-24-2010, 10:13 AM -
ClassNotFoundException
By lgpublic in forum Advanced JavaReplies: 5Last Post: 04-23-2010, 03:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks