简体   繁体   中英

JAR File Creation in JDK 11 and JavaFx

As we already know up to JDK-10 JavaFX used to be a part of JDK but with the release of JDK-11, JavaFX is to be included separately .

For doing that we need to provide VM argument for JavaFX like this:-

--module-path "C:\javafx-sdk-11.0.1\lib" --add-modules=javafx.controls,javafx.fxml

Up to this point every thing is ok but when we are finally creating a JAR file for distribution then a message is appearing like this:

VM arguments will not be part of the runnable JAR. Arguments can be passed on the command line when launching the JAR

截图

Therefore now JAR file is not able to open the application.

Now please help me by suggesting some way out so that user can open application just by clicking on JAR icon as it used to be earlier.

EDIT

After applying the solution provided by openjfx.io (section non-modular projects), I am able to generate a Standalone Jar File with JavaFX.

Now I want to add some local dependencies like pdfbox and Sqlite :

sqlite =C:\sqlite-jdbc-3.6.20.1.jar 
pdfbox=C:\pdfbox-app-2.0.10.jar

I am doing following steps:

Step-1

cd eclipse-workspace2018\test101

Step-2

set PATH_TO_FX="C:\javafx-sdk-11.0.1\lib"

Step-3

dir /s /b src\*.java > sources.txt & \
    javac --module-path %PATH_TO_FX% --add-modules=javafx.controls \
    -d out @sources.txt & del sources.txt

Step-4

cd out & jar xf "%PATH_TO_FX%\javafx.base.jar" & \
    jar xf "%PATH_TO_FX%\javafx.graphics.jar" & \
    jar xf "%PATH_TO_FX%\javafx.controls.jar" & \
    cd .. 
copy "%PATH_TO_FX%\..\bin\prism*.dll" out & \
    copy "%PATH_TO_FX%\..\bin\javafx*.dll" out & \
    copy "%PATH_TO_FX%\..\bin\glass.dll" out & \
    copy "%PATH_TO_FX%\..\bin\decora_sse.dll" out 
del out\META-INF\MANIFEST.MF & del out\module-info.class 
mkdir libs 
jar --create --file=libs/index101.jar \
    --main-class=test101.Launcher -C out . 
java -jar libs\index101.jar

Kindly modify my steps for adding above mentioned two dependencies pdfBox & sqlite.

First of all, a word of advice: Creating fat jars on command line is not advised at all. It is a manual process that should be avoided. You should try Maven or Gradle and the existing plugins for this task, in case you still need a fat jar. You should try to create a modular project, instead, and create a custom image with jlink that you can distribute for a given platform.

Second, if you still want to do a fat jar manually, you should understand the above steps, in order to be able to modify them according to your needs. Basically the main idea of a fat jar is a single project with all the *.class files (and other resources) from all the possible dependencies, not only those from your source code. The tutorial at openjfx.io just mentions the case of JavaFX dependencies.

So if you have extra dependencies, the idea is to extract the content of their jars, in the same way you extract the JavaFX jars. See jar command options like xf .

That is done in the step 3:

cd out & jar xf "%PATH_TO_FX%\javafx.base.jar" & \
    jar xf "%PATH_TO_FX%\javafx.graphics.jar" & \
    jar xf "%PATH_TO_FX%\javafx.controls.jar" & \
    cd .. 

You can modify that step with your existing jars:

cd out & jar xf "%PATH_TO_FX%\javafx.base.jar" & \
    jar xf "%PATH_TO_FX%\javafx.graphics.jar" & \
    jar xf "%PATH_TO_FX%\javafx.controls.jar" & \
    jar xf "C:\sqlite-jdbc-3.6.20.1.jar" & \
    jar xf "C:\pdfbox-app-2.0.10.jar" & \
    cd .. 

Then you can proceed with the rest of the steps in the same way.

Note that while this might work, those third party jars might have other dependencies as well (see their pom.xml for that). If that's the case, you will need to download them manually, and add them to the fat jar in the same way. Should you use Maven or Gradle, they will do it automatically for you.

As mentioned before, this is not advised at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM