Java NetBeans - 在一个 jar 中部署所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2212913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:47:57  来源:igfitidea点击:

NetBeans - deploying all in one jar

javanetbeansjar

提问by Tim

Possible Duplicate:
Put external library to the JAR?

可能的重复:
将外部库放入 JAR?

I have NetBeans 6.8 and I wrote one class which has two libraries (jar-files). Building it, I get a "dist" folder with my project.jar and a "lib" folder which contains the two lib jar files.

我有 NetBeans 6.8,我写了一个类,它有两个库(jar 文件)。构建它,我得到一个包含 project.jar 的“dist”文件夹和一个包含两个 lib jar 文件的“lib”文件夹。

How could I get all this in one jar file? (I do not use Maven/Ant or something like this.)

我怎么能在一个 jar 文件中得到所有这些?(我不使用 Maven/Ant 或类似的东西。)

采纳答案by Thorbj?rn Ravn Andersen

The basic problem is that the current version of Java does not support jars inside jars out of the box.

基本问题是当前版本的 Java 不支持开箱即用的 jars inside jars。

The recommended solution is to use the Class-Path line in the MANIFEST.MF file inside your jar to point to required libraries (relative paths are allowed) and then deploy all files together and invoking it with "java -jar your.jar"

推荐的解决方案是使用 jar 内 MANIFEST.MF 文件中的 Class-Path 行指向所需的库(允许使用相对路径),然后将所有文件部署在一起并使用“java -jar your.jar”调用它

If you really want to have a "jar-inside-jar" solution, we have used one-jar for several years, but gone away from it since our target JVM worked better with the solution described above.

如果您真的想要一个“jar-inside-jar”解决方案,我们已经使用 one-jar 好几年了,但由于我们的目标 JVM 与上述解决方案配合得更好,所以不再使用它。

http://one-jar.sourceforge.net/

http://one-jar.sourceforge.net/

I used it with the fatjar plugin in Eclipse. I do not have any experiences with building it into Netbeans, but it is simple to build into an ant script which I believe is what NEtbeans use anyway.

我将它与 Eclipse 中的 fatjar 插件一起使用。我没有将它构建到 Netbeans 中的任何经验,但是构建一个 ant 脚本很简单,我相信无论如何 NEtbeans 都会使用它。

回答by Waverick

You can create an extra build target in the build.xml file. And use zipfilesetand zipgroupfilesetto create one big jar e.g.

您可以在 build.xml 文件中创建一个额外的构建目标。并使用zipfilesetzipgroupfileset创建一个大罐子,例如

<target name="YourBigJar" depends="-post-jar">
  <jar destfile="BigJar.jar">
    <zipfileset src="dist/Project1.jar"/>
    <zipfileset src="../OtherProject/dist/project2.jar"/>
    <zipgroupfileset dir="../libs/."/>
  </jar>
</target>

回答by Joshua Born

I agree with Waverick. The simplest way to do this with NetBeans is to add a custom target to your build.xml. (By the way, by virtue of using NetBeans, you are using Ant, since NetBeans uses Ant to build your jar file.)

我同意韦弗里克的观点。使用 NetBeans 执行此操作的最简单方法是将自定义目标添加到 build.xml。(顺便说一下,由于使用 NetBeans,您正在使用 Ant,因为 NetBeans 使用 Ant 来构建您的 jar 文件。)

Waverick's Ant target seems to be designed to merge the compiled code from a different NetBeans project into the current project's jar file. My targets below do exactly what you are looking for.

Waverick 的 Ant 目标似乎旨在将来自不同 NetBeans 项目的编译代码合并到当前项目的 jar 文件中。我下面的目标完全符合您的要求。

<target name="-unjar-and-copy-lib-jars">
    <unjar dest="${build.classes.dir}">
        <fileset dir="lib">
            <include name="**/*.jar"/>
        </fileset>
        <patternset>
            <exclude name="META-INF/**"/>
            <exclude name="/*"/>
        </patternset>
    </unjar>
</target>

<target depends="init,compile,-pre-pre-jar,-pre-jar,-unjar-and-copy-lib-jars" name="fat-jar">
    <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
    <jar destfile="${dist.jar}">
        <fileset dir="${build.classes.dir}"/>
        <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
        </manifest>
    </jar>
    <echo>To run this application from the command line without Ant, try:</echo>
    <property location="${dist.jar}" name="dist.jar.resolved"/>
    <echo>java -jar "${dist.jar.resolved}"</echo>
</target>

<target depends="clean,fat-jar" name="clean-and-fat-jar"/>

回答by gobernador

You can use Java to install the external libraries programmatically using Class.getResourceAsStream()and a FileOutputStream. Libraries will usually go in Java\lib\ext\.

您可以使用 Java 以编程方式安装外部库,Class.getResourceAsStream()并使用FileOutputStream. 图书馆通常会进去Java\lib\ext\

Here's how I approached it:

这是我如何处理它:

I placed a copy of all the JAR's I used into a .ressubpackage. From there, I can copy them anywhere.

我将我使用的所有 JAR 的副本放入一个.res子包中。从那里,我可以在任何地方复制它们。

private void installLibraries() {
    new Thread() {

        @Override
        public void run() {
            System.out.println("Checking for libraries");

            File jre = new File(System.getProperty("java.home"));
            File jar = new File(jre, "lib/ext/JAR_NAME.jar");
            //Create more File objects that wrap your JAR's
            try {
                boolean added = false;

                if (!jar.exists()) {
                    copyResource(jar, "JAR_NAME.jar");
                    added = true;
                }
                //Repeat for more JAR's

                if (added) {
                    System.out.println("Libraries installed.");
                } else {
                    System.out.println("Library check complete");

            } catch (IOException ex) {
                System.out.println("Library installation failed.");
                ex.printStackTrace(System.out);
            }
        }

        private void copyResource(File dest, String src) throws IOException {
            System.out.println("Copying resource " + src + " to " + dest);
            InputStream in = THIS_CLASS_NAME.class.getResourceAsStream("/YOUR_PACKAGE/res/" + src);
            dest.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int i = 0;
            while ((i = in.read(buffer)) > 0) {
                out.write(buffer, 0, i);
            }
            out.close();
            in.close();
        }
    }.start();
}

Run this method before you do anything else, and you can completely ignore the external JAR files that Netbeans gives you, and just distribute the one. As an additional note, I used this method to install javax.comm, which didn't like being distributed externally. It came with a .dll file and a properties file. These files can be installed using the exact same method, but it's worth noting that the .dll file must be placed in the Java\lib\directory and properties files go in the Java\lib\directory (not in the \extfolder).

在执行任何其他操作之前运行此方法,您可以完全忽略 Netbeans 提供给您的外部 JAR 文件,而只需分发该文件即可。作为附加说明,我使用这种方法来安装javax.comm,它不喜欢在外部分发。它带有一个 .dll 文件和一个属性文件。这些文件可以使用完全相同的方法安装,但值得注意的是 .dll 文件必须放在Java\lib\目录中,属性文件放在Java\lib\目录中(而不是在\ext文件夹中)。

回答by chAmi

I've found a good article here. But seems it's worth to mention that it is ask for external libraries in dest/lib folder. You can make it that way but normally we have lib folder in the root folder(Or what i have). If it is just change

我在这里找到了一篇好文章。但似乎值得一提的是,它要求 dest/lib 文件夹中的外部库。你可以这样做,但通常我们在根文件夹中有 lib 文件夹(或者我有的)。如果只是改变

<zipgroupfileset dir="dist/lib" includes="*.jar"/>to

<zipgroupfileset dir="dist/lib" includes="*.jar"/>

<zipgroupfileset dir="/lib" includes="*.jar"/>. 

That's all.

就这样。