运行Jar文件时出现问题

时间:2008-11-21 15:05:02

标签: java ant jar mainclass

我已经将一个java项目编译成一个Jar文件,并且在运行它时遇到了问题。

当我跑步时:

java -jar myJar.jar

我收到以下错误

Could not find the main class: myClass

类文件不在jar的根目录中,所以我尝试更改主类的路径以匹配类文件的路径,我得到同样的问题。

我应该扁平化文件结构吗?如果是这样我该怎么做如果有任何用途,我正在使用Ant构建Jar文件。

更新

以下是jar和相关Ant部分的内容,我已将我工作的公司名称更改为“org”:

META-INF/
META-INF/MANIFEST.MF
dataAccessLayer/
dataAccessLayer/databaseTest.class
org/
org/eventService/
org/eventService/DatabaseObject.class
org/eventService/DatabaseObjectFactory.class
org/eventService/DbEventClientImpl$HearBeatMonitor.class
org/eventService/DbEventClientImpl.class
org/eventService/EmptyQueryListException.class
org/eventService/EventHandlerWorkItem.class
org/eventService/EventProcessor.class
org/eventService/EventTypeEnum.class
org/eventService/EventWorkQueue$MonitorThread.class
org/eventService/EventWorkQueue$PoolWorker.class
org/eventService/EventWorkQueue.class
org/eventService/FailedToLoadDriverException.class
org/eventService/IConnectionFailureListener.class
org/eventService/InvalidEventTypeException.class
org/eventService/JdbcInterfaceConnection.class
org/eventService/NullArgumentException.class
org/eventService/OracleDatabaseObject.class
org/eventService/ProactiveClientEventLogger.class
org/eventService/ProactiveClientEventLoggerException.class
org/eventService/PropertyMap.class
org/eventService/SQLServerDatabaseObject.class
org/eventService/TestHarness.class
org/eventService/Utilities.class

蚂蚁目标:

<target name="compile" depends="init" description="compile the source ">
    <javac srcdir="src" destdir="bin" classpathref="project.class.path"/>
</target>
<target name="buildjar" description="build jar file" depends="compile">
    <mkdir dir="dist"/>
    <jar destfile="dist/myJar.jar" basedir="bin" includes="**/*.class" >
        <manifest>
            <attribute name="Main-Class" value="org.eventService.ProactiveClientEventLogger"/>
        </manifest>
     </jar>
</target>

2 个答案:

答案 0 :(得分:6)

在您的清单文件中,请确保将属性Main-Class设置为包含main()方法的类的名称。例如,如果类的包限定名称为my.cool.Class,则在您的JAR清单中,Main-Class应设置为my.cool.Class

还要确保您的.java文件中包含声明(例如,在Class.java中,请确保您拥有正确的package my.cool;声明)。另外,请确保您的目录层次结构设置正确(my.cool.Class应该在$SRC/my/cool/Class.java)。

答案 1 :(得分:4)

您应该在jar创建期间使用完整路径指定主类,例如:

<target name="jar" depends="compile">
     <delete file="myJar.jar"/>
     <delete file="MANIFEST.MF"/>
     <manifest file="MANIFEST.MF">
        <attribute name="Main-Class" value="my.package.myClass"/>
    </manifest>

    <jar destfile="myJar.jar"
           basedir="."
           includes="**/*.class"
           manifest="MANIFEST.MF" />
</target>