我想使用亚马逊机械土耳其语API构造一个jar文件。该SDK附带了我想了罐子的完整性检查一个HelloWorld文件 - 它位于:定义在捆绑一个罐子蚂蚁主类的混乱
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可以由其他人执行而无需安装库。看起来这样做的“正确”方法是从蚂蚁内构建一个jar。
我的理解是,我需要包括:
- 必要的库,从SDK本身(和一个.jar提供)
- 内置HelloWorld类文件
- manifest属性建立指定要运行的主类
我不知道是否需要包含任何其他内容。我知道在运行时有一个大的,复杂的类路径,我可以在命令行上指定类路径,但是我怀疑对类路径进行硬编码会阻止我分发.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>
此建立。当我运行的罐子,但是它崩溃,:
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 tf helloworld.jar | grep hello
build/private/classes/samples/helloworld/
samples/helloworld/
build/private/classes/samples/helloworld/MTurkHelloWorld.class
samples/helloworld/MTurkHelloWorld.java
这表明,也许如果classpath中被设定为建立/私营/班/样品/它会正常工作:
<attribute name="Class-Path" value="build/private/classes/samples/helloworld" />
这导致相同的错误。我认为这里有一些非常重要的东西,我很感激任何帮助!
您认为在这种情况下最佳做法是什么?要制作一个jar文件(使用一个jar文件),还是只给出说明,让大家自己安装这个包并使ant任务运行? – alexplanation
@alexplanation指令总是可能被误读。我建议(如果你直接给最终用户)一个zip文件(包含所有内容,只需要解压缩)。或者onejar方法。 – oers