0
votes

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.

3
Compile time is not the same as runtime. How are you trying to run your application?Romski
Yes. I understend this. I'm trying to run it with java -jar <jarname>.jarMax Saichuk
So, do you want to know how to run your application from a command line having some external dependencies?Renat Gilmanov
Putting dependent jars in a lib or META-INF/lib folder of a jar will not work. It only works for war or ear archives because it's art of the JEE spec, and JEE containers are able to construct a classpath from there. Under normal circumstances, you have to explicitly set a classpath that includes these separate jars along with you jar.Patrice M.

3 Answers

0
votes

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.

0
votes

try to change the path like this.

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