简体   繁体   English

如何在Netbeans项目中包含外部jar

[英]How can I include external jar on my Netbeans project

When I run "clean and build" the .jar file that is being created only runs if the lib folder is at the same folder of the .jar file. 当我运行“清理并生成”了.jar正在创建的文件只有在运行lib文件夹是在同一文件夹中.jar文件。

So if I move the jar file to the desktop and leave the lib folder in the dist folder, the jar file will give me an exception. 因此,如果我将jar文件移动到桌面并将lib文件夹保留在dist文件夹中,那么jar文件将给我一个例外。

How can I deal with this problem? 我该如何处理这个问题?

I solved this by creating just one jar file with all libraries inside, adding the following to my build.xml file in NetBeans: 我通过创建一个包含所有库的jar文件解决了这个问题,将以下内容添加到NetBeans中的build.xml文件中:

<target name="-post-jar">
  <jar jarfile="dist/Combined-dist.jar">
    <zipfileset src="${dist.jar}" excludes="META-INF/*" />
    <zipfileset src="lib/commons-io-1.4.jar" excludes="META-INF/*" />
    <zipfileset src="lib/ninja-utils-3.2.jar" excludes="META-INF/*" />
    <zipfileset src="lib/unicorn-1.0.jar" excludes="META-INF/*" />
    <manifest>
        <attribute name="Main-Class" value="com.example.mypackage.Main"/>
    </manifest>
  </jar>
</target>

This creates a jar file (Combined-dist.jar) which is the combination of the dist jar and the specified library jars (in this case, commons-io-1.4.jar,ninja-utils-3.2.jar and unicorn-1.0.jar). 这将创建一个jar文件(Combined-dist.jar),它是dist jar和指定库jar的组合(在本例中为commons-io-1.4.jar,ninja-utils-3.2.jar和unicorn-1.0)。罐)。 You have to be sure to specify your Main Class package for the new jar file or it won't run when you try to open it. 您必须确保为新的jar文件指定Main Class包,否则当您尝试打开它时它将不会运行。

If you copy your jars into the source code directory, they will be in your final jar. 如果将jar复制到源代码目录中,它们将在最终的jar中。 Nevetheless, I am not sure if this will work 100% of the time. 毫无疑问,我不确定这是否会100%有效。

There is a great post at java-forum that states the following: java-forum上有一篇很棒的文章说明如下:

Except for a select few circumstances, what works best for me is to simply merge the files manually. 除少数情况外,对我来说最有效的方法是手动合并文件。 A .jar is basically a .zip with organized contents, and you can open them in almost any .zip capable archive program (I just use gnome's standard archiver, File Roller, and it works great). .jar基本上是一个带有组织内容的.zip,您可以在几乎任何.zip功能的归档程序中打开它们(我只使用gnome的标准归档程序,File Roller,它工作得很好)。 Backup your jar file and open it in the archiver of your choice, and do the same for each library jar in the library directory. 备份您的jar文件并在您选择的归档器中打开它,并对库目录中的每个库jar执行相同的操作。 Drag and drop the working folders (IE, everything EXCEPT the META-INF Directory) from each library into your jar's root path (alongside your META-INF and your app's root package). 将每个库中的工作文件夹(IE,除META-INF目录之外的所有内容)拖放到jar的根路径中(与META-INF和应用程序的根程序包一起)。 Now drag the META-INF/MANIFEST.MF file from your jar to your Desktop or any other folder. 现在将jar中的META-INF / MANIFEST.MF文件拖到桌面或任何其他文件夹中。 Open it, and erase the Class-Path and X-COMMENT lines. 打开它,擦除Class-Path和X-COMMENT行。 Don't forget to leave a blank newline at the end of the file! 不要忘记在文件末尾留下空白换行符! Save the new manifest file and drag it back to your jar's META-INF directory, overwriting the old one. 保存新的清单文件并将其拖回到jar的META-INF目录中,覆盖旧的清单文件。 Test the jar. 测试罐子。

That's really easy to package every dependent library (*.jar) into one single myProject.jar. 将每个依赖库(* .jar)打包到一个单独的myProject.jar中非常容易。

Just follow these steps and you will finally pack every dependent library into single jar. 只需按照这些步骤操作,您最终将每个依赖库打包到单个jar中。 If you are using NetBeans then you can follow exactly or else you need to find your build.xml file in project files. 如果您正在使用NetBeans,那么您可以完全遵循,否则您需要在项目文件中找到您的build.xml文件。

Follow these steps to edit build.xml 请按照以下步骤编辑build.xml

1) Click on Files tab on the left side of the project panel in NetBeans. 1)单击NetBeans中项目面板左侧的“ Files tab

2) Double click on the build.xml file and add these lines in it just before </project> line 2)双击build.xml文件,并在</project>行之前添加这些行

 <target name="package-for-store" depends="jar">
    <property name="store.jar.name" value="myProject"/>
    <property name="store.dir" value="store"/>
    <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
    <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
    <delete dir="${store.dir}"/>
    <mkdir dir="${store.dir}"/>
    <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
        <zipgroupfileset dir="dist" includes="*.jar"/>
        <zipgroupfileset dir="dist/lib" includes="*.jar"/>
        <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
        </manifest>
    </jar>
    <zip destfile="${store.jar}">
        <zipfileset src="${store.dir}/temp_final.jar"
        excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
    </zip>
    <delete file="${store.dir}/temp_final.jar"/>
</target>

3) Change value in second line of the code as per your project name which is 3)根据您的项目名称更改代码第二行的value

<property name="store.jar.name" value="myProject"/> //<---Just value not name

4) Save it and right click on build.xml and choose Run Target and then Other Targets and finally click on Package-for-store 4)保存并右键单击build.xml并选择Run Target ,然后选择Other Targets ,最后单击Package-for-store

5) And here you done. 5)在这里你完成了。 Now you can go and check just like dist folder there will be a store folder which will be containing your final complete jar including all of your dependent libraries. 现在您可以像dist文件夹一样检查是否有一个store文件夹,它将包含您的最终完整jar,包括所有依赖库。 Now whenever you want to change / add more libraries or so, just follow step 4. 现在,无论何时想要更改/添加更多库,只需按照步骤4进行操作即可。

Picture for step 4 步骤4的图片

在此输入图像描述

You could use Apache Ant since version 1.7 for build the JAR with the required libraries in only one file. 从版本1.7开始,您可以使用Apache Ant在仅一个文件中构建具有所需库的JAR。 You could have a configuration file as follows: 您可以拥有如下配置文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="buildJar">
    <target name="buildJar">
        <!-- Name of jar -->
        <jar destfile="C:/MyJar.jar" filesetmanifest="mergewithoutmain">
            <manifest>
                <!-- Your class with the main method -->
                <attribute name="Main-Class" value="myPackage.MyClass"/>
                <!-- Path in the jar -->
                <attribute name="Class-Path" value="."/>
            </manifest>
            <!-- Dir of compiled class -->
            <fileset dir="C:/NetBeansProjects/MyProject/bin"/>
            <!-- Include required jars -->
            <zipfileset excludes="META-INF/*.SF" 
                src="C:/NetBeansProjects/MyProject/lib/library1.jar"/>
            <zipfileset excludes="META-INF/*.SF" 
                src="C:/NetBeansProjects/MyProject/lib/library2.jar"/>
        </jar>
    </target>
</project>

In Netbeans, place the XML file in your project and run it with the context menu. 在Netbeans中,将XML文件放在项目中并使用上下文菜单运行它。

See more in Apache Ant User Manual . 请参阅Apache Ant用户手册中的更多内容

If you are going to distribute your app to another pc 如果您要将应用程序分发到另一台PC

You just zip .jar along with lib folder. 你只需将.jar与lib文件夹一起压缩。

If want to run your app from any place in your pc 如果想从您电脑的任何地方运行您的应用程序

Take in cosideration Maven way of doing this - create local repository eg. 考虑到实现这一点的Maven方式 - 创建本地存储库,例如。 C:\\libs where your libraries would exist and .jar could accesses them later from any place in your pc. C:\\ libs您的库将存在,而.jar可以稍后从您的电脑中的任何位置访问它们。

Or you could just use Maven. 或者你可以使用Maven。 There is a discussion on distributing application with all dependencies (libraries): Java: How do I build standalone distributions of Maven-based projects? 有关使用所有依赖项(库)分发应用程序的讨论: Java:如何构建基于Maven的项目的独立分发? .

Copy that jar file to: 将该jar文件复制到:

C:\\Program Files\\Java\\jdk\\jre\\lib\\ext C:\\ Program Files \\ Java \\ jdk \\ jre \\ lib \\ ext

and

C:\\Program Files\\Java\\jre\\lib\\ext C:\\ Program Files \\ Java \\ jre \\ lib \\ ext

You should be able to use it in Netbeans and in your manual imports, just like standard imports. 您应该能够在Netbeans和手动导入中使用它,就像标准导入一样。

I have found maybe the easiest solution for this problem here . 我已经发现也许对于这个问题最简单的解决方案在这里 You just need to copy the next code snippet at the end of the build.xml file in your project folder. 您只需要复制项目文件夹中build.xml文件末尾的下一个代码段。

