0

I'm doing this project where I have a webservice done in Java (HttpServlet) that uses Hibernate to select, insert and update information into a database and return an array of objects as JSON to whoever connects to the JSON endpoint.

I created it on my local computer and it works fine. However, I'm now trying to see how it would work on an actual server instead of locally.

So I connected through SSH to the server and copied the .war build to the Tomcat webapps folder, tried to run it, then I got the below error:

javax.servlet.ServletException: Servlet execution threw an exception

root cause java.lang.UnsupportedClassVersionError: org/hibernate/HibernateException : Unsupported major.minor version 52.0 (unable to load class org.hibernate.HibernateException)

So naturally I looked on StackOverflow to see what this error is about and I found that it's when you create your project in one version of Java and try to run the project on an inferior java machine. And I did that, indeed.

I created my project using Java 8 and I was running it on Java 7, on the server. So I updated the server to use Java 8. However, after updating to Java 8, I still get the same error.

If I run the java -version command, here's what I get:

LOCAL COMPUTER:

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

SERVER:

java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

As you can see, I have java 1.8.0_121 on my local machine and java 1.8.0._111 on the server. Do you think there's any chance that that is the problem? Aren't they both Java 8?

Also, how does Tomcat know which java to use? I have 3 versions on the server (2 versions of the Java 7 and one version of the Java 8, java 1.8.0_111). How does Tomcat know which one to use?

To choose which one to use I used the sudo update-alternatives --config java command and chose the 1.8.0_111 version from there (the server is Ubuntu 12.04).

The problem is - I keep getting that error: java.lang.UnsupportedClassVersionError: org/hibernate/HibernateException : Unsupported major.minor version 52.0 (unable to load class org.hibernate.HibernateException) - and I can't run my project on the server.

Any suggestions?

Robert Ruxandrescu
  • 627
  • 2
  • 10
  • 22

2 Answers2

0

When I got this error few days back, I had different versions of JDK and JRE running on my machine. I rectified it by setting the correct JRE path in Project's Build Path and also in the Window Preferences->Java->Installed JRE's. Also if you go to setenv.bat in your Tomcat\bin, there you specify which version of java should the Tomcat use by specifiying this:
set "JRE_HOME=%ProgramFiles%\Java\jre1.8.0_121", (obv, your version might be different so change that accordingly). I hope that answers your query!

Edit: If you are wondering what setenv.bat is, refer: https://docs.oracle.com/cd/E40518_01/integrator.311/integrator_install/src/cli_ldi_server_config.html it explains what this bat file should contain.

robot_alien
  • 955
  • 2
  • 13
  • 30
  • I'm in Linux, there's no "Tomcat\bin" folder. Do you know where you set that variable under Linux? Let me google that. – Robert Ruxandrescu Apr 03 '17 at 14:22
  • If I do an `echo $JAVA_HOME"` I get this: `echo $JAVA_HOME /usr/lib/java/jdk1.8.0_111` So it seems the right java version is available (this in the terminal, I suppose Tomcat gets which version of java to use by following that variable, so it should use the right version). – Robert Ruxandrescu Apr 03 '17 at 14:25
  • Ah! Thats supposed to be JRE_HOME, JDK_HOME seems alright, do check again! Also, try this: If you're using Eclipse, go to Run->External Tools Configuration->JRE tab, and look which version of JRE it's using. This is my last guess on this! Let me know. – robot_alien Apr 03 '17 at 14:32
  • I'm using Netbeans. If I go to the project properties - libraries - JDK 1.8 is selected. So it uses the right JDK. It also uses Hibernate, though. Maybe there's an issue with Hibernate? After all, I get the `unable to load class org.hibernate.HibernateException` error. – Robert Ruxandrescu Apr 03 '17 at 14:34
  • Project Properties is another and Run time configuration settings are a different one. Good if you have Project props set already.Look for the other one's possibility in your IDE, which version of JRE it picks up to execute your application(s). For your hibernate issue, check this: http://stackoverflow.com/a/18223559/1004631 – robot_alien Apr 03 '17 at 14:36
  • There is no such option, there's only "Java Platform: JDK 1.8". That's it. I guess it's being built on JDK 1.8 and if the server on the machine also uses that, it works. If not, it doesn't. – Robert Ruxandrescu Apr 03 '17 at 14:40
  • I also got the same error which OP posted here. I'm using Hibernate 5.2, will this version work on Java 1.7 ? – Suresh Jan 31 '18 at 09:41
  • @mannedear if you look at this link http://hibernate.org/orm/releases/ especially the Compatibility Matrix from the official website of hibernate, it says NO. – robot_alien Jan 31 '18 at 09:48
0

As per the Documentation,

Hibernate 5.2 and later versions require at least Java 1.8 and JDBC 4.2.

Hibernate 5.1 and older versions require at least Java 1.6 and JDBC 4.0.

When building Hibernate 5.1 or older from sources, you need Java 1.7 due to a bug in the JDK 1.6 compiler.

Suresh
  • 1,491
  • 2
  • 22
  • 27