-2

I'm working on a lab for a class where the teacher just said

Go here, install this, do the lab. "https://dev.mysql.com/downloads/connector/j/"

Now I followed that, installed mysql, ran a server, tested it with workbench, and a few queries. Works.

However the rest of the lab was in java, so I built a folder in VSCode, lab x, ran javac *.java, java Program

The error I got was "Client exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"

Now this makes no sense since I installed mysql with the JConnector, and added it to my computer's environmental variables, I looked online and all I'm finding is stuff about Maven and Netbeans, I'm not familiar with either. I just want to complete this lab.

I tried adding it to the folder, then saying "javac -cp mysql-connector-java-8.0.28.jar Program.java" but then any java file referenced within the same folder was a symbol it couldn't find.

DashWin
  • 9
  • 3
  • If you are calling `Class.forName` in your code, remove that. It is unnecessary ... and it breaks if you are using Connector/J version 8. The class FQN has changed. – Stephen C Apr 14 '22 at 03:24
  • If your teacher told you to use `Class.forName` ... they are about 10 years out of date. It stopped being necessary in Java 6 / JDBC 4.0. – Stephen C Apr 14 '22 at 03:27
  • Wow must be using an old text book then haha – DashWin Apr 14 '22 at 03:50
  • Actually ... according to the source code, using the old "com.mysql.jdbc.Driver" class name should still work in Connector/J version 8. There is a backwards compatibility hack. But the preferred driver class is now "com.mysql.cj.jdbc.Driver". And the preferred method is to use `DriverManager` rather than `Class.forName`; see https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html. My guess is that your real problem was simply that your runtime classpath was incorrect. – Stephen C Apr 14 '22 at 04:15
  • You might have installed the jar somewhere, but it sounds like that location isn't somewhere VSCode checks for jars. Perhaps this would help? https://stackoverflow.com/questions/50232557/visual-studio-code-java-extension-howto-add-jar-to-classpath – SOS Apr 14 '22 at 04:32
  • I just didn't understand classpaths, thank you for your comments though, see my new answer. – DashWin Apr 17 '22 at 23:51

1 Answers1

0

BAD ANSWER - Old

I figured it out.
I opened the jar file in 7zip, dragged out the com folder and put it in my lab folder.
Works!!

GOOD ANSWER - VSCode - New

  1. Download Java Extension for VSCode.
  2. Go to Command Pallet (ctrl+shift+P.) and write Create Java Project.
  3. Click on Build Tools
  4. Name it whatever your project's name is.
  5. Drag/Drop your Java files into the folder "src" in the project's folder.
  6. Look at the bottom left of your screen where it says Java Projects and expand it.
  7. Hover on the right side of Referenced Libraries and click the + button.
  8. In the popped up Open window find where you have Connector/J and select the .jar file (in this case, mysql-connector-java-8.0.28.jar) and click Select Jar Libraries
  9. Click the Refresh button on your Java Project and click Build Workspace and pick Full
  10. Restart VSCode
  11. Click the Run and Debug button / Press ctrl+shift+D
  12. Works!

GOOD ANSWER - Command Line - New

javac -classpath "[Project folder or just a "." if your console is in it];[folder with jarfile]\\[jarfile].jar" Program.java
java -classpath "[Project folder or just a "." if your console is in it] ; [folder with jarfile]\\[jarfile].jar" Program

In this specific case

javac -classpath ".;C:\\Program Files\\MySQL\\Connector J 8.0\\mysql-connector-java-8.0.28.jar" Program.java
java -classpath ".;C:\\Program Files\\MySQL\\Connector J 8.0\\mysql-connector-java-8.0.28.jar" Program

So in the background the VSCode Answer does what the Command Line Answer does.

Basically every time you run java or javac, java adds a thing implicitly pointing/referencing where your classes (compiled .java programs) are (in a folder, or a zipped folder called a .jar), to your java/javac commands called a classpath in between the java/javac and your .java file.

With the implicit

javac Program.java
java Program

Being equivalent to

javac -classpath "." Program.java
java -classpath "." Program

The implicity makes sense as you would be using the classes from the folder that the program is in. (Note: The "." means the folder the Commandline is in, navigated by cd/ChangeDirectory in Bash/PowerShell, see Linux/Mac/Unix Path Formats and/or Windows Path Formats ).

Now you might be wondering why there's a ";", this is to denote multiple classpaths, as you want to look into your projects, ".", and the library JDBC, if you wanted to add more classpaths add another ";", however on Linux/Mac/Unix you would add ":" instead of ";".

The reason the old Bad Answer worked was because the dragged folder was included in the implicit classpath.

The reason the old Bad Answer is a bad answer is because its reflective of poor practices and lack of understanding. Its poor practice for few reasons: the zipped files take up less space thus unzipping needlessly eats space, these files aren't yours so mixing them with yours could violate the terms of use of the author, and its a lot of work unzipping that you're doing for no reason.

DashWin
  • 9
  • 3