0

I was wondering if someone could help me with a serious issue... I need to create a jar from a project using a lot of dependencies... Problem is : my build.xml file I wrote with my own hand is not working. More specifically it runs without any problem, just a warning :

Buildfile: C:\Users\Alexis Fasquel\workspace\FirstEditServer\build.xml
clean:
   [delete] Deleting directory C:\Users\Alexis Fasquel\workspace\FirstEditServer\bin
   [delete] Deleting directory C:\Users\Alexis Fasquel\workspace\FirstEditServer\dist
makedir:
    [mkdir] Created dir: C:\Users\Alexis Fasquel\workspace\FirstEditServer\bin
    [mkdir] Created dir: C:\Users\Alexis Fasquel\workspace\FirstEditServer\dist
compile:
    [javac] C:\Users\Alexis Fasquel\workspace\FirstEditServer\build.xml:101: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 14 source files to C:\Users\Alexis Fasquel\workspace\FirstEditServer\bin
jar:
      [jar] Building jar: C:\Users\Alexis Fasquel\workspace\FirstEditServer\dist\firstEditServer.jar
main:
BUILD SUCCESSFUL
Total time: 1 second

But unfortunately I still get some classNotFoundDefError


I didn't print the full trace because it's the first class that i tried to instanciate which is not found... So i'm guessing nothing's working... but here it is :

Exception in thread "main" java.lang.NoClassDefFoundError: gate/util/GateExcept
on
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: gate.util.GateException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more

There is my build.xml file... Could someone take a look at it, and maybe find what's my error ?

