简体   繁体   中英

exception in thread main java.lang.noclassdeffounderror after build with ant

I builded and deployed a project using the following ant script:

<?xm1 vertion="1.0" ?>
<project name="my project" default="build" basedir=".">

    <path id="project-classpath">
        <fileset dir="lib" includes="*.jar" />
    </path>

    <target name="build">
        <echo>Build</echo>
        <antcall target="clean"/>
        <antcall target="init"/>
        <antcall target="compile"/>
        <antcall target="package"/>
        <antcall target="deploy"/>
    </target>

    <target name="clean">
        <echo>Clean</echo>
        <delete dir="build/classes"/>
        <delete dir="dist"/>
    </target>

    <target name="init" depends="clean">
        <echo>Init</echo>
        <mkdir dir="build/classes"/>
        <mkdir dir="dist"/>
    </target>

    <target name="compile" depends="init">
        <echo>Compile</echo>
        <javac srcdir="src/myproject" destdir="build/classes"  classpathref="project-classpath"/>
    </target>

    <target name= "package" depends= "compile">
        <echo>Package</echo>
        <jar destfile="dist/project.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="myproject.Main"/>
            </manifest>
        </jar>
    </target>

    <target name="deploy" depends="package">
        <echo>Deploy</echo>
        <echo message="Dir: ${user.home}"/>
        <copy todir="${user.home}">
            <fileset dir="dist"/>
        </copy>
    </target>

</project>

When I check my dir where I deploy the jar file I find the file project.jar, but when I run it using the command:

java -jar project.jar

I have the following some errors:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/core/MultivaluedMap at java.lang.Class.getDeclarativeMethods0(Native Method)...

(If I run using the IDE it works fine.)

In my project.jar I have the following tree structure:

project.jar

.....myproject (folder)

..........Main.class (file)

.....META-INF (folder)

..........MANIFEST.MF (file)

And the file MANIFEST.MF contains the following:

Manifest-Version: 1.0

Ant-Version: Apache Ant 1.9.6

Created-By: 1.7.0_79-b14 (Oracle Corporation)

Main-Class: myproject.Main

Anyone knows what I am doing wrong?

运行jar时,需要在类路径中包含javax.ws.rs-api jar库。

java -classpath javax.ws.rs-api-2.0.1.jar -jar project.jar

NoClassDefFoundError means that the class was present at compile-time but is missing at run-time. In other words, javax/ws/rs/core/MultivaluedMap is not present in the classpath when you run your generated jar.

In your IDE, your project is using external libreries (jars) that are not included in your generated jar.

So you need to add to the classpath needed libraries, in this case, jsr311-api .

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