如何将库与我的jar组合? [英] How to combine library with my jar?

查看:103
本文介绍了如何将库与我的jar组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我编写了一个程序,它使用了第三方开源库,我希望将它与我的程序打包在一个jar中。我正在使用netbeans 6.8和我尝试过的所有java总是吐出错误:

Ok so i wrote a program that makes use of a 3rd party open source library and i want to package it with my program in a single jar. I'm using netbeans 6.8 and everything I've tried java always spit back the error:

java.lang.NoClassDefFoundError: libraryname;

off topic :我也想知道如何制作可执行文件-jar(exe)如果有可能通过netbeans。 (我见过用java编写但是是.exe的程序)

off topic:also i would like to know how to make an executable-jar(exe) through netbeans if it is possible. (ive seen programs that were written in java but were an .exe)

编辑发现了一个名为FatJar的eclipse插件可以做我的事情想要,但我找不到类似的netbeans,有这样的事情吗?

EDIT discovered a plugin for eclipse called FatJar which can do what i want, but i cant find something similar for netbeans, is there such thing?

推荐答案

我将从强制性免责声明开始: Java可执行JAR不能这样工作。可执行JAR有一个在JAR的MANIFEST.MF文件中定义的主类,而MANIFEST也允许定义类路径以包含代码库在可执行的JAR中需要。 MANIFEST中的类路径定义必须枚举要放在类路径上的每个JAR或文件夹,相对路径相对于可执行JAR的位置 - 而不是包含内部可执行JAR的路径。可执行JAR使用java可执行文件的-jar参数启动,java-cp标志和CLASSPATH环境变量都忽略。至于为什么可执行JAR是以这种方式设计的,你应该知道从JAR中包含的JAR加载类的主要缺点,即使这个回复的其余部分将集中于这样做。

I'll start off with the obligatory disclaimer: Java executable JARs do not work this way. An executable JAR has a main class defined in the JAR's MANIFEST.MF file, and the MANIFEST also allows the definition of a class path to include libraries that the code in the executable JAR will need. The class path definition in the MANIFEST must enumerate every JAR or folder to put on the class path, relative paths are relative to the location of the executable JAR - not to paths contained inside the executable JAR. Executable JARs are launched with the "-jar" argument to the java executable, and both the java "-cp" flag and the CLASSPATH environment variable are ignored. As for why executable JARs were designed this way, you should be aware of the primary disadvantage of loading classes from JARs contained within JARs, even though the rest of this reply will focus on doing just that.

注意:我丢失了完全解释它的原始sun论坛主题,但实质上是因为顶级JAR中的条目可以以随机访问方式读取,但整个嵌入式JAR必须是在访问任何条目之前读取,因为顶级JAR可能已压缩其条目。

我使用了 One-Jar 过去成功,但最终产生的jar的结构可能不是你所期望的。基本上,One-Jar类是最终jar中唯一的非JARd类;所有其他代码(您的代码和任何依赖库代码)作为JAR文件包含在结果中。您的应用程序在最终JAR的main文件夹中作为名为main.jar的常规JAR文件进行JAR。您的代码所需的任何库都作为JAR文件放在最终的JAR的lib文件夹中。最后但并非最不重要的是,最终的JAR的MANIFEST.MF文件告诉One-Jar你的主类是什么。执行是一个简单的java -jar final.jar [args your app uses]。我不知道如何转换到关于你的非主题问题的OS原生EXE的下一步,但最好使用与One-Jar不同的打包机制。我不确定如何使用NetBeans,我的建议是使用构建工具来打包最终的jar。幸运的是,One-Jar提供了使用Ant生成最终jar的说明,并且应该可以很容易地集成到NetBeans中。

I have used One-Jar successfully in the past, but the structure of the final resulting jar may not be what you expect. Essentially the One-Jar classes are the only non-JARd classes in the final jar; all other code (your code and any dependent library code) is included in the resulting as JAR as JAR files. Your application is JARed as a regular JAR file named "main.jar" in the final JAR's "main" folder. Any libraries your code needs is placed, as JAR files, in the final JAR's "lib" folder. And last but not least the final JAR's MANIFEST.MF file tells One-Jar what your main class is. Execution is a dead simple "java -jar final.jar [args your app uses]". I don't know how to take the next step of converting to an OS-native EXE regarding your off-topic question, but it would probably be best to use a different packaging mechanism than One-Jar anyway. I'm not sure how to go about this with NetBeans, my advice there is to use a build tool to package the final jar. Fortunately One-Jar provides instructions on generating the final jar with Ant, and that should be easily integratable into NetBeans.

我相信Eclipse FatJar插件创建了一个One-Jar可执行文件JAR,所以如果那个插件看起来像你想做的那样,那么One-Jar就是这样做的方法。就个人而言,我使用了Maven程序集。

I believe the Eclipse FatJar plugin creates a One-Jar executable JAR, so if that plugin seems to do what you want, then One-Jar is the way to do it. Personally, I used a Maven assembly.

有一点需要注意 - 任何需要(或希望)利用Java签名JAR验证的已签名库可能无法正常工作 - 像BouncyCastle这样的Java Cryptographic Extension(JCE)实现是一个值得注意的例子。我认为原因是签名验证针对最终的JAR运行,而不是签名库。幸运的是,One-Jar允许最终用户向类路径添加额外的库,这在运行可执行JAR时明确排除;为了解决这个问题,你最好用最终的JAR和依赖操作系统的启动脚本(.bat,.sh等)交付有问题的JAR。

There is a caveat - any signed libraries that require (or desire) to take advantage of Java's signed JAR verification may not work this way - Java Cryptographic Extension (JCE) implementations like BouncyCastle are a notable example. I think the reason is that the signature verification runs against the final JAR, not the signed library. Fortunately One-Jar allows the end user to add additional libraries to the classpath, something that is explicitly precluded when running an executable JAR; to workaround this you might be better off delivering the problematic JARs with the final JAR and an OS dependent launch script (.bat, .sh, etc).

这篇关于如何将库与我的jar组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