1

I am creating an application that is utilizing JDBC and am currently having problems running the code without getting java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. I have done extensive research into trying to solve this issue and nothing i have tried seems to work.

For one, I would like to note that my code works fine when it is run from my IDE (intellij). The database connection works and all the queries execute as necessary. The problem comes when I try to compile and run using javac and java in the terminal. Becuase this is a school project, I will need to enable my professors to be able to run it locally.

Secondly, I am compiling the application using the same jar files that are also included within intellij:

javac -classpath lib/\* DatabaseDriver.java

The lib folder contains my JDBC jar file. You can find the two jar files in this folder here (json.org) and here (connector j). Everything compiles correctly but it seems that once the following execution occurs in the code (see below for the full code) the is a runtime excetion that says the class can not be found:

Class.forName("com.mysql.cj.jdbc.Driver"); // also tried com.mysql.jdbc.Driver

The following code is what I am trying to compile (not the full DatabaseDriver, just enough to reproduce the issue):

import org.json.JSONObject;
import java.sql.*;

public class DatabaseDriver {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";

    public static String runStatement(String sql) throws Exception {
        // Register JDBC driver
        Class.forName(JDBC_DRIVER);

        return "";
    }//end main

    public static void main(String[] args) throws Exception {
        DatabaseDriver.runStatement("SELECT * FROM employees LIMIT 10");
    }
}

This is the full stack that I get when I run the code:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at DatabaseDriver.runStatement(DatabaseDriver.java:15)
        at DatabaseDriver.main(DatabaseDriver.java:40)

Problem Summary: I am unable to run this script such that the jar file gets picked up even though it is the same jar file that my IDE is able to properly use. There must be something that I am forgetting or missing to do because all of the solutions I am finding only have to deal with people forgetting to include the correct jar files.

Shawn31313
  • 5,978
  • 4
  • 38
  • 80
  • 2
    You need to add the library also the the class path when you run the application (not only when you compile it) – Stefan Feb 15 '20 at 16:05
  • It's be `java` that requires the driver on the classpath. You could test the driver is available from `javac` by using the class directly instead of messing about with reflection. – Tom Hawtin - tackline Feb 15 '20 at 16:05
  • This is unrelated to your question, but you don't need to use Class.forName to preload the class. – MeBigFatGuy Feb 15 '20 at 16:06

1 Answers1

1

Run your class as given below:

In Unix based systems

java -cp ".:lib/*" DatabaseDriver

In MS Windows

java -cp ".;lib/*" DatabaseDriver
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110