<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->
    <property name="src.dir" location="src" />
    <property name="bin.dir" location="bin" />
    <property name="gate.dir" location="gate" />
    <property name="virtuoso.dir" location="virtuoso" />
    <property name="recommendation.dir" location="recommendation" />
    <property name="dist.dir" location="dist" />

    <path id="bin.classpath">
        <fileset dir="${virtuoso.dir}">
            <include name="openrdf-sesame-2.6.5-onejar.jar" />
            <include name="slf4j-api-1.5.0.jar" />
            <include name="slf4j-jdk14-1.5.0.jar" />
            <include name="virt_sesame2.jar" />
            <include name="virtjdbc3.jar" />
        </fileset>

        <fileset dir="${gate.dir}">
            <include name="plugins\Gazetteer_LKB\Gazetteer_LKB.jar" />
            <include name="lib\gate.jar" />
            <include name="lib\log4j.jar" />
            <include name="lib\commons-io.jar" />
            <include name="plugins\Tagger_Framework\TaggerFramework.jar" />
            <include name="lib\ant-contrib.jar" />
            <include name="lib\ant-launcher.jar" />
            <include name="lib\ant.jar" />
            <include name="lib\aopalliance.jar" />
            <include name="lib\apache-mime4j-core.jar" />
            <include name="lib\apache-mime4j-dom.jar" />
            <include name="lib\bcmail-jdk15.jar" />
            <include name="lib\bcprov-jdk15.jar" />
            <include name="lib\commons-codec.jar" />
            <include name="lib\commons-compress.jar" />
            <include name="lib\commons-lang.jar" />
            <include name="lib\commons-logging.jar" />
            <include name="lib\dom4j.jar" />
            <include name="lib\fontbox.jar" />
            <include name="lib\gate-asm.jar" />
            <include name="lib\gate-compiler-jdt.jar" />
            <include name="lib\lucene-core.jar" />
            <include name="lib\metadata-extractor.jar" />
            <include name="lib\nekohtml.jar" />
            <include name="lib\pdfbox.jar" />
            <include name="lib\poi-ooxml-schemas.jar" />
            <include name="lib\poi-ooxml.jar" />
            <include name="lib\poi-scratchpad.jar" />
            <include name="lib\poi.jar" />
            <include name="lib\spring-aop.jar" />
            <include name="lib\spring-beans.jar" />
            <include name="lib\spring-core.jar" />
            <include name="lib\tika-core.jar" />
            <include name="lib\tika-parsers.jar" />
            <include name="lib\wstx-lgpl.jar" />
            <include name="lib\xercesImpl.jar" />
            <include name="lib\xmlbeans.jar" />
            <include name="lib\xmlunit.jar" />
            <include name="lib\xpp3.jar" />
            <include name="lib\xstream.jar" />
            <include name="lib\ivy.jar" />
            <include name="lib\java-getopt.jar" />
            <include name="lib\java2html.jar" />
            <include name="lib\javacc.jar" />
            <include name="lib\jaxen.jar" />
            <include name="lib\jdom.jar" />
            <include name="lib\jempbox.jar" />
            <include name="lib\junit.jar" />
            <include name="plugins\Gazetteer_LKB\lib\commons-cli-1.1.jar" />
            <include name="plugins\Gazetteer_LKB\lib\commons-collections-3.2.1.jar" />
            <include name="plugins\Gazetteer_LKB\lib\commons-httpclient-3.1.jar" />
            <include name="plugins\Gazetteer_LKB\lib\kim-api-3.0-RC5.jar" />
            <include name="plugins\Gazetteer_LKB\lib\kim-util-3.0-RC5.jar" />
            <include name="plugins\Gazetteer_LKB\lib\owlim-lite-4.3.jar" />
            <include name="plugins\Gazetteer_LKB\lib\trove4j-2.0.2.jar" />
            <include name="plugins\Gazetteer_LKB\lib\openrdf-sesame-2.6.3-onejar.jar" />
            <include name="plugins\Gazetteer_LKB\lib\slf4j-api-1.5.11.jar" />
            <include name="plugins\Gazetteer_LKB\lib\slf4j-log4j12-1.5.11.jar" />
        </fileset>

        <fileset dir="${recommendation.dir}">
            <include name="simmetrics_jar_v1_6_2_d07_02_07.jar" />
        </fileset>
    </path>

    <!-- Deletes the existing bin, docs and dist directory-->
    <target name="clean">
        <delete dir="${bin.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <!-- Creates the  bin and dist directory-->
    <target name="makedir">
        <mkdir dir="${bin.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>

    <!-- Compiles the java code (including the usage of library for JUnit -->
    <target name="compile" depends="clean, makedir">
        <javac srcdir="${src.dir}" destdir="${bin.dir}" classpathref="bin.classpath">
        </javac>

    </target>


    <!--Creates the deployable jar file  -->
    <target name="jar" depends="compile">
        <jar destfile="${dist.dir}\firstEditServer.jar" basedir="${bin.dir}">
            <manifest>
                <attribute name="Main-Class" value="com.firstedit.main.Main" />
            </manifest>
        </jar>
    </target>

    <target name="main" depends="compile, jar">
        <description>Main target</description>
    </target>

</project>

As you can see, GateException is really in the gate.jar...

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
Aleks
  • 410
  • 4
  • 22
  • 3
    Adding "it's very urgent" will not speed up the process of someone answering your query. Also, please put the full stack trace, not just the top line. – David Jun 08 '12 at 12:51
  • You don't even say what class def isn't found. – Dave Newton Jun 08 '12 at 12:52
  • First of all, i know that it will not speed the process, it's simply the truth... Second, the full trace is useless because it's the first class that i tried to instanciate... But i'll edit to print the full trace as you wish. – Aleks Jun 08 '12 at 13:02
  • How are you starting the application? – Mark Rotteveel Jun 08 '12 at 13:11
  • 2
    Silly question: have you verified that gate/util/GateExcept really is in one of those jars you include? I mean, really uncompress the jar and look into it. This is my top mistake – Miquel Jun 08 '12 at 13:12
  • Ignore for now since you're in a hurry, but, in the future, you might consider using maven for projects with such a long list of dependencies – Miquel Jun 08 '12 at 13:12
  • Is `gate.util.GateExcept` packed in jar? It seems no. Is it compiled in bin? Hope you are not using IDE like Eclipse when checking, it can build some classes that ant didn't so you won't find the problem, try to use just command line for examination... – Betlista Jun 08 '12 at 13:14
  • I had no plobem using this jar with eclipse, so i can't see why it would cause a problem when i'm exporting my project.... – Aleks Jun 08 '12 at 13:17
  • Has it ever worked (when built without using Ant) for you? Did you compare the `Manifest.mf` of the version where it worked and the one where it didn't? – adarshr Jun 08 '12 at 13:36

4 Answers4

1

Assuming just for a moment that the class it isn't finding is in the jar file lib\gate.jar, I would say that what is happening is you are constructing the jar (firstEditServer.jar) file from the source of your project and that is all. In order to execute that jar, you would also need to ensure all of the jar files in your lib... directory are also in the classpath.

Now, if the intention is that you want a single jar file constructed that will include all classes in the library jars then you have to build your jar file slightly differently. You can't build an executable jar (one with a Main-Class attribute) that includes other jars. The standard classloader will not look inside one jar for other jar files. So you need to expand the library jars classes into your final jar.

Ant provides the support for this using something like the below. The zipgroupfileset is a task that will take a list of jar files, expand them and include the classes themselves into the target jar. I filter out manifest.mf and properties files so they do not collide with my own (such as a log4j.properties file in a library jar).

This creates a single jar with all contents of all lib jars. Manifest files will collide and the last manifest file seen wins.

<jar destfile='${build.dir}/lib-jars.jar'>
   <zipgroupfileset dir='${lib.dir}'>
     <include name='**/*.jar'/>
   </zipgroupfileset>
</jar>

The zipfileset used here with a single jar can filter out any contents from the one jar desired. This removes the MANIFEST.MF file, but could also remove other files with additional exclude tags.

<jar destfile='${jar.file}' basedir='${classes.dir}'>
  <zipfileset src='${build.dir}/lib-jars.jar'>
    <exclude name="META-INF/MANIFEST.MF"/>
  </zipfileset>
  <manifest>
    <attribute name="Built-By" value="${user.name}"/>
    <attribute name="Main-Class" value="${main.class}"/>
    <section name="common">
      <attribute name="Specification-Title" value="${project.title}"/>
      <attribute name="Specification-Version" value="${release.version}"/>
      <attribute name="Specification-Vendor" value="${vendor}"/>
      <attribute name="Implementation-Title" value="${project.title}"/>
      <attribute name="Implementation-Version" value="${release.version} ${TODAY}"/> 
      <attribute name="Implementation-Vendor" value="${vendor}"/>
    </section>
  </manifest>      
</jar>
Jere
  • 581
  • 2
  • 3
  • Hey ! Thanks for the answers, but what if I don't want all my dependencies jar into my firstEditServer.jar ? – Aleks Jun 08 '12 at 13:49
  • I often have my lib directory split like **lib/runtime** and **lib/provided** then I'll put the jars I want in the final jar file into **lib/runtime** and change the **dir** in the **zipgroupfileset** to that subdirectory only. – Jere Jun 08 '12 at 14:08
  • To be honest, i think we misanterstood each other... I don't care if there is any libs in my jar. I would just want my jar to work, so i'm not sure your solutions is accurate anymore, is it ? – Aleks Jun 08 '12 at 14:18
  • For your jar to work when you run it, you need to make sure all the classes your code uses are also available in the classpath. So, if you don't include lib classes in your jar, you must include the lib jars in your classpath. So it is really a matter of how you want to deploy - one jar or multiple. – Jere Jun 08 '12 at 16:17
  • I don't see how the 'exclude' directives could possibly "filter out manifest.mf and properties files so they do not collide." Why would 'include' apply to the jars to be flattened while 'exclude' refers to contents of said jars? – Hollis Waite Dec 08 '13 at 21:00
  • Late reply, you're right on excluding the files inside the jars when using a **zipgroupfileset**. If you want to do that, you first have to create a single jar from the contents of all the lib jars, then use a **zipfileset** on the single jar. Edited to clean that up. – Jere Jan 08 '14 at 20:41
0

Make sure your lib folder is in your classpath. I have something similar i did myself like you are. The build is okay because ant finds the jars. If you are building a jar, make sure your manifest adds the jars to your classpath in your manifest file. There's a manifest option and you can have ant add a manifest file to your jar. If nothing else run your program on command line and use the -classpath option to point to your lib folder. I think your issue is around your classpath though, I've fought this battle many times.

Logan
  • 2,369
  • 19
  • 20
0

This is not an answer, but it's too long for comment, so...

You can these steps:

  1. add simple main method to gate.util.GateExcept
public static void main( final String[] args ) {
    System.out.println( GateExcept.class.getName() );
}
  1. build the jar again (let it call myjar.jar)

  2. run this command

    java -cp myjar.jar gate.util.GateExcept

  3. if that works fine try (executable jar)

    java -jar myjar.jar

It seems to me that on screenshot you have gate.util.GateException, but in exception stack there is only gate.util.GateExcep (but probably this is just SO problem).

Let me know if that works.

Betlista
  • 10,327
  • 13
  • 69
  • 110
0

Correct me if I'm wrong, but you're not running this jar using ANT are you? ANT is only used to build the jar file correct?

There are two ways to launch your java program

  1. java -jar myapp.jar
  2. java -cp myapp.jar MyAppClass

The first requires you to set the "Main-Class" and "Class-Path" manifest entries. The second requires you to list each dependency explicitly.

I suspect you are trying the former, but forgetting to include the classpath..... The following question had the same problem. It outlines a solution that uses the ANT manifestclasspath task to simplify the classpath setting in your manifest:

Cannot find Main Class in File Compiled With Ant

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