CodeMed
CodeMed

Reputation: 9191

ClassNotFound Exception when running java from CentOS 7 terminal

When I try to run the simple Java program below from the CentOS 7 terminal, I get a java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error at the line of code that says Class.forName("com.mysql.jdbc.Driver");. How can I resolve this and similar errors in the code below so that the simple program below will run without errors?

Here is what I have so far:

I navigate to /path/to/ and then type:

javac somepackage/TestJDBC.java
java somepackage.TestJDBC  

This results in the error described above. The full code of this simple program is:

package somepackage;
//STEP 1. Import required packages
import java.sql.*;

public class TestJDBC {
 // JDBC driver name and database URL
 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
 static final String DB_URL = "jdbc:mysql://localhost:3306/somedb?autoReconnect=true";

 //  Database credentials
 static final String USER = "usrname";
 static final String PASS = "pword";

 public static void main(String[] args) {
 Connection conn = null;
 Statement stmt = null;
 try{
    //STEP 2: Register JDBC driver
    Class.forName("com.mysql.jdbc.Driver");

    //STEP 3: Open a connection
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);

    //STEP 4: Execute a query
    System.out.println("Creating statement...");
    stmt = conn.createStatement();
    String sql;
    sql = "SELECT id, name FROM peeps";
    ResultSet rs = stmt.executeQuery(sql);

    //STEP 5: Extract data from result set
    while(rs.next()){
       //Retrieve by column name
       int id  = rs.getInt("id");
       String name = rs.getString("name");

       //Display values
       System.out.print("ID: " + id);
       System.out.println(", name: " + name);
    }
    //STEP 6: Clean-up environment
    rs.close();
    stmt.close();
    conn.close();
 }catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
 }catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
 }finally{
    //finally block used to close resources
    try{
       if(stmt!=null)
          stmt.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
       if(conn!=null)
          conn.close();
    }catch(SQLException se){
       se.printStackTrace();
    }//end finally try
 }//end try
 System.out.println("Goodbye!");
}//end main
}//end FirstExample  

Upvotes: 0

Views: 1493

Answers (2)

chrisinmtown
chrisinmtown

Reputation: 4324

You need the library - cannot simplify that away. Previously I'm guessing you used a web app server like JBoss that provided the library for you. Now you're outside the container.

First download the Connector/J jar (contains the JDBC driver class com.mysql.jdbc.Driver) from http://dev.mysql.com/downloads/connector/j/5.0.html.

Then with the jar file in the current working directory, invoke your program something like this:

java -cp mysql-connector-java-5.n.nn.bin.jar:. somepackage.TestJDBC

Also see this SO post: Including jars in classpath on commandline (javac or apt)

HTH

Upvotes: 2

duckstep
duckstep

Reputation: 1138

Locate (or upload) the mysql driver jar to the server, then run your code with

java -classpath /path/to/mysql-connector-java-5.0.8-bin.jar:. somepackage.TestJDBC

Adjust the version to whatever you're using.

Upvotes: 1

Related Questions