Java:如何从本机安装程序创建一个有效的 .exe? [英] Java: how to create a working .exe from a native installer?

查看:28
本文介绍了Java:如何从本机安装程序创建一个有效的 .exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,我希望任何用户都能轻松安装,我认为创建本机安装程序是可行的方法.我使用了 Netbeans 8.0 的功能(项目属性 > 启用本机打包和打包为).我能够创建一个有效的本机安装程序.安装后,我有以下文件树:

I have a program I want to be able to be installed easily by any user, and I thought creating a native installer was the way to go. I used Netbeans 8.0's functionalities (Project properties > Enable native packaging, and Package as). I was able to create a working native installer. After installation, I have the following file tree:

+-- Project
    +-- app
         +--lib (containing all the jars used by the project)
         ---Project.jar
         ---package.cfg
    +--runtime
        +--jre (contains the current jre)
    ---Project.exe

从 Project.jar 执行程序有效.但是,当我尝试启动 Project.exe 时,我在一个带有无详细信息的窗口弹出窗口中收到以下错误消息:

Executing the program from Project.jar works. However, when I try to launch Project.exe, I get the following error message on a window pop-up with no details whatsoever:

由于主类异常而失败

为了确定问题的根源,我使用了基本的标准输出并将它们重定向到一个文件中,这要归功于 Windows 命令提示符.我得到了输出,直到第一次调用外部库.所以看起来库链接没有完成.但是,这是我的 package.cfg 文件的内容:

To identify the source of the problem, I have used basic standard outputs and redirected them into a file thanks to the Windows command prompt. I got outputs until the first call to an external library. So it seems the library linking is not done. However, here's the content of my package.cfg file:

app.mainjar=Project.jar
app.mainclass=project/Main
app.classpath=lib/firstlibrary.jar lib/secondlibrary.jar 

类路径已指定,但似乎无法识别.我曾尝试反转斜杠 () 并更改 ; 的空格,但无济于事.

The classpath is specified but does not seem to be recognized. I have tried inverting the slash () and changing whitespaces for , or ; to no avail.

有一种简单的方法可以重现该错误(Netbeans 8.0 和 JDK 1.8u11):

There is an easy way to reproduce the bug (Netbeans 8.0 and JDK 1.8u11):

  • 在 Netbeans 中创建一个新的 Java 项目
  • 链接任何外部库 (.jar):右键单击库 > 添加 jar/文件夹
  • 在 main 方法中,实例化一个引用该库的对象.
  • 右键单击项目名称 > 属性 > 部署 > 在项目菜单中启用本机打包操作
  • 右键单击您的项目 > 打包为 > EXE 安装程序
  • 构建完成后,转到创建安装程序的目录,然后启动安装程序.
  • 安装结束后,进入安装目录,启动.exe.应该会发生错误.

知道如何解决这个问题吗?

Any idea how to fix this?

推荐答案

我刚刚在本地打包的 JavaFX 应用程序中遇到了类似的问题,我收到了由于主类异常而失败"错误.我的 package.cfg 看起来和你的一样.

I just had a similar issue with a native-packaged JavaFX application where I was getting the "Failed due to exception from main class" error. My package.cfg looked the same as yours.

我所做的诊断只是从命令行手动运行 jar 文件(例如 Project.jar)并查看堆栈跟踪是什么,例如如果你的主类在 org.project.Project

What I did to diagnose it was just to manually run the jar file (e.g. Project.jar) from the command line and see what the stacktrace was, e.g. if your Main class was in org.project.Project

java -cp Project.jar org.project.Project

对我来说,我用来加载打包在 jar 中的各种文件(例如 JavaFX 的 FXML 文件)的 URL 导致了问题 - 我使用的是相对 URL(例如./blah.ext"或"../foo.txt"等),但是一旦我根据文件在 jar 中的布局方式将 URL 更改为绝对的,它就可以正常工作(例如/org/project/blah.ext"和/org/foo.txt").

Turns out for me that the URLs I had been using to load various files (e.g. the FXML files for JavaFX) packaged in the jar were causing issues - I was using relative URLs (e.g. "./blah.ext" or "../foo.txt" etc), but once I had changed the URLs to be absolute based on how the files were laid out in the jar it worked fine (e.g. "/org/project/blah.ext" and "/org/foo.txt").

这篇关于Java:如何从本机安装程序创建一个有效的 .exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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