2

I have developed a JavaFX application under NetBeans, which needs JRE8u60 and some external libraries (jar and dll) and a "SQLite" database file to run. I'm looking for a way to create an executable file, or an installer, which includes (or install) this version of the JRE and the needed libraries.

First I tried with the NetBeans native packager, as showed in https://netbeans.org/kb/docs/java/native_pkg.html#fx , but when I run the installer obtained, the executable file says it can't find the main class. I just followed that tutorial, so I don't know if I missed some point necessary to fit into my particular case. My build.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Petra" default="default" basedir="." xmlns:fx="javafx:com.sun.javafx.tools.ant">
<description>Builds, tests, and runs the project Petra.</description>
<import file="nbproject/build-impl.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="Petra.jar"/>
      </fx:resources>
      <fx:info title="${application.title}" vendor="${application.vendor}"/>
      <fx:platform basedir="C:\Program Files\Java\jdk1.8.0_60"/> 
  </fx:deploy>  
</target>
</project>

Nothing seems to be wrong in the output when compile:

ant -f C:\\Users\\jros\\Documents\\NetBeansProjects\\Petra jfx-rebuild
init:
deps-clean:
Updating property file: C:\Users\jros\Documents\NetBeansProjects\Petra\build\built-clean.properties
Deleting directory C:\Users\jros\Documents\NetBeansProjects\Petra\build
clean:
init:
deps-jar:
Created dir: C:\Users\jros\Documents\NetBeansProjects\Petra\build
Updating property file: C:\Users\jros\Documents\NetBeansProjects\Petra\build\built-jar.properties
Created dir: C:\Users\jros\Documents\NetBeansProjects\Petra\build\classes
Created dir: C:\Users\jros\Documents\NetBeansProjects\Petra\build\empty
Created dir: C:\Users\jros\Documents\NetBeansProjects\Petra\build\generated-sources\ap-source-output
Compiling 19 source files to C:\Users\jros\Documents\NetBeansProjects\Petra\build\classes
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 26 files to C:\Users\jros\Documents\NetBeansProjects\Petra\build\classes
compile:
Created dir: C:\Users\jros\Documents\NetBeansProjects\Petra\dist
Copying 3 files to C:\Users\jros\Documents\NetBeansProjects\Petra\dist\lib
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_60\jre\..\lib\ant-javafx.jar
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_60\jre\..\lib\ant-javafx.jar
No base JDK. Package will use system JRE.
RelativeFileSet{basedir:C:\Users\jros\Documents\NetBeansProjects\Petra\dist, files:[Petra.jar, lib\RXTXcomm.jar, lib\ojdbc6.jar, lib\sqlitejdbc-0.5.4.jar]}
No base JDK. Package will use system JRE.
Using base JDK at: C:\Program Files\Java\jdk1.8.0_60\jre
RelativeFileSet{basedir:C:\Users\jros\Documents\NetBeansProjects\Petra\dist, files:[Petra.jar]}
Using base JDK at: C:\Program Files\Java\jdk1.8.0_60\jre
Creating app bundle: Petra in C:\Users\jros\Documents\NetBeansProjects\Petra\dist\bundles
Result application bundle: C:\Users\jros\Documents\NetBeansProjects\Petra\dist\bundles
Installer (.exe) saved to: C:\Users\jros\Documents\NetBeansProjects\Petra\dist\bundles
RelativeFileSet{basedir:C:\Users\jros\Documents\NetBeansProjects\Petra\dist, files:[Petra.jar]}
jfx-deployment-script:
jfx-deployment:
jar:
jfx-rebuild:
BUILD SUCCESSFUL (total time: 3 minutes 31 seconds)

But when I run the installer, the "exe" generated throws this two windows:

enter image description here

enter image description here

Any clues on this??

Juan
  • 1,949
  • 1
  • 15
  • 22
  • have you considered to use gradle as your build tool? there exists a gradle plugin for javafx. Take a look [here](http://stackoverflow.com/questions/21721119/creating-runnable-jar-with-gradle), [here](https://docs.gradle.org/current/userguide/userguide.html) and [here](https://bitbucket.org/shemnon/javafx-gradle) – DonMushroom Jun 24 '15 at 07:45
  • 1
    or use maven java fx plugin , it has some bugs but will help a lot for build / exporting to jar – AntJavaDev Jun 25 '15 at 07:22

1 Answers1

1

I haven't found a way to put the JRE directly into the executable, but here's a way to at least put all the libraries inside your Jar (using Netbeans) to reduce the clutter. You may have seen it elsewhere on StackOverflow before.

  1. Clean and Build your program.
  2. Open Files < build.xml

enter image description here

  1. Add this code to build.xml:

    <target name="package-for-store" depends="jar">
    
        <property name="store.jar.name" value="MyJarName"/>
    
        <!-- don't edit below this line -->
    
        <property name="store.dir" value="store"/>
        <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
    
        <echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
    
        <delete dir="${store.dir}"/>
        <mkdir dir="${store.dir}"/>
    
        <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
            <zipgroupfileset dir="dist" includes="*.jar"/>
            <zipgroupfileset dir="dist/lib" includes="*.jar"/>
    
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
            </manifest>
        </jar>
    
        <zip destfile="${store.jar}">
            <zipfileset src="${store.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
        </zip>
    
        <delete file="${store.dir}/temp_final.jar"/>
    
    </target>
    

Just change MyJarName to whatever you want the Jar file to be called (Leave off the .jar extension).

  1. Right click build.xml and click Run Target < Other Targets < package-for-store.

enter image description here

  1. You're all set. The complete jar will be in a folder called store in your main project folder. You can then package it up as an .exe in Launch4J.
corpico
  • 617
  • 3
  • 16
  • 26
  • Thanks for your approach, but the generated jar doesn't even start :s Probably I had to add the dll library, isn't it?? I'm new to Ant – Juan Jun 24 '15 at 14:35
  • Can you post an screenshot of your build.xml with the package-for-store code in it? – corpico Jun 24 '15 at 14:39
  • The above code only copies into the final jar the contents of the "lib" folder. If you have any other dependencies, you'll have to add them too with something like `` – corpico Jun 24 '15 at 14:44
  • It seems that this approach doesn't work with dll's libraries :( Thanks anyway, it might help in next projects ;) – Juan Jun 24 '15 at 15:38
  • This wont work for java fx since java fx has its own ant tool . – I.Tyger Feb 11 '19 at 12:17