Build and deploy javafx application using java11
Asked Answered
C

6

23

I followed the steps in https://blog.jetbrains.com/idea/2013/03/packaging-javafx-2-applications-in-intellij-idea-121/

but when I try to build artifacts the as in the last step I get this error

enter image description here

Error:Java FX Packager: Can't build artifact - fx:deploy is not available in this JDK

enter image description here

I know JavaFX has been removed from java11 my question is what should I do to build a .jar or .exe

here is a hello world app for quick testing.

Cule answered 23/11, 2018 at 16:19 Comment(9)
1. I doubt this to be too broad, I mean if you are already aware of the fact that JavaFX is not bundled with the JDK anymore you shall try out the standalone binaries. You could explore out the newer options like OpenJFX and find out a guide to building libraries under the JavaFX and IntelliJ section on the Documentation page. 2. That post is from 2013 and meant for 12.1 version of IntelliJ! Were you really relying on it till now?Copalm
I searched a lot there is no guide for building a jar all the guides I find stop at just running the appCule
I have read the doc you linked and it helped me run and develop the app but I can't find anywhere how to build it.Cule
2. the post still works for java8 and I hoped it will just work, and all the steps still valid except the last stepCule
Okay, let's try to narrow down the question then to solve for it, did you try to create an artifact as mentioned in the doc linked in the question? Faced any error? I would try it out as well if you have an application ready (on GitHub etc) would help expedite.Copalm
okay I will create a hello world JavaFX app using java11 and will share the linkCule
I have updated my answer @nullpointerCule
Great. Would definitely take a look at it in some time and expecting you could update the question with exact step where you're stuck in the meanwhile.Copalm
For building an executable package with integrated runtime take a look at the new jlink command: #53453712Fewell
A
14

Using the JavafX JAR export option doesn't work anymore in Intelij. You can export it as a regular jar with "Jar-From module with dependencies". This will export a valid Jar, but in order to run it, you need to add your javaFx path and modules to your command.

After you have the jar, the run command should look something like this:

java --module-path PATH_TO_YOUR_JAVAFX_LIB_FOLDER --add-modules javafx.controls,javafx.fxml,javafx.graphics,javafx.web -jar yourJar.jar

I made a youtube tutorial with this: https://youtu.be/HGHu-SzL-5E

Amphi answered 31/8, 2019 at 16:47 Comment(2)
Thanks, that was the solution I was looking forYtterbite
This works great, thanks a lot. However is there a way to make the jar run just by double clicking it directly? I used to be able to do this on Java 8.Diaphanous
C
10

The following method works for me with OpenJDK 11 and OpenJFX 11 on Windows and Ubuntu using IntelliJ and successfully creates a jar that can be run with just java -jar filename.jar without JavaFX installed on the target machine. Keep in mind that the JavaFX jars are platform dependent. So the Windows jar needs to be compiled on Windows and the Linux jar compiled on Linux.

My main method extends application...

public class Main extends Application {

So, first make a new Java class something like Start.java - this links to the original Main method.

public class Start {
    public static void main(String[] args){
        Main.main(args);
    }
}

Then you make the .jar file;

File > Project Structure > Artifacts

Click + > Choose JAR > From Modules with dependencies

For the Main class choose the Start.java > click Ok

The javafx jars should be extracted automatically (if they're not add them manually)

Click Apply and Ok

Build > Build Artifacts > Build

The file created should run fine with just java -jar filename.jar even on machines without JavaFX installed.

Colocynth answered 6/6, 2020 at 0:25 Comment(2)
To double click and run... make a shortcut. In the target box... C:\Users\me\Desktop\java -jar "C:\Program Files\MyApp\myFX.jar"Colocynth
For run on double click on Linux chmod +x myFX.jarColocynth
P
2

Unfortunately, you won't be able to build your jar using JFX11 this way, as apparently the packager was removed from the JFX SDK. There is hope it will be implemented in a future release (maybe 12). Read here for more details:

https://youtrack.jetbrains.com/issue/IDEA-200721 containing the following 2 links:

https://bugs.openjdk.java.net/browse/JDK-8212780

https://openjdk.java.net/jeps/343

As a temporary solution, you might simply use/downgrade to version 10 which still includes the needed packager.

Plebs answered 24/11, 2018 at 23:53 Comment(2)
oracle no longer host java 10 on their website.Cule
for any one looking for java10 you can find it here oracle.com/technetwork/java/javase/downloads/…Cule
N
1

As Barosanu240 answered, IntelliJ cannot currently export JavaFX Jars anymore, although development on that aspect is being carried out. Further, his solution on his YouTube video works perfectly well, so thank you for posting that.

In order to have a JAR file open without use of the command line, the application essentially needs to restart itself with the additional JVM argumemts listed above. Although messy, this can be accomplished in the following way.

Suppose the application looks for a temporary file in its current directory, named exist.txt. If this file does not exist, it creates it, sends a command to CMD to start another instance of itself with the additional JVM arguments, then closes. The newly opened instance then checks for the existence of exist.txt, and it now does exist; therefore, it knows that the UI is open and visible. It then deletes exist.txt, and the application then continues on as normal.

This methodology can be accomplished with a try/catch block, although admittedly messy, as follows.

try (FileReader fileRead = new FileReader(new File(Paths.get("").toAbsolutePath().toString() + FileSystems.getDefault().getSeparator() +"exist.txt"))) {
        //If the code gets this far, the file exists. Therefore, the UI is open and visible. The file can now be deleted, for the next time the application starts.

        fileReader.close();
        File file = new File(Paths.get("").toAbsolutePath().toString() + FileSystems.getDefault().getSeparator() +"exist.txt");
        boolean result = file.delete();
        if (result) {
            System.out.println("File deleted successfully; starting update service.");
        } else {
            System.out.println("File was not deleted successfully - please delete exist.txt.");
        }

    } catch (IOException e) {
        //The file does not exist because an exception was generated. Therefore, create the file, and restart the application using CMD with the additional arguments required
        File file = new File(Paths.get("").toAbsolutePath().toString() + FileSystems.getDefault().getSeparator() +"exist.txt");
        try {
            boolean result = file.createNewFile();
            if (result) {
                //Start a new instance of this application
                final String command = "cmd /c start cmd.exe /k java --module-path PATH_TO_YOUR_JAVAFX_LIB_FOLDER --add-modules javafx.controls,javafx.fxml,javafx.graphics,javafx.web -jar yourJar.jar;
                Runtime.getRuntime().exec(command);
            } else {
                System.out.println("Unable to create exist.txt. Application will close.");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        //Now that a new file has been created and a command sent to CMD, exit this instance
        System.exit(-1);
    }

//The rest of your application's code

Note that the path to your JavaFX lib folder should not contain any spaces. The above code needs to be in your application's main method, before the launch(args) line. There are probably better ways to achieve this same behaviour without use of a try/catch (as invoking exceptions purposely is not good practice), but this solution works fine for the time being.

Nigro answered 23/4, 2020 at 11:25 Comment(0)
K
0

i have the same problem , after a lot of searching i found the solution on youtube with this video , the link for the video : https://www.youtube.com/watch?v=F8ahBtXkQzU

you have to add all (.dill) files from your JavaSDKFX File on your Computer To the libs that you are going to Set in the Artifact , check the link for more obvious Info .

Kilkenny answered 6/5, 2021 at 5:51 Comment(2)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Exigible
I got you , and will be more obvious in my future answers , thanks .Kilkenny
C
0

Artifacts -> + -> javaFx Application -> from modules then set Application class

Coelho answered 5/6, 2021 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.