Java:如何从本机安装程序创建工作.exe? [英] Java: how to create a working .exe from a native installer?

查看:92
本文介绍了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):右键单击Libraries> Add jar / folder

  • In main方法,实例化引用该库的对象。

  • 右键单击项目名称>属性>部署>在项目菜单中启用本机打包操作

  • 右键单击您的项目> Package as> EXE installer

  • 构建后,转到创建安装程序的目录,然后启动安装程序。

  • 安装结束时,转到安装目录,然后启动.exe。应该发生错误。

  • Create a new Java project in Netbeans
  • Link any external library (.jar): right click on Libraries > Add jar/folder
  • In the main method, instantiate an object referencing that library.
  • Right click on the project's name > Properties > Deployment > Enable Native Packaging actions in project menu
  • Right click on your project > Package as > EXE installer
  • After build, go to the directory where the installer has been created, and launch the installer.
  • At the end of the installation, go to the installation directory, and start the .exe. The error should occur.

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

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)看看堆栈跟踪是什么,例如如果你的Main类在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

对我来说,我用来加载各种文件的URL(例如JavaFX的FXML文件)打包在jar导致了问题 - 我使用的是相对URL(例如./blah.ext或../foo.txt等),但是一旦我根据文件的布局方式将URL更改为绝对URL它在jar中工作正常(例如/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天全站免登陆