I have a java file hello.java, which has a JDBC connection , while running it, I am getting the error(Ubuntu 18.04 LTS) :
javac hello.java
java hello
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at hello.main(hello.java:22)
Goodbye!
And when I try with the classpath, it gives :
javac -cp /usr/share/java/mysql-connector-java-8.0.23.jar hello.java
java -cp /usr/share/java/mysql-connector-java-8.0.23.jar hello
Error: Could not find or load main class hello
I also tried with mysql-connector-java-5.1.45.jar, still the same issue.
And java -version gives :
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~18.04-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)
I tried the following questions :
- ClassNotFoundException com.mysql.jdbc.Driver
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error even after importing library
- How to fix 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' after adding it to the build path and registered using Class.forName();
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse
But got no satisfactory result. Does anyone know how to fix this?
Here is my code, hello.java :
//STEP 1. Import required packages 
import java.sql.*; 
public class hello { 
    // JDBC driver name and database URL 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    static final String DB_URL = "jdbc:mysql://localhost/data"; 
    // Database credentials 
    static final String USER = "newuser"; 
    static final String PASS = "password"; 
    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 * from temp"; 
        ResultSet rs = stmt.executeQuery(sql); 
        //STEP 5: Extract data from result set 
        while(rs.next()){ 
            //Retrieve by column name 
            int id = rs.getInt("id"); 
            int age = rs.getInt("age"); 
            String first = rs.getString("first"); 
            String last = rs.getString("last"); 
            //Display values System.out.print("ID: " + id); 
            System.out.print(", Age: " + age); 
            System.out.print(", First: " + first); 
            System.out.println(", Last: " + last); 
        } 
        //STEP 6: Clean-up environment 
        rs.close(); 
        stmt.close(); 
        conn.close(); 
        }catch(Exception e){ 
            //Handle errors for JDBC 
            e.printStackTrace(); 
        } finally{ 
            //finally block used to close resources 
            try{ 
                if(stmt!=null) {
                    stmt.close(); 
                }
            }catch(SQLException se){ 
            // nothing we can do 
            se.printStackTrace(); 
            }
            try{ 
                if(conn!=null) conn.close();
            }catch(SQLException se){ 
                se.printStackTrace(); 
            }
            System.out.println("Goodbye!"); 
        }//end main 
    }//end FirstExample
} 
 
    