Java: how to create a working .exe from a native installer?

给你一囗甜甜゛ 提交于 2019-11-30 22:13:43

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.

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

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").

Same problem here, using these:

  • Java SE Version 8 Update 31 (build 1.8.0_31-b13)
  • NetBeans 8.0.2 (Patch 1)
  • Windows 7 Enterprise SP1 on Intel Core i7 (64-bit)
  • Inno Setup Compiler 5.5.5 (u) - Unicode

I also traced output by redirecting printlns, and it blew up the first time it called for an external library. I thought maybe I had the same problem as matt1 so I fixed all the relative paths, but no joy. I finally figured out that the installer was not creating the /app/lib folder with the external jar files. After manually copying the lib folder to the target installation folder it worked fine, even on a machine with no JRE installed (I'm working on a self-contained app).

The fix was to add a line to the build.xml:

   <target name="-post-jfx-deploy">
   <fx:deploy width="${javafx.run.width}" height="${javafx.run.height}" 
             nativeBundles="all"
             outdir="${basedir}/${dist.dir}" outfile="${application.title}">
      <fx:application name="${application.title}" mainClass="${javafx.main.class}"/>
      <fx:resources>
          <fx:fileset dir="${basedir}/${dist.dir}" includes="*.jar"/>
          <!--below is the magic bit that copies all the dependency jar files to the native package output-->
          <fx:fileset dir="${basedir}/${dist.dir}" includes="lib/*.jar"/>
      </fx:resources>
      <fx:info title="MyApp" vendor="MyVendor"/>
  </fx:deploy>          
</target>

The second fileset directive forces a copy of the jar dependencies from the lib folder, which fixed everything when deploying either as an EXE or an MSI.

I thinks your main class is not correctly set to package. Instead of using Netbeans you can make exe from your jar file using exe4J.(please make sure main class is set from project > properties > run > main class

download exe4J from here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!