0

I am trying to compile and run HelloWorld on linux machin with java version 1.6. I get it compiled but can not run it. I know that the program is at least correct.

Can anybody tell me how to get the silly program run?

I know even this problem had been metioned several times but nowhere found the fix of that problem. Compile command I used: javac ExampleProgram.java. Run Command I used: java ExampleProgram

//A Very Simple Example
class ExampleProgram {
  public static void main(String[] args){
    System.out.println("I'm a Simple Program");
  }
}

The error:

java -classpath . ExampleProgram
Exception in thread "main" java.lang.UnsupportedClassVersionError: ExampleProgram : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: ExampleProgram.  Program will exit.
6
  • 5
    You are compiling and running with different version of java Commented Mar 28, 2013 at 18:57
  • That does not help me much how to fix the problem. When I check the version of java, it tells me: java version "1.6.0_35" Java(TM) SE Runtime Environment (build 1.6.0_35-b10) Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01, mixed mode) Commented Mar 28, 2013 at 19:00
  • Your jre and jdk should be same version... Commented Mar 28, 2013 at 19:01
  • How to fix it that compiling and running can be issued with the same version? Commented Mar 28, 2013 at 19:01
  • 1
    check your javac version using javac -version Commented Mar 28, 2013 at 19:02

1 Answer 1

2

Your program is compiled with JDK 1.7, but you are actually running it with an earlier version of Java.

If you are invoking the program in command line, type

java -version

to see which version of Java you are actually using.

Or, if you are using an editor like Eclipse, check out your Java settings.

Another solution is to use

javac -source 1.6 -target 1.6 SimpleProgram.java

instead of

javac SimpleProgram.java
Sign up to request clarification or add additional context in comments.

5 Comments

When I check the version of java, it tells me: java version "1.6.0_35" Java(TM) SE Runtime Environment (build 1.6.0_35-b10) Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01, mixed mode)
Also see this stackoverflow.com/questions/11964010/… I answered a similar question a while ago
Can you delete the SimpleProgram.class file and try again with javac -source 1.6 -target 1.6 SimpleProgram.java? It should work.
@su- OPs Classname is class ExampleProgram
I see. That's why. Thanks Smit. MMAB, as Smit pointed out, you were compiling class A and executing class B.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.