简体   繁体   中英

Runtime error while executing JAR file from CMD: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I've been working on a java jFrame project, with MySQL database, via Apache server provided by XAMPP, in NetBeans IDE. After completion, I built the project into a jar file. When the project is executed within the IDE, it works perfectly. But when the jar file is executed, from the cmd, the following error pops up: SEVERE: null...java.lang.ClassNotFoundException: com.mysql.jdbc.Driver . I've been searching for solutions all over the internet, but couldn't find anything apt.

JDK used: JDK 12 SQL Driver used: MySQL Connector/J 5.1.47

Thanks in advance.

You need to build a fat jar, it will contain all the dependencies. By default Netbeans does not build a fat jar.

Check this link: https://dzone.com/articles/how-build-fat-jar-using

Make sure your MySQL connector jar is included in a lib/ subfolder inside your main jar location. Then you can run your app like below (by specifying the classpath):

java -cp yourJar.jar:lib/* name.of.your.Main.class

Another option as mentioned would be to build a fat jar where all your external libs would be included inside it, in which case it could be run with java -jar yourFatjar.jar

Well, the problem occurred due to some technical issue with the NetBeans IDE. While building a project from the IDE, a 'lib' folder, containing all the associated (referenced) jar files, is also expected to be created along with the jar file, in the 'dist' folder. But here, the 'lib' folder was not being created. So, I just un-installed and re-installed the NetBeans software, and the issue was solved. Now, I've the 'lib' folder created automatically (as it should be), in the 'dist' folder and my final jar file is executing perfectly. Thanks to @btreport & @funkyjelly for the quick responses and help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM