简体   繁体   中英

Setting Class-Path in Ant build script

I'm trying to build my console application and I'm using Ant to build it. I can run my application in Eclipse, but when I try to run it from jar that I get - the ClassNotFoundException: is thrown. is in one of jars, that I use for my application. Here is a part of build.xml where I create manifest:

<manifest>
      <attribute name="Main-Class" value="com.package.Reporter" />
      <attribute name="Class-Path" value="lib/commons-httpclient-3.1.jar
        lib/commons-logging-api.jar
        ...lot of jars...
        lib/stax-api-1.0.1.jar" />
</manifest>

The required class is in commons-httpclient-3.1.jar And here is how I set up classpath for compiling, that is fine:

<path id="libs.dir">
    <fileset dir="lib" includes="**/*.jar"/>
</path>

UPD : Should I put jars with libs to my jar? Now I'm putting them to "lib" directory of my jar. So myjar.jar contains package with my classes, META-INF directory and lib directory.

try to change the path like this.

 <path id="libs.dir">
   <fileset dir="./lib" includes="**/*.jar"/>
 </path>

You need the manifestclasspath task.

Example:

Max, you can't insert jar libs into jar, assuming normal usage. Either you don't have to specify them manually at runtime as Romski suggested. When invoking java -jar myjar.jar it should locate all your jars provided that they are located in the lib directory. lib directory must be in the same directory that jar resides in. Doesn't matter if you call java executable directly or through ant java task.

Note that the current directory doesn't matter only the relation between the jar and the lib .

Now being overly explicit. Perform sanity test as follows: create a new tmp directory and copy files to it:

tmp/myjar.jar
tmp/lib/commons-httpclient-3.1.jar

Run java -jar tmp/myjar.jar

Edit: now I see I just wrote the same what is in Oracle jar tutorial . But I also made tests myself with a relative directory. I also see dozens of stackoverflow questions searching for jar in jar so please first search SO, then ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM