Issue to JRE

HuaMin Chen
HuaMin Chen used Ask the Experts™
on
Hi,
How to upgrade JRE to 11+, or downgrade your JDBC driver to Java 8, within CentOS 6?

Here is exception I've got

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Main.doConnect(Main.java:86)
at Main.main(Main.java:46)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
For Java, you can just download and install the JDK (or JRE) to any path on your system and then just make sure to set the JAVA_HOME environment variable to point to that new location - instead of the default Java install that came with CentOS 6.

In other words, it's not necessary to upgrade the installed Java version that comes with CentOS (which can be a pain given how interconnected CentOS is), you can just add a second one and ignore the original.

This is how we handle our Java installations and we run CentOS for all our production systems.

You can download the Linux installation from:
https://www.oracle.com/technetwork/java/javase/downloads/jdk13-downloads-5672538.html

Doug
Top Expert 2016

Commented:
Are you using MS SQL Server on Linux? That's normally (if not always) an RDBMS for Microsoft Windows. Maybe the server itself IS on Windows ...
Top Expert 2016

Commented:
When you download the driver, there are multiple JAR files. The name of the JAR file indicates the version of Java that it supports.
( https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15 )
Even the very latest version of the driver supports Java 8
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

HuaMin ChenProblem resolver

Author

Commented:
Thanks to all.

What to adjust in project below? Do I need to change JAR file being used?
1d.png
Top Expert 2016

Commented:
DId you read the link i just posted?
HuaMin ChenProblem resolver

Author

Commented:
I changed JRE to version 11 below,
2b.pngBut I still get this exception

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Main.doConnect(Main.java:86)
at Main.main(Main.java:46)
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
I changed JRE to version 11 below

But you haven't, by that screenshot you are still using Java 8. Look at the last entry in that list.
HuaMin ChenProblem resolver

Author

Commented:
How to change against last item "JavaSE 1.8" in the screenshot above?
Top Expert 2016

Commented:
You're using the wrong driver jar. You need to use the one for Java 8. I would advise against upgrading the JRE to solve that simple problem - it could have knock-on effects that could break earlier projects since Java is no longer backwardly compatible
HuaMin ChenProblem resolver

Author

Commented:
Sorry, what are the details to use Java 8?
Top Expert 2016

Commented:
mssql-jdbc-7.4.1.jre8.jar in the distribution is what you need. If you get dependency problems, try mssql-jdbc-7.4.1.jre8-shaded.jar
HuaMin ChenProblem resolver

Author

Commented:
Sorry, I do not see file "mssql-jdbc-7.4.1.jre8.jar" or "mssql-jdbc-7.4.1.jre8-shaded.jar", within current file - "mssql-jdbc-7.4.1.jre11".
Top Expert 2016

Commented:
within current file - "mssql-jdbc-7.4.1.jre11".
It won't be within that one. It will be in the distribution archive that you downloaded (probably a zip file on Windows)
HuaMin ChenProblem resolver

Author

Commented:
There are several versions of windows file. Which should be the one?
Top Expert 2016

Commented:
I don't know. If in doubt, just download the latest driver distribution again
Top Expert 2016
Commented:
Yes. The link is in the leftmost column. As you can see, it clearly states that JRE 8 is supported
Microsoft JDBC Driver 7.4       8/1/2019       JRE 8, 11, 12
HuaMin ChenProblem resolver

Author

Commented:
Here is the project
9b.png
but I still have got this exception
java.sql.SQLException: No suitable driver found for sa
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Main.doConnect(Main.java:88)
at Main.main(Main.java:46)

Top Expert 2016

Commented:
Looks like the latest driver class is
com/microsoft/sqlserver/jdbc/SQLServerDriver

Open in new window

mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
Can you post the code that is loading the driver and the code that is making the connection to the database?
HuaMin ChenProblem resolver

Author

Commented:
Carl,
Here is how it is doing the connection
      public static boolean doConnect(){
            try{
                  final String un = "sa";
                  final String pw = "****";
                  final String host = "jdbc:sqlserver://?.?.?.?:59365;databaseName=acc_sch" ;

                  final String sql = "SELECT * FROM customer3";
                  //loading the driver class
                  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //This is for driver sqljdbc2*
                  //   Class.forName("com.microso ft.jdbc.sq lserver.SQ LServerDri ver"); /*This is for driver sqljdbc4*/
                  final Connection conn = DriverManager.getConnection(un, pw, host);
                  final Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE );
                  final ResultSet rs = stmt.executeQuery(sql);
                  while (rs.next()){
                        System.out.println(rs.getString(1));
                        conn.close();
                  }
                  return true;

            } catch (final Exception ex) {
                  ex.printStackTrace();
             JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Information", JOptionPane.INFORMATION_MESSAGE);
                  return false;
            }
      }

Open in new window

IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
On line 11 (of the above cope snippet) you are calling DriverManager.getConnection() with the parameters in the wrong order. It should be...

                  final Connection conn = DriverManager.getConnection(host, un, pw);

Open in new window

HuaMin ChenProblem resolver

Author

Commented:
Sorry to that I got the following, after making your suggested change.
9c.png
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
That output is next to useless without the error "message" that goes with it. On line 21 (of above code snippet) you also print the full stack trace to the console, can you get the full error message/stack trace text from the console and post it?
Top Expert 2016

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial