在 ant 中定义 Main-Class 来捆绑 jar 的混乱

2024-07-04

我正在尝试使用 Amazon Mechanical Turk API 构建一个 jar 文件。 SDK 附带了一个 helloworld 文件,我试图将其打包作为健全性检查 - 它位于此处:

http://aws.amazon.com/code/SDKs/695 http://aws.amazon.com/code/SDKs/695

设置完所有内容后,我可以使用 ant 使用提供的 build.xml 文件正确构建和执行。

bash-3.2$ ant helloworld
ant helloworld
Buildfile: /Users/astorer/Work/dtingley/java-aws-mturk-1.2.2/build.xml

compile-sample:
     [echo] Compiling the sample java source files...
    [javac] /Users/astorer/Work/dtingley/java-aws-mturk-1.2.2/build.xml:252: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

helloworld:
     [echo] Running the Hello World sample application...
     [java] Got account balance: 10000.00
     [java] Created HIT: 2RB2D5NQYN5F41KJ2IKYPNCW2H3A60
     [java] You may see your HIT with HITTypeId '22R58B727M0IHQ4HZEXYISVF4XCWBC' here: 
     [java] http://workersandbox.mturk.com/mturk/preview?groupId=22R58B727M0IHQ4HZEXYISVF4XCWBC
     [java] Success.

BUILD SUCCESSFUL
Total time: 11 seconds

我希望 helloworld 可由其他人执行,而无需安装库。似乎“正确”的方法是从 ant 内部构建一个 jar。

我的理解是我需要包括:

  • 必要的库,从 sdk 本身构建(并作为 .jar 提供)
  • 构建的 helloworld 类文件
  • 指定要运行的主类的清单属性

我不知道是否需要添加其他内容。我知道运行时有一个大而复杂的类路径,并且我可以在命令行上指定类路径,但我怀疑对类路径进行硬编码会阻止我分发 .jar 文件,这就是重点。

以下是该 jar 的 build.xml 片段:

  <target name="hellojar" depends="helloworld" description="Creates Jar of helloworld" >
    <jar destfile="helloworld.jar">
      <fileset file="${sdk.jar}" />
      <fileset dir="${sample.classes.dir}/helloworld/" />
      <fileset dir="."/>
      <manifest>
        <attribute name="Main-Class" value="MTurkHelloWorld" />
      </manifest>
    </jar>
  </target>

这建立了。然而,当我运行 jar 时,它崩溃了:

bash-3.2$ java -jar helloworld.jar 
java -jar helloworld.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: MTurkHelloWorld (wrong name: helloworld/MTurkHelloWorld)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

这是有道理的,因为我的 MTurkHelloWorld 实际上位于 helloworld 包中。因此,我应该改为:

  <manifest>
    <attribute name="Main-Class" value="helloworld.MTurkHelloWorld" />
  </manifest>

这样就构建成功了。当我运行它时:

bash-3.2$ java -jar helloworld.jar 
java -jar helloworld.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: helloworld/MTurkHelloWorld
Caused by: java.lang.ClassNotFoundException: helloworld.MTurkHelloWorld
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

我们可以调查 jar 中的文件:

jar tf helloworld.jar | grep hello
build/private/classes/samples/helloworld/
samples/helloworld/
build/private/classes/samples/helloworld/MTurkHelloWorld.class
samples/helloworld/MTurkHelloWorld.java

这表明如果类路径设置为 build/private/classes/samples/ ,它可能会正常工作:

<attribute name="Class-Path" value="build/private/classes/samples/helloworld" />

这会导致相同的错误。我认为我在这里缺少一些非常基本的东西,如果有任何帮助,我将不胜感激!


你的包文件夹必须直接启动,你不能将它们放在 jar 文件的任何子目录中:

它必须看起来像这样:

helloworld/
helloworld/MTurkHelloWorld.class

Your jar-task https://ant.apache.org/manual/Tasks/jar.html必须看起来像这样:

<jar destfile="helloworld.jar" basedir="${sample.classes.dir}">>
  <manifest>
    <attribute name="Main-Class" value="MTurkHelloWorld" />
  </manifest>
</jar>

basedir 是编译包的源。 和<fileset />您只需添加普通文件即可。

此外,您不能将外部库放入 jar 文件中。它们必须与 jar 位于同一文件夹中java classpath或在指定的文件夹中classpath-attribute http://docs.oracle.com/javase/1.4.2/docs/guide/jar/jar.html清单的。 或者你可以使用,将类包含到您的 jar 中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ant 中定义 Main-Class 来捆绑 jar 的混乱 的相关文章

随机推荐

  • 在 Bash 中使用大小写表示数字范围

    我正在尝试使用以下方法进行操作case在 Bash 中 在 Linux 中 如果X在460到660之间 则输出X信息 如果 X 介于 661 和 800 之间 请执行其他操作 Etc 现在这就是我所拥有的 case MovieRes in
  • 如何循环调用分页函数?

    我正在尝试使用空气桌API https airtable com api从我的数据中检索记录 具体来说 是我在列单元格中的 URL 列表 我写了一个函数 get airtable records 通过以下方式进行 API 调用curl它可以
  • OpenCV 2.4.6 SIFT 关键点检测使用大量内存

    我们在 openCV 2 4 3 中使用 SIFT 我们决定升级到 openCV 2 4 6 升级后 openCV 2 4 6 中的内存使用量从约 150MB 跃升至 1 2GB 有人知道这是一个错误还是我们现在需要配置的东西吗 我们的图像
  • PHP url 函数和子域

    我有一个网站thenoblesite com 它有一些子域 即 download thenoblesite com壁纸 thenoblesite com etc 子域的页面位于主 htdocs 文件夹中 即 httpdocs downloa
  • 字符串的两半就地交错

    给定一个字符串even尺寸 说 abcdef123456 我如何将两半交错 这样same字符串将变成这样 a1b2c3d4e5f6 我尝试开发一种算法 但失败了 有人能给我一些吗hints至于如何进行 我需要在不创建额外的字符串变量或数组的
  • Android CardView 不适用于 Api 21

    我正在使用安卓CardView它在 Api 21 下完美运行 但是当我在 Api 21 即 Lollipop 上使用它时 XML 属性如下cardElevation cornerRadius不起作用 我究竟做错了什么 这是我的 XML 布局
  • 如何解决“您的系统管理员已将 Chrome 配置为打开备用浏览器进行访问”

    我正在尝试在 google chrome 中打开一个应用程序 当我启动 chrome 网站时 它会重定向到 IE 并在 chrome 中显示消息 您的系统管理员已将 chrome 配置为打开备用浏览器进行访问 如何克服这个问题 Behavi
  • Socket:Python中的2路通信

    我想要在 Python 中进行双向通信 我想绑定到一个客户端可以连接的套接字 然后服务器和客户端可以彼此 聊天 我已经有了基本的监听器 import socket HOST localhost PORT 50008 s socket soc
  • 让所有街道在 Google 地图视口中可见

    我正在尝试使用以下算法构建地图 等待平移或缩放发生 查询视口中可见的所有街道 范围 使用预定义的颜色为每条可见街道着色 Example 我想显示每条街道上的企业数量 或者每条街道上发生的犯罪数量 我有一个数据库 其中包含此类信息 街道名称
  • PHAsset(或 ALAsset)跨设备标识符

    我实际上正在创建一个 iOS 应用程序 并且想要在 SQLite 数据库中存储有关照片库图片的一些详细信息 我还希望能够在用户拥有的不同设备上共享这些数据 想象一下有人想要在他的 iPhone 上重新组织图片 并想要在他的 iPad 上检索
  • 计算已发布字符串中的行数

    我试过这个 PHP 中计算文本区域中的新行以调整容器大小 https stackoverflow com questions 1743745 count new lines in textarea to resize container i
  • 没有可编译的体系结构(ARCHS=i386、VALID_ARCHS=arm64 armv7 armv7s)

    Preface 我确实看过类似的问题 https stackoverflow com questions 22328882 xcode 5 1 no architectures to compile for only active arch
  • 在 Scala 中指定 lambda 返回类型

    注意 这是一个理论问题 我并不是试图解决任何问题 也不是试图达到任何实际效果 在 Scala 中创建 lambda 时使用 arguments gt expression语法 可以显式提供返回类型吗 Lambda 与方法没有什么不同 它们都
  • String.Format() 中的 CultureInfo.CurrentCulture 真的有必要吗?

    您认为确实有必要提供哪些IFormatProvider在方法中String Format string object 写完整的变体是否更好 String Format CultureInfo CurrentCulture String is
  • 如何在Android 10及更高版本中通过intent MediaStore.ACTION_IMAGE_CAPTURE获取图像URI

    对于版主 我知道已经存在这样的问题 但所有这些方法最终都会通过提供位图data getExtra data 这实际上只是thumbnail 我想要获取 URI 而不是位图 并且我需要获取实际图像的 URI 而不是其缩略图 方法可在2021
  • 在 Python 中运行 Bash 命令

    在我的本地计算机上 我运行一个包含此行的 python 脚本 bashCommand cwm rdf test rdf ntriples gt test nt os system bashCommand 这很好用 然后我在服务器上运行相同的
  • 检查范围是否包含 Scala 中的值的通用方法

    我想编写一个通用类来保存范围的端点 但通用版本会出现编译错误 value gt is not a member of type parameter A final case class MinMax A lt Comparable A mi
  • 如何在Mandlebrot张量流程序中显示图像。当前输出是

    导入模拟库 import tensorflow as tf import numpy as np 导入可视化 from PIL Image from io import BytesIO from IPython display import
  • 在python绑定/clang中的get-includes中解析cpp文件时过滤目录

    我应该编写一个 python clang 解析器 它返回 cpp 文件中的所有包含内容 所以我使用像下面的代码这样的东西 def main from clang cindex import Index from optparse impor
  • 在 ant 中定义 Main-Class 来捆绑 jar 的混乱

    我正在尝试使用 Amazon Mechanical Turk API 构建一个 jar 文件 SDK 附带了一个 helloworld 文件 我试图将其打包作为健全性检查 它位于此处 http aws amazon com code SDK