Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ClassNotFoundException:通过ANT构建在JAR文件中的主类_Java_Ant_Jar_Classnotfoundexception - Fatal编程技术网

Java ClassNotFoundException:通过ANT构建在JAR文件中的主类

Java ClassNotFoundException:通过ANT构建在JAR文件中的主类,java,ant,jar,classnotfoundexception,Java,Ant,Jar,Classnotfoundexception,基本上,我正在尝试运行我使用ANT构建文件创建的JAR文件。程序需要使用外部jar才能正确编译。我已经将其包含在JAR清单的类路径中。在运行时,它吐出一个堆栈跟踪,让我知道它找不到主类 堆栈跟踪如下所示: [java] java.lang.NoClassDefFoundError: $edu/gatech/oad/antlab/pkg1/AntLabMain [java] Caused by: java.lang.ClassNotFoundException: $edu.

基本上,我正在尝试运行我使用ANT构建文件创建的JAR文件。程序需要使用外部jar才能正确编译。我已经将其包含在JAR清单的类路径中。在运行时,它吐出一个堆栈跟踪,让我知道它找不到主类

堆栈跟踪如下所示:

        [java] java.lang.NoClassDefFoundError: $edu/gatech/oad/antlab/pkg1/AntLabMain
    [java] Caused by: java.lang.ClassNotFoundException: $edu.gatech.oad.antlab.pkg1.AntLabMain
    [java]  at java.net.URLClassLoader$1.run(Unknown Source)
    [java]  at java.security.AccessController.doPrivileged(Native Method)
    [java]  at java.net.URLClassLoader.findClass(Unknown Source)
    [java]  at java.lang.ClassLoader.loadClass(Unknown Source)
    [java]  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    [java]  at java.lang.ClassLoader.loadClass(Unknown Source)
    [java] Could not find the main class: $edu.gatech.oad.antlab.pkg1.AntLabMain. Program will exit.
    [java] Exception in thread "main" 
    [java] Java Result: 1
但是当我检查\edu\gatech\oad\antlab\pkg1下的jar文件时,AntLabMain.class就在那里。奇怪

我将在下面包含XML源代码,老实说,我不确定是什么导致了这个问题。我最好的猜测是我JAR文件的方式有问题,但我已经做了几个小时了,看不到尽头

<project name="My Buildfile" default="all" basedir=".">
<target name="init" depends="prepare" description="Initialize and set up properties">
    <property name="src.dir" location="./src/" />
    <property name="build.dir" location="build" />
    <property name="library.dir" location="lib" />
    <property name="apidoc.dir" location="apidoc" />
</target>

<target name="prepare" description="Making required directories">
    <mkdir dir="build" />
    <mkdir dir="apidoc" />
</target>

<target name="compile" depends="init" description="Compiling necessary files">
    <javac destdir="${build.dir}" includeantruntime="first">
        <src path="${src.dir}" />
        <classpath>
            <pathelement path="${build.dir}" />
            <fileset dir="${library.dir}" includes="**/*.jar" />
        </classpath>
    </javac>
</target>

<target name="javadoc" depends="init" description="Generating useful javadocs">
    <javadoc sourcepath="${src.dir}" access="public" destdir="${apidoc.dir}">
        <fileset dir="${src.dir}">
            <include name="**/*.java" />
        </fileset>
        <classpath>
            <fileset dir="${library.dir}" includes="**/*.jar" />
         </classpath>
    </javadoc>
</target>

<target name="jar" depends="compile" description="JARing files for distribution">
    <jar destfile="${build.dir}/AntLabRun_Test.jar" basedir="${build.dir}" excludes="**/*.jar">
        <manifest>
            <attribute name="Main-Class" value="edu.gatech.oad.antlab.pkg1.AntLabMain" />
            <attribute name="Class-Path" value="${library.dir}\resources.jar" />
        </manifest>
    </jar>
</target>

<target name="run" depends="jar" description="Now running the program">
    <java jar="${build.dir}/AntLabRun_Test.jar" fork="true">
        <classpath>
            <fileset dir="${library.dir}" includes="**/*.jar" />        
        </classpath>
    </java>
</target>

<target name="all" depends="run" description="Doing EVERYTHING!">
</target>

<target name="clean" depends="init" description="Cleaning Directories">
    <delete dir="${build.dir}" />
    <delete dir="${apidoc.dir}" />
</target>

查看清单文件,似乎
Main Class
属性在类名前面有一个额外的$。尝试删除它,重新编译jar,然后再次执行它


此外,如果您打算在其他计算机上运行这个jar,您可以考虑将清单文件中的<代码>类路径属性更改为相对路径..

作为最后的手段,您可以在构建文件夹中提取资源.jar(而不是将其包含在类路径中),并再次生成jar文件。(其中包含从resources.jar中提取的文件)。

其他人已经指出了类名上的$问题

问题,您的AntLabMain类是否有任何在加载该类时运行的静态初始值设定项?如果有,并且其中一个初始值设定项失败,则JVM将报告找不到AntLabMain类,因为它未能加载它。这确实是错误的错误,但您将看到


因此,如果类中确实有静态初始化,那么请检查以确保所有这些都正常工作。

结果证明答案很简单。我需要编辑init目标以将属性设置为值,而不是位置

i、 e



谢谢大家的回复。

您的AntLabMain是否包含公共静态void main(字符串[]args)方法?是的,AntLabMain.java包含一个main方法。它只打印一堆文本,表示我们已经完成了分配。但是它依赖于我在类路径中包含的resources.jar。我在主类属性中编辑了错误,但它仍然给我相同的错误消息。你能详细解释一下你的意思吗通过将class path属性设置为相对路径?当前,您的类路径是绝对路径。如果您尝试在没有resources.jar的另一台计算机上以完全相同的路径运行此jar,您将得到一个错误(尽管是关于从resources.jar中丢失某些类的不同错误)。此外,您应该会遇到一个稍有不同的错误。您确定正确地清理并重新编译了jar吗?是的,我尝试过手动和通过ant清理它,但没有效果。它仍然会发出完全相同的消息。但减去多余的$。我使用-debug参数运行ant,似乎所有需要的类文件都在编译中AntLabMain会初始化几个对象,其中一个包含在resources.jar文件中,而不是直接编译到jar中。问题可能是JVM在运行时找不到resources.jar文件吗?
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_21-b07 (Sun Microsystems Inc.)
Main-Class: edu.gatech.oad.antlab.pkg1.AntLabMain
Class-Path: C:\Users\Mike Sandt\workspace\M4\lib/resources.jar
<target name="init" depends="prepare" description="Initialize and set up properties">
    <property name="src.dir" location="./src/" />
    <property name="build.dir" value="build" />
    <property name="library.dir" value="lib" />
    <property name="apidoc.dir" value="apidoc" />
</target>