2015-06-03 73 views
0

我想执行一个jar文件StartupUtil.jar,但它给出了无法找到并加载主类的错误。我看着其他类似的问题,并试图但无法弄清楚什么是错的。捆绑可执行jar文件 - 找不到主类

我给创建StartupUtil.jar结构是

- > com.ihc.startup.util.StartupService

- > META-INF/MANIFEST.MF

清单的内容是:

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:\Users\tgupta12\workspace_new\IHC_Startup\lib\bson-3.0.1 
.jar C:\Users\tgupta12\workspace_new\IHC_Startup\lib\mongodb-driver-3 
.0.1.jar C:\Users\tgupta12\workspace_new\IHC_Startup\lib\mongodb-driv 
er-core-3.0.1.jar C:\Users\tgupta12\workspace_new\IHC_Startup\classes 

这里是我的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> 

下面是我的build.properties文件:

#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

+0

那么*这个类是否存在?它在jar文件中吗? –

+0

是的,它存在于它的src目录下,并且包com.ihc.startup.util。当我提取jar并检查它存在于com/ihc/startup/util/StartupService.class时,它存在于jar文件中 –

+0

它仅启动,我犯了一个错字错误 –

回答

1

我强烈嫌疑人问题是它找不到依赖关系,这意味着它无法正确加载主类。我从来没有在清单中看到绝对文件名,我也不确定你是如何打破这些界限的(尽管可能是有效)。鉴于使用绝对文件名是不可移植的,我强烈建议你只使用相对的文件名。

更改您的清单只是:

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

+0

它现在可以工作了,我将名称更改为relative,并将这些库复制到与StartupUtil.jar相同的目录中。现在它工作正常。我添加了部分到build.xml来复制库。谢谢很多:) –

+0

有没有一种方法,我有一个多目录例如。 lib,lib1和我所有的jar都在这些目录中,我可以在clasppath中提供实际路径? –

+0

@Tushar:尝试一下,如果它不起作用,请提问一个有关详细信息的新问题。 –

相关问题