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

前端 未结 3 1348
小鲜肉
小鲜肉 2021-01-06 03:42

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 (P

相关标签:
3条回答
  • 2021-01-06 04:12

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

    0 讨论(0)
  • 2021-01-06 04:26

    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

    0 讨论(0)
  • 2021-01-06 04:32

    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.

    0 讨论(0)
提交回复
热议问题