Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding the current directory to a JAR file's classpath

My code is being deployed as a JAR file. The JAR contains a directory lib which contains a number of third-party JARs which my code requires. I've added these to the classpath, using Ant, via the MANIFEST.MF.

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 20.5-b03 (Sun Microsystems Inc.)
Main-Class: com.package.Class
Class-Path: ../lib/c3p0-0.9.1.2.jar ../lib/dbunit-2.4.8.jar ../lib/gua
va-10.0.1.jar ../lib/hsqldb.jar ../lib/javax.jms.jar ../lib/log4j-1.2
 .16.jar ../lib/mockito-all-1.9.0.jar ../lib/ojdbc14.jar ../lib/slf4j-
 api-1.6.4.jar ../lib/slf4j-log4j12-1.6.4.jar ../lib/xmlunit-1.3.jar

There is also a queries.properties file which is in the root of the JAR.

There are two further properties files which are required. I would like these to reside in the same directory as the JAR file and for the code to be able to locate them. I believe for the code to be able to locate these properties files, they need to be in the classpath. I therefore need to add the JAR file's directory to the classpath.

Firstly, is this the correct approach of should I use an alternative means of locating the properties files?

If this is correct, how do I use Ant to add the JAR's current directory to the classpath in MANIFEST.MF? I added the JARs in the lib directory to the classpath using the manifestclasspath Ant task.

like image 679
mip Avatar asked Mar 02 '12 11:03

mip


People also ask

Is current directory in classpath?

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings. Classpath entries that are neither directories nor archives (.

How do I run a Java class in a jar classpath?

To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]


1 Answers

Have you tried adding the . as a reference to the current directory?

   <jar jarfile="dummy.jar">
            <manifest>
                    <attribute name="Main-Class" value="com.package.Class"/>
                    <attribute name="Class-Path" value=". ${jar.class.path}"/>
            </manifest>               
    </jar>

Please also see Executable jar won't find the properties files

like image 138
nwinkler Avatar answered Sep 25 '22 01:09

nwinkler