Thursday, 2 June 2016

How to solve Unsupported major.minor version 51.0

As per the javadoc, UnsupportedClassVersionError thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.

When UnsupportedClassVersionError occurs?
When you compile a class using higher jdk version (like Java8) and trying to run using lower jdk (like Java7), you will get this error.
Let me give an example,


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


Let me compile above program using Java8 compiler and try to run using Java7 virtual machine.
C:\java_programs>"C:\Program Files\Java\jdk1.8.0_45\bin\javac" HelloWorld.java

C:\java_programs>"C:\Program Files\Java\jdk1.7.0_79\bin\java" HelloWorld
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)


As you see the output, Java run time throws “java.lang.UnsupportedClassVersionError: HelloWorld : Unsupported major.minor version 52.0”.

What is 52.0 here?
When you compile a Java class file, Java embed the major version number of the class file format being used. Java run time environment checks this number before running. If this number is greater than your java run time environment, you will get "Unsupported major.minor version" error.

Following table summarizes the major version numbers for different java run time environments.

Java Version
Major version number
Java SE 9
53 (0x35 hex)
Java SE 8
52 (0x34 hex)
Java SE 7
51 (0x33 hex)
Java SE 6.0
50 (0x32 hex)
Java SE 5.0
49 (0x31 hex)
JDK 1.4
48 (0x30 hex)
JDK 1.3
47 (0x2F hex)
JDK 1.2
46 (0x2E hex)
JDK 1.1
45 (0x2D hex)

How to resolve above error?
There are two ways.

Approach 1: One way is upgrade your Java environment to higher version.

Approach 2: Use javac -target option
‘javac –target’ generate class files for specific VM version.

"C:\Program Files\Java\jdk1.8.0_45\bin\javac" -source 1.7 -target 1.7 HelloWorld.java

Above statement compiles HelloWorld.java using java compiler 1.8 and set the target level to 1.7

C:\java_programs>"C:\Program Files\Java\jdk1.8.0_45\bin\javac" -source 1.7 -target 1.7 HelloWorld.java
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning

C:\java_programs>"C:\Program Files\Java\jdk1.7.0_79\bin\java" HelloWorld
Hello World


You may like

Reference



Wednesday, 1 June 2016

JNDI: Get all group distinguished names


Search for (objectclass=group) and request the distinguishedName attribute. This will get all the group names.

import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class DirectoryUtil {
 private final static String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
 private final static String PROVIDER_URL = "ldap://10.212.141.191:389";
 private static final Properties properties = new Properties();
 private static String adminDN = "CN=admin,CN=Users,DC=example,DC=com";
 private static String adminPassword = "password";

 static {
  initProperties();
 }

 private static void initProperties() {
  properties.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  properties.put(Context.PROVIDER_URL, PROVIDER_URL);
  properties.put("com.sun.jndi.ldap.connect.pool", "true");
  properties.put(Context.SECURITY_PRINCIPAL, adminDN);
  properties.put(Context.SECURITY_CREDENTIALS, adminPassword);
 }

 public static DirContext getContext() throws NamingException {
  DirContext context = new InitialDirContext(properties);
  return context;
 }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

/* get Distinguished name of user */
public class Test {
 /**
  * Return all group names (return the name mapped to cn)
  * 
  * @param context
  * @param baseDN
  * @return
  */
 static Optional<List<String>> getAllGroupNames(DirContext context,
   String baseDN) {

  if (Objects.isNull(context)) {
   System.out.println("context shouldn't be null");
   return Optional.empty();
  }

  List<String> groupNames = new ArrayList<>();

  SearchControls ctls = new SearchControls();
  String[] attrIDs = { "distinguishedName" };
  ctls.setReturningAttributes(attrIDs);
  ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

  NamingEnumeration<?> answer;
  try {
   answer = context.search(baseDN, "(objectclass=group)", ctls);
   while (answer.hasMore()) {
    SearchResult searchResult = (SearchResult) answer.next();
    Attributes attrs = searchResult.getAttributes();

    Attribute cnAttr = attrs.get("distinguishedName");

    NamingEnumeration<?> values = cnAttr.getAll();

    while (values.hasMoreElements()) {
     groupNames.add(values.next().toString());
    }
   }

  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return Optional.empty();
  }

  return Optional.of(groupNames);
 }

 public static void main(String[] args) throws NamingException {
  DirContext context = DirectoryUtil.getContext();
  String baseDN = "CN=Users,DC=example,DC=com";

  Optional<List<String>> groupsTmp = getAllGroupNames(context, baseDN);

  List<String> groups = groupsTmp.get();

  for (String group : groups) {
   System.out.println(group);
  }
 }
}


Previous                                                 Next                                                 Home