I’m new to Java but finding my way around quite well so far. My application now needs a connection to MySQL, so as per tons of tutorials, I do something like this:
Class.forName ("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection (url, userName, password);
That’s all fine, excepted that I need to have the location of the MySQL JDBC driver in my CLASSPATH if Java is to ever find the it. My development machine with Gentoo has it in /usr/share/jdbc-mysql/lib/jdbc-mysql.jar, but my Debian Server which will run the application has it in /usr/share/java/mysql.jar. So the location can be different on various Linux distributions.
I can set the classpath within Eclipse, but that’s pointless as on the deployment machine I won’t run it from within Eclipse. I could write a wrapper shell script that somehow finds out where the driver is and sets CLASSPATH but that seems painful.
What’s the correct way for my application to find the driver, no matter where the distribution stores it?
In other programming languages, I’m used to this being automatic. In perl I write “use Package::Name” and it just finds it, no matter where the distribution stores it. In C, dynamic libraries are automatically found in /usr/lib/.
Any help appreciated, thanks!
One possibility is not to rely on the OS distribution to provide the dependency and distribute it yourself. Most application work this way.
Alternately, the user can be asked to place the jar in a location known to your app.