Unsupported major.minor version 52.0 [duplicate]

2018-12-31 00:48发布

This question already has an answer here:

Pictures:

Command Prompt showing versions Command Prompt showing versions

Picture of error Picture of error

Hello.java

import java.applet.Applet;
import java.awt.*;

public class Hello extends Applet {

    // Java applet to draw "Hello World"
    public void paint (Graphics page) {
        page.drawString ("Hello World!", 50, 50);
    }
}

Hello.html

<HTML>
    <HEAD>
        <TITLE>HelloWorld Applet</TITLE>
    </HEAD>

    <BODY>
        <APPLET CODE="Hello.class" WIDTH=300 HEIGHT=150>
        </APPLET>
    </BODY>
</HTML>

Error

Hello : Unsupported major.minor version 52.0

What may the problem be?

26条回答
笑指拈花
2楼-- · 2018-12-31 01:25

You will need to change your compiler compliance level back to 1.7 in your IDE.

This can be done in the preferences settings of your IDE. For example, in Eclipse go to menu WindowsPreferences, select Java, and expand it. Then select Compiler and change the compliance level to 1.7. I am sure this will work from there.

查看更多
永恒的永恒
3楼-- · 2018-12-31 01:25

I could solve the same problem using the below solution.

In my project, I added a JAR file which were created in Java 8. And my project was referring to JRE 7. When I changed project JRE to 8, my problem was solved.

Steps:

In Eclipse, right click on the project name in project explorer → Build path → Libraries → click on JRE version → click EditInstalled JRE → Add → Standerd VM → select JRE home click-path (path should be localePath\java\jdk1.8.0_25\jre) → provide name → Save → select same JRE for project → Finish → OK. Refresh/build project once → try to run your Java file. It should work.

查看更多
若你有天会懂
4楼-- · 2018-12-31 01:27

The issue is because of Java version mismatch. Referring to the Wikipedia Java Class Reference:

  • Java SE 12 = 56 (0x38 hex)
  • Java SE 11 = 55 (0x37 hex)
  • Java SE 10 = 54
  • Java SE 9 = 53
  • Java SE 8 = 52
  • Java SE 7 = 51
  • Java SE 6.0 = 50
  • Java SE 5.0 = 49
  • JDK 1.4 = 48
  • JDK 1.3 = 47
  • JDK 1.2 = 46
  • JDK 1.1 = 45

These are the assigned major numbers. The error regarding the unsupported major.minor version is because during compile time you are using a higher JDK and a lower JDK during runtime.

Thus, the 'major.minor version 52.0' error is possibly because the jar was compiled in JDK 1.8, but you are trying to run it using a JDK 1.7 environment. The reported number is the required number, not the number you are using. To solve this, it's always better to have the JDK and JRE pointed to the same version.

In IntelliJ IDEA,

  1. Go to Maven SettingsMavenImporting. Set the JDK for importer to 1.8.
  2. Go to Maven SettingsMavenRunner. Set the JRE to 1.8.
  3. Go to menu File* → Project StructureSDKs. Make sure the JDK home path is set to 1.8.

Restart IntelliJ IDEA.

Another approach which might help is by instructing IntelliJ IDEA which JDK version to start up with.

Go to: /Applications/IntelliJ\ IDEA\ 15\ CE.app/Contents/Info.plist and replace the JVM version with:

<key>JVMVersion</key>
<string>1.8*</string>
查看更多
皆成旧梦
5楼-- · 2018-12-31 01:27

If you are using Linux and you have different versions of Java installed, use the following command:

sudo update-alternatives --config java

This will give a quick way of switching between the Java versions installed on the system. By choosing Java 8 I will solve your problem.

查看更多
何处买醉
6楼-- · 2018-12-31 01:30

You need to upgrade your Java version to Java 8.

Download latest Java archive

Download latest Java SE Development Kit 8 release from its official download page or use following commands to download from the shell.

For 64 bit

 # cd /opt/

 # wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.tar.gz"

 # tar xzf jdk-8u51-linux-x64.tar.gz

For 32 bit

 # cd /opt/

 # wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-i586.tar.gz"

  # tar xzf jdk-8u51-linux-i586.tar.gz

Note: If the above wget command doesn’t not work for you, watch this example video to download the Java source archive using the terminal.

Install Java with alternatives

After extracting the archive file, use the alternatives command to install it. The alternatives command is available in the chkconfig package.

 # cd /opt/jdk1.8.0_51/

 # alternatives --install /usr/bin/java java /opt/jdk1.8.0_51/bin/java 2

 # alternatives --config java

At this point Java 8 has been successfully installed on your system. We also recommend to setup javac and jar commands path using alternatives:

 # alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_51/bin/jar 2

 # alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_51/bin/javac 2

 # alternatives --set jar /opt/jdk1.8.0_51/bin/jar

 # alternatives --set javac /opt/jdk1.8.0_51/bin/javac

Check installed Java version

Check the installed version of Java using the following command.

root@tecadmin ~# java -version

java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

Configuring Environment Variables

Most of Java-based applications use environment variables to work. Set the Java environment variables using the following commands:

Setup JAVA_HOME Variable

# export JAVA_HOME=/opt/jdk1.8.0_51
Setup JRE_HOME Variable

# export JRE_HOME=$JAVA_HOME/jre
Setup PATH Variable

# export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

Note that the change to the PATH variable put the new Java bin folders first so that they override any existing java/bins in the path. It is a bit sloppy to leave two java/bin folders in your path so you should be advised to clean those up as a separate task.

Also, put all above environment variables in the /etc/environment file for auto loading on system boot.

查看更多
宁负流年不负卿
7楼-- · 2018-12-31 01:30

You need to use JDK 1.7.0 rather than JDK 1.8.0.

To make sure it, you need to delete JDK 1.8.0 on your computer.

If you use Mac, you need to delete:

/Library/Java/JavaVirtualMachines/jdk.jdk

/Library/PreferencePanes/JavaControlPanel.prefPane

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin

Then, you need to reinstall JDK 1.7.0, and you will succeed to generate the .jar file.

查看更多
登录 后发表回答