Java 8 .jar 文件不会使用 Netbeans 8.0.2 从桌面运行以使用外部库进行编译;如果库包含在源代码中,则运行良好

Posted

技术标签:

【中文标题】Java 8 .jar 文件不会使用 Netbeans 8.0.2 从桌面运行以使用外部库进行编译;如果库包含在源代码中,则运行良好【英文标题】:Java 8 .jar file won't run from desktop using Netbeans 8.0.2 to compile WITH EXTERNAL LIBRARY; runs fine if library is included in source 【发布时间】:2015-08-09 23:56:32 【问题描述】:

编辑

jar 文件现在可以从桌面运行,在清理并构建并将其复制到那里 但是 以实现它我必须 删除包含Utilities.jar 的外部库,并将源包含为另一个java 类。

这是不可接受的,但也许它会告诉我们如何解决下面的问题。

当使用包含Utilities.jar 的外部库进行编译时,我在dist 文件夹内找到了一个lib 文件夹,其中是Utilities.jar

dist 文件夹中的Readme.txt 文件显示:

        ======================== BUILD OUTPUT DESCRIPTION =====

        When you build an Java application project that has a main class, the IDE
     automatically copies all of the JAR files on the projects classpath to your
     projects dist/lib folder. The IDE also adds each of the JAR files to the 
    Class-Path element in the application JAR files manifest file (MANIFEST.MF).

        To run the project from the command line, go to the dist folder and 
    type the following:

        java -jar "MrCopy.jar" 

        To distribute this project, zip up the dist folder (including the 
    lib folder) and distribute the ZIP file.

        Notes:

        * If a library on the projects classpath also has a Class-Path element 
        specified in the manifest, the content 
        of the Class-Path element has to be on the projects runtime path. 

这是manifest.mf 中的内容:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

我应该添加什么吗?

如果我按照readme中的建议“压缩.jar和lib文件夹”并将.zip文件夹放在桌面上并解压缩并运行.jar文件,程序运行正常,因为它应该。

如果我通过javac -jar mrcopy.jardist 文件夹的命令行运行,它当然运行正确。

但我从来不必为了获得一个可执行的 .jar 文件而经历这一切。

我很困惑........

就这么简单吗: 如果我有外部库,我将不得不压缩它们、分发 zip、解压缩 zip 并从解压缩到的任何位置运行 .jar 文件?

编辑结束

多年来,在修改和编译MrCopy.jar 之后,我可以将.jar 文件(位于...\NetBeansProjects\MrCopy\dist 文件夹中)复制到我的桌面并运行它。以下是应该发生的事情:

弹出该对话框,单击“是”会导致复制,最后会出现以下屏幕:

但现在我(截至今天)在项目属性Source\Binary format 中使用JAVA 8 进行编译,我没有得到第一个对话框,也没有任何反应。

项目属性打包如下所示:

如何使 .jar 文件从桌面运行?

【问题讨论】:

【参考方案1】:

如果我的理解是正确的,你有一个依赖于其他库的项目?

如果是这样,您可以做两件事来分发您的应用:

1 - 创建一个胖 Jar 你可以看到它here

2 - 就像你说的那样压缩 project/dist 文件夹的内容

【讨论】:

谢谢,@liponcio。我从来没有听说过“胖罐子”。您是从哪里得知这个程序的? 这最近有问题。见this?

以上是关于Java 8 .jar 文件不会使用 Netbeans 8.0.2 从桌面运行以使用外部库进行编译;如果库包含在源代码中,则运行良好的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 pacifist 在带有 Java 1.7.0.45 的 Mac OS X 10.6.8 上运行线程化的 java jar 文件

属性文件中的更改不会保存在 JAR 文件中

打jar包

myeclipse中如何导入mysql-connector-java-5.1.8-bin.jar环境配置和工具使用

我有一个外部可执行 jar 文件,我希望使用 Eclipse 编辑其中的 java 文件

使用在C ++中导入外部jar文件的java类