Oct 21, 2014

f Comment

Java: Fixing java.lang.UnsupportedClassVersionError and Unsupported major.minor version error!

Amazon I was reading http://docs.oracle.com/javase/tutorial/networking/nifs/parameters.html and decided to compile run the Java code listed on that webpage. Here is the code:
import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class ListNetsEx {

    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %s\n", inetAddress);
        }
       
        out.printf("Up? %s\n", netint.isUp());
        out.printf("Loopback? %s\n", netint.isLoopback());
        out.printf("PointToPoint? %s\n", netint.isPointToPoint());
        out.printf("Supports multicast? %s\n", netint.supportsMulticast());
        out.printf("Virtual? %s\n", netint.isVirtual());
        out.printf("Hardware address: %s\n",
                    Arrays.toString(netint.getHardwareAddress()));
        out.printf("MTU: %s\n", netint.getMTU());
        out.printf("\n");
     }
}
I happily compiled the code with javac ListNetsEx.java and ran it with java ListNetsEx. To my dismay I got the following error message:

Exception in thread "main" java.lang.UnsupportedClassVersionError: <your Java class name> :
Unsupported major.minor version 52.0

It turns out my Java JRE runtime environment is newer than the Java program. How do I fix the Java exception java.lang.UnsupportedClassVersionError and Unsupported major.minor version error?

Solution

I began to look for a solution on the Internet. Somewhere online I found a solution, which is run the following command :

javac -target 1.7 ListNetsEx.java

But then I got the following error:

javac: target release 1.7 conflicts with default source release 1.8

The REAL solution is run the following command:

javac -target 1.7 -source 1.7 ListNetsEx.java

You will get a warning:

warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

But it is harmless. Now run the following command:

java ListNetsEx

and you should see the output!

If it still doesn't work, try 1.6 instead of 1.7 in the command. If it still doesn't work, try 1.5. Try lowering the version number until it works.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael