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.