Home > Back-end >  SWT runs on a different version of java
SWT runs on a different version of java

Time:04-29

First and foremost, I know very little about Java and even less about SWT, so bare with me here. Earlier today I tried to run this simple program I found on https://mkyong.com/swt/swt-hello-world-example

here is the code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SWTHelloWorld {

public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new Shell(display);
    
    Text helloWorldTest = new Text(shell, SWT.NONE);
    helloWorldTest.setText("Hello World SWT");
    helloWorldTest.pack();
    
    shell.pack();
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

When I run it I get this error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/eclipse/swt/widgets/Composite has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

What I understand from this is that SWT is getting compiled with Java 11 when it should be compiled with Java 8, BUT -and that's a big BUT- I don't have Java 11 I deleted it to only have Java 8 and avoid this kind of problems. When I run in my terminal the commands java -version and javac -version in both cases i get this version '1.8.0_331' and there is nothing else. I also checked my settings and configurations in Eclipse and everything seems to be set to run on Java 8. Ultimately I tried this command in the terminal javac -target 1.8.0_331 SWTHelloWorld.java to target the right compiler, but I got javac: invalid target release: 1.8.0_331. I don't know where to go from there. Anyone?

Thanks in advance guys!

PS: I'm on macOS Big Sur 11.4 if that matters.

CodePudding user response:

Uninstalling Java 11 does not change which version SWT itself was compiled for. Your application requires at least Java 11 because a library you're using, SWT, requires it.

  • Related