我正在尝试执行jar文件StartupUtil.jar,但它给出了无法找到并加载主类的错误。我看了其他类似的问题,试了试,但没能找出问题的所在。
我创建的StartupUtil.jar结构是
-> com.ihc.startup.util.StartupService
-> meta - inf/MANIFEST。MF
MANIFEST的内容为:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_79-b15 (Oracle Corporation)
Main-Class: com.ihc.startup.util.StartupService
Class-Path: C:Userstgupta12workspace_newIHC_Startuplibbson-3.0.1
.jar C:Userstgupta12workspace_newIHC_Startuplibmongodb-driver-3
.0.1.jar C:Userstgupta12workspace_newIHC_Startuplibmongodb-driv
er-core-3.0.1.jar C:Userstgupta12workspace_newIHC_Startupclasses
这是我的build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="Startup" default="build" basedir=".">
<property file="./build.properties" />
<path id="lib-classpath">
<fileset dir="${libApp.dir}">
<include name="*.jar"/>
</fileset>
<pathelement path="${bin.dir}"/>
</path>
<target name="build" description="Compile main source tree java files">
<echo message=" Build Startup Utility" />
<mkdir dir="${bin.dir}"/>
<echo message=" Compiling source files" />
<javac destdir="${bin.dir}" source="${versionJDK}" target="${versionTarget}" debug="true"
deprecation="false" optimize="false" failonerror="true" includeantruntime="false">
<src path="${src.dir}"/>
<classpath refid="lib-classpath"/>
</javac>
<echo message=" ...Compilation of source files OK" />
<echo message=" Generating JAR for Startup - StartupUtility.jar" />
<delete file="${out.dir}/${startup-util-name}" />
<!-- convert classpath to a flat list/string -->
<pathconvert property="lib.classpath" pathsep=" ">
<path refid="lib-classpath" />
<!--<flattenmapper />-->
</pathconvert>
<jar destfile = "${out.dir}/${startup-util-name}" basedir = "${bin.dir}" includes = "**/*">
<manifest >
<attribute name="Class-Path" value="${lib.classpath}" />
<attribute name="Main-Class" value="com.ihc.startup.util.StartupService"/>
</manifest>
</jar>
<echo message=" ...JAR Created for Startup" />
</target>
<target name="run" depends="build">
<java jar="${out.dir}/${startup-util-name}" fork="true"/>
</target>
下面是我的构建。属性文件:
#Directories
build.dir=build
src.dir=src
libApp.dir=lib
out.dir=out
web.dir=WebContent/WEB-INF
bin.dir=classes
webcontent.dir=WebContent
#File Name
war-file-name=StartupService.war
startup-util-name=StartupUtil.jar
#Target Properties
versionJDK=1.7
versionTarget=1.7
当它尝试执行目标运行时,它给出
错误:无法找到或加载主类com.ihc.startup.util.StartupService
我强烈怀疑问题是它找不到依赖项,这意味着它不能正确加载主类。我以前从未见过清单中给出的绝对文件名,也不相信您是如何断行的(尽管可能是有效的)。考虑到使用绝对文件名是不可移植的,我强烈建议您使用相对文件名。
将您的清单更改为:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_79-b15 (Oracle Corporation)
Main-Class: com.ihc.startup.util.StartupService
Class-Path: bson-3.0.1.jar mongodb-driver-3.0.1.jar mongodb-driver-core-3.0.1.jar
然后将这些jar文件放到与StartupUtil.jar
相同的目录下。