Reputation: 4677
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!
Upvotes: 2
Views: 107
Reputation: 310884
Class.forName ("com.mysql.jdbc.Driver")
You haven't needed to do that at all for several JDBC releases.
Upvotes: 0
Reputation: 17765
It is going to depend on how you intend to build it. If you use vanilla java jar packaging for your source then chances are you'll need to use relative and not literal paths to make this work.
Upvotes: 0
Reputation: 11543
If you have an install script, then you could locate the jar file there and set the classpath.
However, what we usually do is to have a lib folder located with our java program and put the needed jars there. That way we also have control over what versions are used.
Upvotes: 0
Reputation: 52635
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.
Upvotes: 4