运行Jar文件时出现问题 [英] Problem running a Jar file

查看:171
本文介绍了运行Jar文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've compiled a java project into a Jar file, and am having issues running it.

当我运行时:

java -jar myJar.jar

我收到以下错误

Could not find the main class: myClass

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

The class file is not in the root directory of the jar so I've tried changing the path of the main class to match the path to the class file and I get the same issue.

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

Should I be flattening the file structure? if so how do I do this. I'm using Ant to build the Jar file if thats of any use.

UPDATE

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

Here is the contents of the jar and the relevant Ant sections, I've changed the name of the firm I work for to "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>


推荐答案

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

In your manifest file, make sure you have the attribute Main-Class set to the name of the class containing your main() method. For example, if the package-qualified name of the class is my.cool.Class, then, in your JAR manifest, Main-Class should be set to my.cool.Class.

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

Also made sure that you have a package declaration in your .java files (for example, in Class.java, make sure you have the proper package my.cool; declaration). Also, make sure your directory hierarchy is set up correctly (my.cool.Class should be in $SRC/my/cool/Class.java).

这篇关于运行Jar文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