1

a simple hello word program gets compiled but when I run it I get the following exception.

Exception in thread "main" java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class:HelloWorldApp.Program will exit.

I have java version 1.6.0_21

My program is:

class HelloWorldApp { 
   public static void main(String[] args) { 
   System.out.println("Hello World!"); // Display the string. 
   }
}
David Brossard
  • 13,584
  • 6
  • 55
  • 88
madhurgarg
  • 1,301
  • 2
  • 11
  • 17
  • 1
    how simple is this simple hello world program? can we see the code? – Katana24 Oct 21 '13 at 17:24
  • 1
    possible duplicate of [Unsupported major.minor version 51.0](http://stackoverflow.com/questions/10382929/unsupported-major-minor-version-51-0) – jmj Oct 21 '13 at 17:24
  • My program is:class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } } – madhurgarg Oct 21 '13 at 17:36

5 Answers5

5

You compiled the .class for one version and tried to run it on an older version.

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

You compiled your Java code for one version of the Java JRE (e.g. version 7) but you ran it against an older version of the JRE (e.g. version 6).

To find out which version you have currently set up, you can open a shell or command prompt (in Windows) and type:

java -version

This will return the version number.

===edit=== Adding more details to reply to the comment Java is a binary that comes from either the JRE or the JDK. There are different JRE/JDK providers out there, the main one being Oracle.

If you are on Windows, you can go to C:\Program Files\Java or C:\Program Files (x86)\Java (32bit) and you will notice you have JRE folders and JDK folders. A JDK always contains a JRE. The JRE is the bare minimum you need to run a Java program. Normally, you always have the same JRE version as you do JDK version. To keep things simple, always develop and test against your JDK. Make sure you choose the right one.

How do you know which is in use? That's based on your environments variables. In a Windows command prompt you can write the following:

C:\Users\djob>set JA

and that will reply with

JAVA_HOME=C:\Program Files\Java\jdk1.7.0_04

In that case you know which JRE is being used: the one that comes with the JDK 1.7.0u04. By the way, Java 7 is Java 1.7 (this is a Java convention) much like Java 6 is in fact Java 1.6.

Note that your IDE (e.g. Eclipse) may be set up with a different JRE so do check there as well.

HTH

David Brossard
  • 13,584
  • 6
  • 55
  • 88
0

You run a class compiled in Java 7 with a Java 6 JVM

Julien
  • 2,544
  • 1
  • 20
  • 25
0

The version of class file compiled with the newer version of Java. You need recompile your source code. If you can't recompile it or you don't have sources you can always upgrade to the new version of Java SE. Remember when you installed Java JDK or JRE there's always jshed process running and it always suggest you to do upgrade when the version of Java changes. All yo need is give it access to the internet, and once connected it will do its job, like magic.

0

The java version compiled with not same as you set in JAVA_HOME

maroon912
  • 311
  • 2
  • 8