01BTC10
01BTC10

Reputation: 536

Very simple problem with my first "Hello World" Java code

I saved this code in FirstApp.java:

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

Then this is what I get when trying to compile and run:

$ javac FirstApp.java
$ java FirstApp
Exception in thread "main" java.lang.UnsupportedClassVersionError: FirstApp : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: FirstApp. Program will exit.

What I am doing wrong?

Upvotes: 4

Views: 6215

Answers (5)

Ananthi
Ananthi

Reputation: 11

Check your compiler path when you are trying to run that program. Make sure the path is same when compiling and running.

Upvotes: 0

Hal
Hal

Reputation: 71

Basically it means you compiled with a newer version of Java and are trying to run with an older version, such as compiling with Java 7 but trying to run in a Java 6 environment.

You have 3 options.

1) Upgrade your runtime environment to match your development environment. (Make your JRE match your JDK.)

2) Downgrade your dev environment to match your runtime. (Make your JDK match your JRE.)

3) Use the -source and -target target args when compiling. So, for instance, if your runtime is 1.6, and your JDK is 7, you'd do something like javac -source 1.6 -target 1.6 *.java (Double check the docs for details, I might not have it quite right.)

Upvotes: 5

Perception
Perception

Reputation: 80603

This occurs when you attempt to run a java program with a lower level JVM than what it was compiled with. Somehow, your path is setup in such a way that javac is a higher version level than java.

Upvotes: 0

Ben Parsons
Ben Parsons

Reputation: 1443

You may have multiple versions of the JDK installed. See: http://www.java.net/node/664117

It means that you compiled your classes under a specific JDK, but then try to run them under older version of JDK.

Upvotes: 6

madmik3
madmik3

Reputation: 6973

This error happens when you run with a different runtime than you compiled with. Check your paths and make sure you are compiling and running with the same version.

more about this error here: http://geekexplains.blogspot.com/2009/01/javalangunsupportedclassversionerror.html

Upvotes: 1

Related Questions