0

My system is a Windows 10

I'm using java jdk 19 with no build tools

I'm trying to run a .jar file generated from my code using vscode "export jar" option, but double clicking the resulting file doesn't do anything.

Trying to run the file from the terminal returns this error

Error: LinkageError occurred while loading main class Aula3 java.lang.UnsupportedClassVersionError: Preview features are not enabled for Aula3 (class file version 63.65535). Try running with '--enable-preview'

I've looked all over the internet but no one seems to have an answer, I did find another post here about the same error, but the solution only says

SO basically I had to create a .bat file with java --enable-preview -jar .jar

this resolved my issue.

I don't know exactly what this means or how should I even begin to try it, and having made my account today I don't have enough reputation to ask him with a comment, so I was hoping someone else could shed a light on this for me.

Any help is appreciated as I've been trying to fix this for days now.

Also I've checked my path variables, they're set propperly, and tried updating java, both the jdk and jre (which left me with versions 19 and 1.8 respectively)

  • That error means your code, or code you depend on, was compiled with `--enable-preview`. As [preview features](https://openjdk.org/jeps/12) are subject to change and exist for a specific major version of Java, they are opt-in at both compile-time and run-time. Thus, you must run your JAR file with the `--enable-preview` option if you want it to work. There's really no way around that (other than removing any reliance on preview features from your code and/or removing any dependencies that use preview features). Note Java 19 has [multiple preview features](https://openjdk.org/projects/jdk/19/). – Slaw Mar 04 '23 at 22:42
  • Also, note that, according to the error, you need Java 19 at run-time (the class file version is `16.XX`, which [corresponds to Java 19](https://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers)). Code compiled with newer versions of Java cannot be executed by older versions of Java. The JDK includes a JRE, but if you're looking for a JRE-only package, then I recommend looking elsewhere than Oracle (e.g., [Adoptium](https://adoptium.net/temurin/releases/)). – Slaw Mar 04 '23 at 22:46
  • That all said, is your question more about what a `*.bat` file is and how to use one to solve your problem? – Slaw Mar 04 '23 at 22:48
  • I really just want to fix my problem and I was thinking of how I could use a .bat file to do so, if it works, so I'd say it's the second option – João Vitor da Silva Mar 06 '23 at 01:00
  • also about the JDK, I heard it has a JRE built in, so I tried installing my java 8 JRE which I also had, and I still got the same problem, and other java relian applications/games won't run now. So it seems I'm compiling and running with the same version, but I don't know how to or if I even can make the other applications use the java 19 jdk instead of the other jre I had – João Vitor da Silva Mar 06 '23 at 01:03
  • about the preview features, how can I opt-out from them at run time, and how could I check which features rely on it? – João Vitor da Silva Mar 06 '23 at 01:05
  • As I mentioned, preview features are opt-in. You "opt-out" of them by not opting in. And code compiled using preview features cannot be executed without enabling preview features. That's just how preview features work. Note the error talks about `Aula3` specifically. Given the lack of a package name, I can only assume this code was not written by any "official" entity. In other words, I assume this code was written by you. What happens when you try to _compile_ **without** `--enable-preview`. Do you get any errors? – Slaw Mar 06 '23 at 03:41
  • And the reason I brought up Java versions is because I find it strange you have a JRE 8 installed in the first place. Therefore, I guessed it was because you were told you need a "JDK at compile time" and a "JRE at run time". I just wanted to point out that a JDK is basically a JRE with development tools included (e.g., `javac`). And then I assumed you didn't have a Java 19 JRE because you couldn't find a download of one from Oracle. They don't ship JRE-only packages anymore for newer versions of Java. Hence the link to another vendor (Adoptium). – Slaw Mar 06 '23 at 03:45
  • The error I get when running without `--enable-preview` is described on the post : `Error: LinkageError occurred while loading main class Aula3 java.lang.UnsupportedClassVersionError: Preview features are not enabled for Aula3 (class file version 63.65535). Try running with '--enable-preview'` – João Vitor da Silva Mar 07 '23 at 23:33
  • That's the error you get when you try to run the already-compiled program. I'm asking if you get any errors if you **compile** without `--enable-preview`. Specifically, when you compile the `Aula3` class. – Slaw Mar 07 '23 at 23:34
  • the reason I had both a JRE and a JDK in the first place is because I had the JRE first for games and such which require it, then added the JDK and uninstalled the JRE, now with only the JDK the games don't seem to be able to run without that specific JRE so I reinstalled it. I don't know how to make the games recognize the newer java version – João Vitor da Silva Mar 07 '23 at 23:37
  • Compiled code runs without any errors, with or without `--enable-preview` (I in fact never compiled manually using that option). The problem is that the command that's supposed to turn my .class into a .jar isn't recognized by the console (which is another problem I haven't been able to fix), so I'm forced to resort to VSCode's "export to jar" option, which creates the jar file directly from your uncompiled code. That jar file then has this issue – João Vitor da Silva Mar 07 '23 at 23:41
  • Then perhaps this can help: https://github.com/microsoft/vscode-java-debug/issues/1130 – Slaw Mar 07 '23 at 23:43
  • Also I just realized I didn't quite answer your question, but yes, no errors when I try to compile the code either (without `--enable-preview`) – João Vitor da Silva Mar 07 '23 at 23:43
  • I'm sorry, I added the folder and the file described on the link but I still get the same error :/ – João Vitor da Silva Mar 07 '23 at 23:52
  • Odd. It worked for me. Before I added it, running `java -jar foo.jar` failed with the expected `LinkageError` (running it with `java --enable-preview -jar foo.jar` works, obviously). After adding that file and preference, running `java -jar foo.jar` worked as expected (no preview feature problem). You said you added the folder and file, but just to make sure, did you add `org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled` inside the file? – Slaw Mar 08 '23 at 00:50
  • I did add it in the file, tho the settings folder didn't exist and I had to create it – João Vitor da Silva Mar 12 '23 at 20:49

0 Answers0