0

IntelliJ IDEA 2023.1.3 (Community Edition) Macbook

1. New Project > JavaFX

  • Name : demo
  • Location : ~/IdeaProjects/demo
  • Language : Java
  • Build System : Maven
  • Group : com.example
  • Artifact : demo
  • JDK : 18.0.1 (default)

2. Test build & Run

  • success, normal

3. File > Project Structure... > Artifacts > + > JAR > From modules with dependencies

  • Module : demo
  • HelloApplication (com.example.demo)
  • JAR files from libraries > Extract to the target JAR
  • Directory for META-INF/MANIFEST.MF: /Users/quenyan/IdeaProjects/demo/demo/src/main/resources
  • OK, then OK again to close the Project Structure

4. Build > Build Artifacts...

  • demo:jar > Build
  • the file output at /Users/quenyan/IdeaProjects/demo/demo/out/artifacts/demo_jar/demo.jar

5. First try directly

  • Right click the demo.jar in IDEA > Open in > Finder
  • Double click the demo.jar in finder
  • got this
Java Application launch failed.

Check Console for possible error
messages related to "/Users/quenyan/
IdeaProjects/demo/demo/out/artifacts/
demo_jar/demo.jar".

6. Second try on terminal

  • java -jar /Users/quenyan/IdeaProjects/demo/demo/out/artifacts/demo_jar/demo.jar
  • get this
Error: JavaFX runtime components are missing, and are required to run this application

What did i missed?

