Jar file dependencies in Java 1.4

I am working on a small Java project that now connects to MS SQL Server 2000 database, but will soon connect to MS SQL Server 2005 database. I am creating a unified jar for easy deployment. I tried to set it up to just change the config file and change the drivers (just like in .NET). However, due to the limitations of the Java Jar embedded classpath and the lack of a wildcard on the embedded classpath (1). Is there a way to work around this problem without explicitly targeting each jar of drivers? If I need to do this, I will have to recompile every time the database changes ...

(1) : However, the class-class jar-manifest header does not respect the classpath group templates.

+2


source to share


4 answers


Typically, you can change the classpath at runtime . Approaches like this are a common way of working with "plugin" jars in Java (very similar requirements for your case).



+2


source


You can include information about which driver to use, eg. The configuration file. Or in the manifest of the JAR file itself. Just include all JAR files when starting the application, but load the driver name from the config.



0


source


I would argue that there is no Java way (as in standard practice) to include third party code in the same Jar that you deploy. However, the jar is just a zip file, so in most cases (barring some fancy things going on in the manifest) you can combine them if you need to.

That being said, you can include in your jar file a reference to the classpath for all possible JDBC drivers, or just call the JDBC driver jar correctly. Then create a config file in the same directory (be sure to include it in the JAR class path) and then read the driver name from it and use Class.forName () to load the driver.

You can do more interesting things (for example, find a suitable jar at runtime and load it dynamically even if it is not on the classpath), but these things are a little tricky, so there should be something simple like above.

You shouldn't recompile. You are not really doing JDBC right if you need to recompile when you change drivers.

0


source


You have to separate the driver jar file and the actual vendor specific JDBC driver name.

I would really recommend not including jdbc drivers in your jar. It's just in their way while they work. Likewise, you can get the name of the JDBC Driver Manager from system properties at runtime, or even a config file.

So, do the following:

java -jar myapp.jar -cp sqlserver.jar -DdriverManager=com.microsoft.sqlserver.jdbc.SQLServerDriver -DdbUrl=jdbc:some:url

      

and in your application, do something like this (I exclude exception handling):

Class.forName(System.getProperty("driverManager"));
Connection conn = DriverManager.getConnection(System.getProperty("dbUrl"))

      

;

This way you can change drivers simply by adding the appropriate jar file to the classpath and changing the driverManager and dbUrl properties. This way you don't need to recompile to support the new drivers.

This is the simplest solution I can think of.

0


source







All Articles