运行 Jar 文件时出现问题 [英] Problem running a Jar file
问题描述
我已将一个 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.
更新
这是 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
中,请确保您有正确的 packagemy.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屋!