Maven – Add Java classpath to manifest for runtime

Previously we have talked about how to package a .jar artifact with dependencies included.


An alternative solution is to put your dependencies in the src/main/resources folder and they will be copied to the target folder together with the packaged .jar file. Then we can add the class paths to these dependencies in the Manifest with the help of the maven-jar-plugin.

The following project is a real life example which i use it to test the connection between the JBoss server and the MSSQL server.

1. Create the src/main/java/

package info.ykyuen.eureka.mssql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class ConnectMSSQLServer {

  public void dbConnect(String db_connect_string, String db_userid, String db_password) {
    try {

      Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);

      System.out.println("| Database server connected |");
      Statement statement = conn.createStatement();
      String queryString = "SELECT @@Version";
      ResultSet rs = statement.executeQuery(queryString);
    while ( {
    } catch (Exception e) {

  public static void main(String[] args) {
    ConnectMSSQLServer connServer = new ConnectMSSQLServer();
    connServer.dbConnect("jdbc:sqlserver://<sqlserver host>:<port>;databaseName=<database name>", "<username>", "<password>");


2. The program needs the sqljdbc4-4.0.jar in runtime but it is not available on the Maven Central repository. You can download it from here and save it at src/main/resources/runtime-lib/sqljdbc4-4.0.jar.

3. Create the following pom.xml at your project root.

<project xmlns=""


              <!-- Add the sqljdbc in runtime for the Class.forName() reflection call. -->


3. Package the .jar by mvn clean package.

4. After the build, the runtime-lib folder will be copied to target/classes automatically.

5. Now you can run java -jar .\target\test-connection-1.0-SNAPSHOT.jar to test the connection and the program knows where to find the sqljdbc4-4.0.jar as stated in the Manifest.

Done =)



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.