0

I've been looking on here for a couple of hours and tried a bunch of different solutions, but I haven't gotten any further. I've got my .jar file in my environment variables and all that. I made sure Eclipse has mysql-connector...jar in my build path.

I can't find the Deployment Assembly setting which leads me to believe there's something I have no idea exists that I'm doing wrong. Or maybe they took that feature out of photon? I feel like I've hit an adamantium wall.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class main {


public static void main(String[] args) {
    Connection conn = null;
try {
//Server connection details
Class.forName("com.mysql.jdbc.Driver");
String host = "jdbc:mysql://localhost/3306/db";
String userName = "admin";
String password = "admin";

 //Get the connection going.

conn = DriverManager.getConnection(host, userName, password);
}
catch (SQLException err) {
System.out.println(err.getMessage());
}
}}

Here's what I get:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

Unhandled exception type ClassNotFoundException

at main.main(main.java:12)

  • Can you add the stack trace too? From the looks of it, Eclipse can't find the mysql-connector.jar file in your application classpath. – Venkat D Sep 08 '18 at 21:05
  • Do you need more than the exception I added at the bottom? – Nick Tydryszewski Sep 08 '18 at 21:24
  • Line 12 - Class.forName("com.mysql.jdbc.Driver"); // This certainly looks like a Classpath issue where the compiler cannot find the Mysql driver classes, hence cannot load them to compile your main.java. Do re-check the location where your mysql-driver.jar is stored and ensure it is in your project classpath. – Venkat D Sep 08 '18 at 21:32
  • I went to the class path just to be sure. There is a classpathentry that leads directly to the mysql-connector-java-8.0.12.jar in the correct location. This is both a system variable and a user variable in my Environment Variables (Windows). – Nick Tydryszewski Sep 08 '18 at 21:50
  • Are you compiling main.java within Eclipse or from the command prompt on Windows? – Venkat D Sep 08 '18 at 21:57
  • If it is saved as a system variable and a user variable in windows, you should be able to compile it from the command prompt if 'javac' and 'java' are in the system path too. You can perhaps type "echo %CLASSPATH%" without the double quotes at the command prompt just to ensure mysql connector is indeed in your CLASSPATH. This is what I got -- D:\>echo %CLASSPATH% E:\Tomcat-7\lib\servlet-api.jar;E:\Tomcat-7\lib\jsp-api.jar;E:\Drivers\mysql-connector-java-5.1.18-bin.jar; – Venkat D Sep 08 '18 at 22:03
  • I am compiling within Eclipse. If I use echo %CLASSPATH% it just returns %CLASSPATH%. I'm not used to using the command line with Windows. But maybe this means the variable isn't set up correctly? Does the .jar need to be in the same directly as my java things? – Nick Tydryszewski Sep 09 '18 at 03:48
  • Which of these is line 12? – nitind Sep 09 '18 at 07:31

1 Answers1

0

It is obvious Eclipse can't find the MySQL Jdbc driver in your project class path. Follow the steps mentioned below in Eclipse -

  1. Window -> Preferences -> Connectivity -> Driver Definitions.

Check if MySQL JDBC Driver is visible/present in the Driver Definitions list as depicted in the image below. enter image description here

If MySQL JDBC DRiver is not visible, Click Add and a window will appear as shown in the following image.

enter image description here

Select MySQL from the Filter, select your MySQL version. If a MySQL driver is already registered with your Eclipse Project, you should be able to select that and click on Edit to check if it points to the correct JAR file in your file system.

If not, you can click on Add, select MySQL JDBC from the list, switch to the JAR tab, add the JAR file by locating it in the file system, click ok and you should be good to go.

If you add the MySQL JDBC Connector properly, I am most certain, the ClassNotFoundException will disappear.

However, you will have a new set of errors because you are catching only the SQLException in your main.java.

A quick dirty fix to make your source compile fast is to use Exception as the catch argument as follows -

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class main {


    public static void main(String[] args) {
        Connection conn = null;
    try {
     //Server connection details
     Class.forName("com.mysql.jdbc.Driver");
     String host = "jdbc:mysql://localhost/3306/db";
     String userName = "admin";
     String password = "admin";

     //Get the connection going.

     conn = DriverManager.getConnection(host, userName, password);
    }
    catch (Exception err) { // Catch All otherwise the compiler will flag an uncaught Connection exception
      System.out.println(err.getMessage());
    }
    }}

I hope this sorts out your compile+build issues. Afaik, Eclipse ignores Windows System ENV variables.

Venkat D
  • 127
  • 1
  • 10