简体   繁体   中英

Could not find main class:Main, the application will exit

I've got Main.java, which looks like this:

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        System.out.println("here!");
    }

}

I compile it with:

javac Main.java

And try to run it with

java Main

and it dies with folloving exception:

Exception in thread "main" java.lang.UnsupportedClassVersionError: Main : 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: Main. Program will exit.

java -version is:

java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.3) (6b24-1.11.3-1ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

I'm running this on Ubuntu server.

What am I doing wrong?

UPDATE

javac -version is

javac 1.7.0_03

which java

/usr/bin/java

which javac

/usr/bin/javac

You're trying to run with Java 6, but it looks like you're compiling with Java 7.

Run

javac -version

to check... and assuming it shows Java 7, you need to work out why the two are different.

Running

$ which java
$ which javac

might help... the information would certainly help us to help you.

java.lang.UnsupportedClassVersionError: Main : Unsupported major.minor version 51.0

It seems jdk you have in classpath for compilation is different from the one you have in runtime. Make sure you have same jdk available in both classpaths.

I ran into exact same problem on Ubuntu. My problem was some original libraries from openjdk-6-jre-lib needed to be completely removed. Remove that package and anything relating to openjdk 6 or jre version 6 in your synaptic manager. If you are on Ubuntu 12, you'll need to install synaptic as they removed it. I dislike using the software manager. Make sure you have version 7 installed of the open jdk.

openjdk-7-jre-lib

Keep in mind, this is the alternative to using Oracle's Java 7 install. The open means open source version.

If you install openjdk 7 on Ubuntu, the classpath should be setup automatically. I didn't have to tweak that. If you do want to tweak that, you can set it in ~/.bash_profile. But keep in mind you will have limited areas (need to be under your ~/ hierarchy) where you can compile fron the command line. And you need to reload using "drew: source .bash_profile". I tweaked classpath in /etc/environment and it messed up synaptic package manager regarding openjdk 6 on ubuntu 12. I couldn't uninstall openjdk 6 with synaptic when classpath was altered in anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM