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

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

问题描述

好的,所以我编写了一个使用 3rd 方开源库的程序,我想将它与我的程序一起打包在一个 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;

题外:如果可能的话,我也想知道如何通过netbeans制作一个可执行的jar(exe).(我见过用 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)

EDIT 发现了一个名为 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 文件.您的应用程序在最终 JAR 的main"文件夹中作为名为main.jar"的常规 JAR 文件被 JAR.您的代码需要的任何库都作为 JAR 文件放置在最终 JAR 的lib"文件夹中.最后但并非最不重要的最后一个 JAR 的 MANIFEST.MF 文件告诉 One-Jar 你的主类是什么.执行是一个非常简单的java -jar final.jar [args your app uses]".我不知道如何就您的题外问题采取下一步转换为操作系统原生 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 加密扩展 (JCE) 实现就是一个值得注意的例子.我认为原因是签名验证针对最终的 JAR,而不是签名的库.幸运的是,One-Jar 允许最终用户向类路径添加额外的库,这是在运行可执行 JAR 时明确排除的;要解决此问题,您最好将有问题的 JAR 与最终 JAR 和依赖于操作系统的启动脚本(.bat、.sh 等)一起交付.

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天全站免登陆