Search code examples
javamysqljdbcclassnotfoundexception

Class.forName(“com.mysql.jdbc.Driver”) does not work on Raspberry Pi


this code:

        Class.forName("com.mysql.jdbc.Driver"); 

leads to an

ClassNotFoundException

on raspberry Pi. although my Classpath is correct

pi@raspberrypi ~ $ echo $CLASSPATH
/usr/share/java/mysql-connector-java.jar

on Windows it just works fine! any idea what I'm doing wrong?

I also tried to start my Application with

java -cp /usr/share/java/mysql-connector-java.jar -jar myApp.jar

thanks florian


Solution

  • You're using -jar, which makes the -cp part irrelevant. From the documentation:

    When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored.

    Either add ClassPath entries in your jar manifest, or add the MySQL connector jar file to an extension directory (e.g. jre/lib/ext). Using the manifest is the cleanest approach though.