I have Mysql installed on my Linux box and wrote a sample program to access one of it’s table.
I am using ‘mysql-connector-java-5.1.10.jar’
The code is working fine if i put the jar in ‘jre/lib/ext’. However, other ways of recognizing that jar are not working. I tried with setting $CLASSPATH and tried to use ‘.’ current directory.
It’s failing with the following error :
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
I usually don’t use the global
$CLASSPATHvariable, the easiest way to get it running isSidenote
If you have your application exportet to a jar with a
Main-Classattribute (“executable jar”) and start it withjava -jar myjar.jar, then you have to add all required libraries to the jars manifest,$CLASSPATHand-cpare ignored in this case. And that’s why I usually don’t use the-jaroption…Edit
To answer your additional question: If the current directory was added to the classpath by default, then the location from where the application was started could influence the application itself.
Imagine an application inside a jar and a start command
Now we have a defined environment: only the content of application.jar (and the jre classes) are on the classpath and part of the application. If the current directory was added to the classpath automatically then all files at the current location (and at locations of all subfolders) would be on the classpath too, intend or not. With a result, that the application might work if started from the users home directory but maybe not if started from the root directory (
/).