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 I try 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 am I doing wrong?

+4
source share
5 answers

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

This means that you compiled your classes under a specific JDK, but then try to run them in the old version of the JDK.

+6
source

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

You have 3 options.

1) Update the runtime to suit your development environment. (Make your JRE suitable for the JDK.)

2) Lower your development environment to fit your runtime environment. (Make your JDK suitable for the JRE.)

3) When compiling, use the arguments -source and -target target. So, for example, if your runtime is 1.6 and your JDK is 7, you would do something like javac -source 1.6 -target 1.6 * .java (double check the docs, I might not be completely correct).

+5
source

This error occurs when you run a different runtime than you compiled. Check your paths and make sure you compile and run the same version.

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

+1
source

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

0
source

Check your compiler path when you try to run this program. Make sure the path is the same when compiling and starting.

0
source

Source: https://habr.com/ru/post/1395987/


All Articles