foopeen
  • 31
  • 5
  • 2
    Since you're using a Maven build system I'd recommend you build run the application via Maven directly. For example via `maven-shade-plugin` or `javafx-maven-plugin` plugins. Please see https://stackoverflow.com/questions/30145772/what-is-the-best-way-to-deploy-javafx-application-create-jar-and-self-contained for more details – Egor Klepikov Jul 12 '23 at 08:53
  • maven will not solve the problem. it is when starting the finished application and not when building it. the solution is to learn how the platform works. the topic has been commented on countless times. – mr mcwolf Jul 12 '23 at 10:34
  • agree with mr mcwolf, by messing up with pom.xml wont do anything. I just really dont know how to just make it work, not even gpt can do any for help. – foopeen Jul 12 '23 at 10:41
  • 3
    you need to specify where the javafx libraries are located. the platform cannot find them by itself. that's your problem. https://openjfx.io/openjfx-docs/#install-javafx – mr mcwolf Jul 12 '23 at 10:44
  • already tried that, messing up with the build and got bunches of errors. anyway, i found out a comment about upon building the jar file, some version of sdk or anything like that doesnt like some extensions, so i have to create a new class which basically start the main without and extends. ill try that tomorrow, its my home time now. – foopeen Jul 12 '23 at 11:04
  • What you are talking about is a private case. JavaFX is like any other library (jar + native library). You must specify where it is at compile time, and accordingly at runtime it must be where the platform can find it, or tell it where it is. From the link I gave you, note the last part (the rest is done by the IDE) – mr mcwolf Jul 12 '23 at 11:46
  • logically, if building and running has no problem, the exporting to jar should've been very easy to do, however it is harder than it looks, many people spends hours and days looking for this solution. – foopeen Jul 12 '23 at 12:09
  • You have a jar file right? So everything is OK. Now it remains to learn how to use these files :) To do this, you need to learn the basics of how the platform works (instead of relying on magic) – mr mcwolf Jul 12 '23 at 12:22
  • jar file is generated in out folder, but it's a broken unexecutable jar file, that means the process creating the jar is wrong – foopeen Jul 12 '23 at 12:25
  • 1
    :) That's what I'm explaining to you. It is not broken and the process is not wrong. When you run it, specify where the required components are located and your application will run. Once you understand how things work, you might want to consider optimizing the jar file build. Either by setting up the IDE (as you do it), or with a build system like maven or gradle. And then you might also consider the distribution (jpackage, jlink ...) – mr mcwolf Jul 12 '23 at 12:31
  • 1
    The JavaFX platform is composed of [modules](https://www.oracle.com/corporate/features/understanding-java-9-modules.html). The modules should be on the module path and added to the module system when you run your application. The openjfx-docs referenced by @mrmcwolf provide instructions on how to do that (see the section on runtime images). The Idea new project wizard you use will create a modular app. While you can [convert a modular app to an executable jar](https://jenkov.com/tutorials/java/modules.html#running-a-java-module-from-a-jar-with-a-main-class-set) jlink may be better. – jewelsea Jul 12 '23 at 19:57

1 Answers1

0

this answer is based on Random code's Youtube content (Windows Based)

There are 2 critical things(but not so complicated) to note here:

1. In order to avoid any error, we have to create a new Java Class that runs the main without any extends.

X public class HelloApplication extends Application X

✓ public class YourMain ✓

In Mac, select your folder that has your main java class, in my case, my HelloApplication.java is in com.example.test folder, so i select the com.example.test, then right-click > New > Java Class and name your new Java Class, in my case i named it "Main" (it will create Main.java), and this new Class will have the same level as your "com.example.yourproject" folder

it should look like this now

src
-main
--java
---com.example.test
---NewJavaClass.java
---module-info.java

for your new java class, you write it like this below

import com.example.test.HelloApplication;

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

//HelloApplication is my primary java class which run the program, which is located inside com.example.test folder
//if your new java class has different name, use "public class YourJavaName {blablabla}"

okay that's all for the first part

2. we need to add libraries/binaries/brains into the artifacts for the jar file

After done with the first one, go to File > Project Structure... > Artifacts > + icon > JAR > From modules with dependencies > Main Class (Select your new Java Class) > OK

after the list of elements loaded, look for icon + (says "Add Copy of"), then select File, search for your java sdk folder, in my case i select javafx-sdk-20.0.1 folder > then lib (Mac/Linux, it should be "bin" for Windows) then select all files inside the lib by pressing shift and click, the files should be like *.jar or *.dylib, i also included the *.properties file, for Windows it will be *.dll files.

Then Apply, and OK.

After that, we are all done, final part is building the JAR. Select Build > Build Artifacts > Build and now the JAR (which can be found inside "out" folder) is executable in everywhere(Mac/Linux, havent tried on Win), not sure about putting the bin and lib files together can make it executable really everywhere or cant do, i have no knowledge of that yet.

PS. No need to mess with maven, gradle, module, or whatever that idea are. just follow this 2 simple rules you will be fine.

foopeen
  • 31
  • 5
  • Note a separate main class that does not extend `Application` is only needed if you place JavaFX on the class-path. That configuration [is not technically supported](https://stackoverflow.com/q/67854139/6395627), though nothing breaks as far as I'm aware. JavaFX only supports being loaded from the module-path (i.e., only supports being loaded as _named modules_). Unfortunately, the JAR file specification does not support multi-modular JAR files, and by default double-clicking an executable JAR file launches it via the `-jar` option, which places everything on the class-path. – Slaw Jul 14 '23 at 01:22
  • That fact, plus the fact that JavaFX is not included in most JDKs/JREs, yet has platform-specific native code, makes deploying JavaFX via `jlink` / `jpackage` an alternative you should consider. If you decide to use those tools instead of a so-called fat/uber executable JAR file, then I recommend you use the JMOD files of JavaFX, as they will include the native code in the custom JRE cleanly (whereas the Maven artifacts embed the native code which needs to be extracted before it can be used, and the plain JAR files of the SDK from Gluon do not include the native code at all). – Slaw Jul 14 '23 at 01:26
  • it's a lot for me to process, you mentioned about named modules, yes, that. the jar file runs properly on my Mac(which installed intelliJ), i tried to run it on linux machine and got "unnamed module" error. i'm very not familiar with jlink/jpackage thing also jmod. many things to process. I'm lost. – foopeen Jul 14 '23 at 04:29
  • my personal observation is that it is a mistake to use jlink/jpackage without being clear about what you are doing. learn how to run java applications (especially modular ones). that way you won't have to package everything in one fat jar (and your app will run on any supported platform). – mr mcwolf Jul 14 '23 at 05:12
  • thank you everyone, based on https://stackoverflow.com/questions/53533486/how-to-open-javafx-jar-file-with-jdk-11 i solve this "global executable jar" problem, by using the --module-path thing. also building artifact with zero lib included, makes it really light-weight only about 3KB for helloApp.jar. This method is actually called semi-fat, previously I used the full-fat method, this one makes the jar goes up to 50mb just for hello jar, and full-fat exclusivelly work only on one system, which is bad. – foopeen Jul 14 '23 at 05:33
  • Yes, it can be a lot to process. But don't be too discouraged. I agree with @mrmcwolf in that you should take time to understand the fundamentals before taking on `jlink` / `jpackage`. Those fundamentals including how the class-path and module-path work, and how they work together (note the _Java Platform Module System_ wasn't added until Java 9 around 6 years ago). However, if you intend to distribute your JavaFX application "for real", then you'll want to consider offering end users a "standalone" application package (which `jlink` / `jpackage` can create). – Slaw Jul 14 '23 at 06:09
  • Note: You can make development much easier if you use a JDK that includes JavaFX, such as [BellSoft Liberica "Full JDK"](https://bell-sw.com/pages/downloads/) or [Azul Zulu "JDK FX"](https://www.azul.com/downloads/?package=jdk-fx). You can also require your end users to have a JRE that includes JavaFX so you don't have to ship JavaFX with your application. And note a JDK (Java Development Kit) is just a JRE (Java Runtime Environment) with development tools included (e.g., the compiler). – Slaw Jul 14 '23 at 06:13