【问题标题】:Jar does not run despite specifying correct main class, classpath, etc尽管指定了正确的主类、类路径等,但 Jar 无法运行
【发布时间】:2018-12-10 06:21:24
【问题描述】:

我的构建使用下面的build.xml 文件完美编译和运行。

唯一的问题是我创建了一个 jar,但它在没有给出 Could not find or load main class 错误的情况下无法运行。

我已经阅读了几个关于常见 Could not find or load main class 错误的 Stack Overflow 答案。但是,我无法在下面的build.xml 中发现我做错了什么。

build.xml

<?xml version="1.0" encoding="UTF-8"?>

<project default="run" name="My Project ">

<target name="run" depends="createjar">
    <java classname="com.company.program.project.MyMainClass">
        <classpath path="staging">
            <fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins">
              <include name="org.eclipse.swt.*.jar" />
            </fileset>
        </classpath>
    </java>
</target>

<target name="createjar" depends="compile">
  <jar destfile="./builds/jars/swtgui.jar" basedir="./staging/com/company/program/project" filesetmanifest="mergewithoutmain">
    <manifest>
      <attribute name="Main-Class" value="com.company.program.project.MyMainClass" />
    </manifest>

    <!-- 
        <fileset dir="./bin/com/company/program/project" includes="./*.class" />
    -->
    <fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins\" includes="org.eclipse.swt.win32.win32.x86_64_3.6.0.v3650b.jar" />
  </jar>
</target>

<target name="compile">
    <javac includeantruntime="false" srcdir="./src" destdir="staging">
        <classpath>
             <fileset dir="C:\COMPANY\Eclipse\3.6-64\plugins">
              <include name="org.eclipse.swt.*.jar" />
            </fileset>
        </classpath>
    </javac>

</target>

<record name="./MyMainClass.log" loglevel="verbose" action="start"/>

MyProject.log

createjar:
  [jar] found a total of 0 manifests in 2 resource collections
  [jar] A$1.class added as A$1.class is outdated.
  [jar] A$10.class added as A$10.class is outdated.
  [jar] A$11.class added as A$11.class is outdated.
  [jar] ...
  [jar] No sources found.
        [jar] Building jar: C:\my_workspace\my_project\builds\jars\swtgui.jar
  [jar] adding directory META-INF/
  [jar] adding entry META-INF/MANIFEST.MF
  [jar] adding entry A$1.class
  [jar] adding entry A$10.class
  [jar] adding entry A$11.class
  [jar] adding entry org.eclipse.swt.win32.win32.x86_64_3.6.0.v3650b.jar
  [jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.
Location: C:\my_workspace\my_project\build.xml:16: 
createjar:  duration 0 seconds

正确指定了主类。正确指定了类路径 (basedir)。它在编译完成之前等待执行。等等。

我在下面留下了我的日志文件,但似乎ant 没有提供任何关于它为什么找不到我的主类的线索。有没有人立即发现上述两个文件有什么问题?

【问题讨论】:

  • 我相信你的 jar 文件有类似 src/com/xxx 的结构 ....内部结构应该是 com/asd/... ..尝试更改 basedir="./staging/com/ company/program/project" 到 basedir="./staging/com/company/program/project/"

标签: java eclipse jar ant


【解决方案1】:

你试过eclipse-->导出-->可运行的jar文件选项吗?

当我尝试从我的测试摇摆应用程序手动创建一个 jar 时,我也遇到了这个问题。但是当我尝试这个选项时,它起作用了。

【讨论】:

    【解决方案2】:

    jar 任务的basedir 属性应与javac 任务的destdir 属性匹配。

    javac 任务写成

    <javac includeantruntime="false" srcdir="./src" destdir="staging">
    

    因此jar 任务应该写成

    <jar destfile="./builds/jars/swtgui.jar" basedir="staging" filesetmanifest="mergewithoutmain">
    

    由于您的包被定义为com.company.program.project,Java 编译器和 JVM 期望您的类在文件夹 com/company/program/project 中,但您当前的 jar 任务将它们放入 jar 文件的根文件夹中。

    【讨论】:

    • 非常好。不过,它通过了Could not find or load main class error. 它看不到单独的库(SWT)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2015-10-31
    • 2021-10-28
    • 2011-08-31
    • 2018-09-05
    • 2011-02-16
    相关资源
    最近更新 更多