问题描述
我正在尝试使用 build.xml 文件和提示代码构建一个项目的可运行 JAR 文件
I am trying to build a runnable JAR file of a project by using build.xml file and the prompt code
cd <the directory of you application>
ant -f bin/build.xml jar
但是我在运行 JAR 时出现异常,因为我无法添加外部 JAR.
But I have exceptions when I run the JAR because I cannot add the external JAR.
build.xml 如下:
the build.xml is as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
This file was generated by Jason 1.3.9-alpha
http://jason.sf.net
Ocak 11, 2014 - 01:09:12
-->
<project name ="virtualClassroom"
basedir=".."
default="run">
<property name="mas2j.project.file" value="virtualClassroom.mas2j"/>
<property name="debug" value=""/> <!-- use "-debug" to run in debug mode -->
<property name="build.dir" value="${basedir}/bin/classes" />
<property name="jasonJar" value="C:\Users\Emre\Desktop\MasterThesis-BDI\Jason-1.3.9\Jason-1.3.9\lib\jason.jar"/>
<property name="JavaCsBridgeJar" value="C:\Users\Emre\Desktop\MasterThesis-BDI\virtualClassroom\lib\JavaCsBridge.jar"/>
<path id="project.classpath">
<pathelement location="${basedir}"/>
<pathelement location="${build.dir}"/>
<pathelement location="${JavaCsBridgeJar}"/>
<pathelement location="${jasonJar}"/>
<fileset dir="${basedir}/lib" > <include name="*.jar" /> </fileset>
</path>
<!-- tasks the user can override in his/her c-build.xml script -->
<target name="user-init">
</target>
<target name="user-end">
</target>
<target name="init">
<mkdir dir="${build.dir}" />
<antcall target="user-init" />
</target>
<target name="compile" depends="init">
<condition property="srcdir" value="${basedir}/src/java" else="${basedir}" >
<available file="${basedir}/src/java" />
</condition>
<javac srcdir="${srcdir}" destdir="${build.dir}" debug="true" optimize="true" includeantruntime="false" >
<classpath refid="project.classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<delete file="${ant.project.name}.jar" />
<copy file="${jasonJar}" tofile="${ant.project.name}.jar" />
<copy file="${JavaCsBridge}" tofile="${ant.project.name}.jar" />
<copy file="${mas2j.project.file}" tofile="default.mas2j" />
<jar update="yes" jarfile="${ant.project.name}.jar" >
<fileset dir="${basedir}">
<include name="**/*.asl" />
<include name="**/*.mas2j" />
</fileset>
<fileset dir="${build.dir}">
<include name="**/*.class" />
</fileset>
<manifest>
<attribute name="Main-Class" value="jason.infra.centralised.RunCentralisedMAS"/>
</manifest>
</jar>
<delete file="default.mas2j" />
</target>
<target name="jnlp" depends="jar" >
<mkdir dir="${basedir}/${ant.project.name}-jws"/>
<java classname="jason.infra.centralised.CreateJNLP"
failonerror="true" fork="yes" dir="${basedir}/${ant.project.name}-jws" >
<classpath refid="project.classpath"/>
<arg line="${ant.project.name} ${mas2j.project.file}"/>
</java>
<copy todir="${basedir}/${ant.project.name}-jws" failonerror="no">
<fileset dir="${basedir}/lib" includes="**/*.jar" />
<fileset dir="${basedir}" includes="${ant.project.name}.jar" />
<fileset dir="C:\Users\Emre\Desktop\MasterThesis-BDI\Jason-1.3.9\Jason-1.3.9/src/images" includes="Jason-GMoreau-Icon.jpg" />
</copy>
<signjar jar="${basedir}/${ant.project.name}-jws/${ant.project.name}.jar" alias="jason"
storepass="rbjhja" keypass="rbjhja" keystore="C:\Users\Emre\Desktop\MasterThesis-BDI\Jason-1.3.9\Jason-1.3.9/src/jasonKeystore" />
<echo message="**" />
<echo message="** Java Web Start application created in directory ${ant.project.name}-jws" />
<echo message="** Update the codebase (in the second line of the .jnlp file)" />
<echo message="** with the URL where you will upload the application." />
<echo message="**" />
</target>
<target name="run" depends="compile" >
<echo message="Running project ${ant.project.name}" />
<java classname="jason.infra.centralised.RunCentralisedMAS"
failonerror="true" fork="yes" dir="${basedir}" >
<classpath refid="project.classpath"/>
<arg line="${mas2j.project.file} ${debug} "/>
<!-- jvmarg line="-Xmx500M -Xss8M"/ -->
</java>
<antcall target="user-end" />
</target>
<target name="clean" >
<delete failonerror="no" includeEmptyDirs="true" verbose="true">
<fileset dir="${basedir}" includes="**/*.class"/>
</delete>
</target>
</project>
有人知道该怎么做吗?
谢谢
推荐答案
首先,您需要准确地告诉我们哪个类给出了 NoClassDefFoundError
作为通用答案,解释是特定类在编译时可用,但在运行时丢失.这是 jar 可执行文件的常见问题.
Firstly, you need to tell us exactly which class is giving a NoClassDefFoundError
As a generic answer, the explanation is that the particular class was available during compile time but missing during runtime. This is a common issue with jar executables.
解决方案:要么将缺少的类包含在 jar 本身中,要么将它们添加到系统类路径中.
Solution : Either include the missing classes in the jar itself or add those to the system classpath.
这篇关于JAR 文件 NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!