0

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.

Mad Max
  • 79
  • 3
  • 10
  • 1
    Compile time is not the same as runtime. How are you trying to run your application? – Romski Oct 12 '12 at 12:24
  • Yes. I understend this. I'm trying to run it with `java -jar .jar` – Mad Max Oct 12 '12 at 12:28
  • So, do you want to know how to run your application from a command line having some external dependencies? – Renat Gilmanov Oct 12 '12 at 15:07
  • 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. Oct 13 '12 at 02:22

3 Answers3

0

try to change the path like this.

 <path id="libs.dir">
   <fileset dir="./lib" includes="**/*.jar"/>
 </path>
amicngh
  • 7,831
  • 3
  • 35
  • 54
  • that didn`t help. But I can compile my application with no problems. Seems like something wrong with classpath when I run the app – Mad Max Oct 12 '12 at 12:32
0

You need the manifestclasspath task.

Example:

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
0

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.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66