Intellij Javafx artifact - how do you make it?

2020-07-10 03:30发布

问题:

I've been trying all day to turn my javafx application into a jar file. I'm using Java 1.7 update 7.

Oracle has some information, but it just seems scattered all over the place. Intellij is almost doing the job, but I get the following error:

java.lang.NoClassDefFoundError: javafx/application/Application

Which seems to say that I need to tell java where the jfxrt.jar is... If I add this jar to my classpath info for the manifest build in intellij - ctrl+shift+alt+s -> Artifacts -> Output Layout tab -> Class Path, then I get another error:

 Could not find or load main class com.downloadpinterest.code.Main

It seems strange to have to include jfxrt.jar in my class path though...

I've tried to create an ant script as well, but I feel like IntelliJ is 90% of the way there - and I just need a bit of help to figure out why I need to include jfxrt.jar, and why my Main class isn't being found (I'm guessing I need to add it to the classpath somehow?).

Could anyone clue me up as to what is happening? I had a basic gui before which worked fine, but JavaFX seems to be making life complicated!

回答1:

Your assumption is correct, you need to add JavaFX to the classpath.
It is located in [JDK]/jre/lib/jfxrt.jar.

To provide JavaFX at runtime, you can bundle it with your application, illustrated in this Oracle guide or make Java 7 Update 6 a requirement for your app, then add [JAVA_HOME]/lib/jfxrt.jar to the classpath at the application's earliest convenience.

The first option means more work for you, but makes absolutely sure that all users have the same version of the library - and you can bundle the JRE as well, while you are at it. The second version doesn't offer this certainty, but is easier to realize.

As a variant to the second option - if bundling the JRE is actually possible for you - , you can also just bundle it in your distribution and then include a shell-scripts/batch-file/executable-wrapper to make your program use it. That way, you get all the certainty from option I with only a moderate boost in difficulty from option II.



回答2:

I had the same problem. But I found this blog post: Packaging JavaFX 2 Applications in IntelliJ IDEA 12.1 that shows how to do it in Intellij 12.1

Apparently IntelliJ has the capability to produce an special "JavaFX Application" artifact. It automatically bundles everything it needs within your jar-file.