1

when I run my HelloWorld class file after compiling I receive the following error message:

Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld :
Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        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$000(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)
Could not find the main class: HelloWorld.  Program will exit.

My program is as follow:

class HelloWorld {
    public static void main(String[] args)  {
        System.out.println("Hello, World");
    }
}

I am new to Java, and I have installed JDK 7 for the first time, so I do not have multiple versions. Please help.

neatnick
  • 1,489
  • 1
  • 18
  • 29
user2256222
  • 11
  • 1
  • 2
  • these errors usually mean youve compiled your code to target a newer version of java than the one youre trying to run the code with. is ti possible youve compiled with JDK7 yet are trying to run on JRE 6? see http://stackoverflow.com/questions/10382929/unsupported-major-minor-version-51-0 – radai Apr 08 '13 at 05:34
  • can you check version with java --version – shola Apr 08 '13 at 05:35

2 Answers2

1

From the api

Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.

Means your compilation level is higher JDK and

You are using lower runtime environment then compilation level.

subodh
  • 6,136
  • 12
  • 51
  • 73
  • I don't understand how it happens. I just installed for the first time, and I took both JDK and JRE 7. Should I uninstall everything and reinstall from a different site? – user2256222 Apr 08 '13 at 05:48
  • No, You may have already installed jdk. Right click on your project go to properties and then java compiler see the compilation level of your java project. Is it set lower version then you have. – subodh Apr 08 '13 at 05:52
0

You are compiling the class with Java Version 7 (JDK 7). But you try to start it with a lower Version of Java.

you can enter "java -version" in a command-shell to take a look whats your installed default java version.

sushicutta
  • 1,536
  • 1
  • 9
  • 11
  • I entered java -version and got this: C:\JDK>java -version java version "1.6.0_20" Java(TM) SE Runtime Environment (build 1.6.0_20-b02) Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode) – user2256222 Apr 08 '13 at 05:43
  • your compiled class-file is compiled for the java version >= 7. that is the default if you compile something with jdk 7. the compiler for java is javac, see http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html – sushicutta Apr 08 '13 at 05:48
  • with javac you can use the option "target". so "javac -target 1.6" would end up in a class-file that also runs on java 1.6_0_20. – sushicutta Apr 08 '13 at 05:50
  • but if you have jdk 7. you should run your compiled classes also with the jre 7. jdk: Java Development Kit. jre: Java Runtime Environment. the jre is also installed with jdk. in the bin folder of jdk, see c:\jdk\bin, it has a java and javaw executable. with this you can run your compiled classes. – sushicutta Apr 08 '13 at 05:53