Edit: Updated my question to better reflect what my problem was. Original Question: Is there a way to prevent Jar files exported in VScode from compiling using --enable-preview?
I'm working on a java project in VScode that uses JavaFX. Whenever I export a jar of the project using the Export Jar button and try to run the jar in cmd, I keep getting this error:
Error: LinkageError occurred while loading main class MyProgram
java.lang.UnsupportedClassVersionError: Preview features are not enabled
for MyProgram (class file version 61.65535). Try running with '--enable-preview'
Whenever I try to use the --enable-preview tag I get the following:
Error: JavaFX runtime components are missing, and are required to run this application
This is my JDK I use and I have JavaFX version 17.0.1.
openjdk version "17.0.1" 2021-10-19 LTS
OpenJDK Runtime Environment Microsoft-28056 (build 17.0.1 12-LTS)
OpenJDK 64-Bit Server VM Microsoft-28056 (build 17.0.1 12-LTS, mixed mode, sharing)
Other solutions to similar problems seem to be issues with having multiple JDKs but I've only have this JDK downloaded on my device and I've only ever had this version downloaded. JavaFX is properly configured to my project and my JAVA_HOME path is to the JDK folder & Path's path is to the JDK bin.
The program runs fine when I run it in VScode, its when I export the jar and try to run it in cmd or terminal that this error pops up.
I tested it with a basic print hello world jar (not using JavaFX) and I get the same error too but no error appears when I run with the --enable-preview tag.
CodePudding user response:
Thanks to help in the comments & this blog, adding the following to your VM arguments:
--module-path path/to/your-version-of-javafx/lib" --add-modules javafx.controls,javafx.fxml
and running the following command in cmd/terminal
java --enable-preview --module-path path/to/your-version-of-javafx/lib" --add-modules javafx.controls,javafx.fxml -jar MyProgram.jar
Allows the jar run without errors.
The blog also has other solutions/methods to dealing with this problem as well.