<target name="-post-jar">

    <!-- Change the value to the name of the final jar without .jar -->
    <property name="store.jar.name" value="MyJarName"/>

    <!-- don't edit below this line -->
    <property name="store.dir" value="dist"/>
    <property name="temp.dir" value="temp"/>
    <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>

    <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>

    <delete dir="${temp.dir}"/>
    <mkdir dir="${temp.dir}"/>

    <jar destfile="${temp.dir}/temp_final.jar" filesetmanifest="skip">
        <zipgroupfileset dir="dist" includes="*.jar"/>
        <zipgroupfileset dir="dist/lib" includes="*.jar"/>

        <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
        </manifest>
    </jar>

    <delete dir="${store.dir}"/>

    <zip destfile="${store.jar}">
        <zipfileset src="${temp.dir}/temp_final.jar"
        excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
    </zip>

    <delete dir="${temp.dir}"/>

</target>
  1. Go to the build.xml in the root of your project and add the code right before </project> tag at the end. 转到项目根目录中的build.xml,并在最后添加</project>标记之前的代码。

  2. Now change the value of the first propertiy field as commented and save the changes. 现在将第一个属性字段的值更改为已注释并保存更改。

  3. From now on, each time you Clean & Build your project, the full jar with dependencies will be generated in the project dist folder 从现在开始,每次清理和构建项目时,将在项目dist文件夹中生成具有依赖项的完整jar

This is what worked for me: 这对我有用:

I built in excel export functionality into my project. 我在我的项目中内置了excel导出功能。 The 2 external .jars I used for this purpose was jxl.jar end sx.jar 我用于此目的的两个外部.jars是jxl.jar end sx.jar

Unpack these 2 jars into a folder(java classes) using 7-Zip without any META files. 使用7-Zip将这2个罐子解压缩到一个文件夹(java类)中,不带任何META文件。 Unpack your project jar into the same folder including the META file. 将项目jar解压缩到包含META文件的同一文件夹中。

Re-Pack the whole java classes folder using JARMaker to recreate your Project .jar in its original distribution folder ... and there you go ... full excel functionality. 使用JARMaker重新打包整个java类文件夹,在其原始分发文件夹中重新创建Project .jar ...然后你去...完全excel功能。

user1016588's solution works for me. user1016588的解决方案适合我。 There's one typo: this line should be zipfileset src="dist/lib/commons-io-1.4.jar" excludes="META-INF/*" 有一个错字:这行应该是zipfileset src =“dist / lib / commons-io-1.4.jar”excludes =“META-INF / *”

Try this - in the Netbeans IDE: 试试这个 - 在Netbeans IDE中:

  1. Go to Tools --> Libraries 转到工具 - >库
  2. In the dialog box, on the bottom left click "New Library", give a name 在对话框的左下角,单击“新建库”,输入名称
  3. On the right side, click on "Add JAR/Folder" 在右侧,单击“添加JAR /文件夹”
  4. Click OK on the bottom right 单击右下角的“确定”
  5. Re-start the IDE and check. 重新启动IDE并检查。

请遵循以下步骤: - 1.右键单击netbeans编辑器中打开的项目2.选择属性3.选择库4.添加jar 5.单击确定

You can also use this (when the libraries are not in "dist/lib"), tested with NetBeans and ant-contrib: 您也可以使用它(当库不在“dist / lib”中时),使用NetBeans和ant-contrib进行测试:

  <target name="-post-jar"> <taskdef resource="net/sf/antcontrib/antlib.xml"> <classpath> <!-- Path to ant-contrib --> <pathelement location="../../Libs/ant-contrib-1.0b3.jar"/> </classpath> </taskdef> <!-- Change the value to the name of the final jar without .jar --> <property name="store.jar.name" value="${application.title}"/> <!-- don't edit below this line --> <property name="store.dir" value="dist"/> <property name="temp.dir" value="temp"/> <property name="temp.libs.dir" value="temp/libs"/> <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/> <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/> <fileset id="store.jars.absolute" dir="${store.dir}" includes="*.jar"/> <pathconvert property="store.jars.relative" refid="store.jars.absolute" dirsep="/"> <map from="${basedir}/" to=""/> </pathconvert> <for list="${store.jars.relative}" param="item"> <sequential> <echo message="Adding @{item} into single JAR at ${store.jar}"/> </sequential> </for> <delete dir="${temp.dir}"/> <mkdir dir="${temp.dir}"/> <for list="${javac.classpath}" param="item" delimiter=":"> <sequential> <echo message="Adding @{item} into single JAR at ${store.jar}"/> <copy file="@{item}" todir="${temp.libs.dir}" overwrite="true" /> </sequential> </for> <jar destfile="${temp.dir}/temp_final.jar" filesetmanifest="skip"> <zipgroupfileset dir="${store.dir}" includes="*.jar"/> <zipgroupfileset dir="${temp.libs.dir}" includes="*.*"/> <manifest> <attribute name="Main-Class" value="${main.class}"/> </manifest> </jar> <delete dir="${store.dir}"/> <zip destfile="${store.jar}"> <zipfileset src="${temp.dir}/temp_final.jar" excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/> </zip> <delete dir="${temp.dir}"/> </target> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM