"The Java Exception has ocurred" error while running the Postgre JDBC driver

Asked

Viewed 136 times

0

Follow my problem:

1 - I am using the Postgre 9.6 database.

2 - I downloaded and opened in ECLIPSE a project with example Java application with the use of Hibernate and Postgre that the teacher created (that is, the code is correct) I put the Postgre JDBC driver jar, version: 4.2.0.0 in Eclipse in "Java Build Path".

3 - The version of Jdk that is installed and linked to Eclipse is 1.7.0_80 which, I thought would be compatible with Postgre and the above JDBC driver.

The error is: When running the application, Eclipse displays errors:

Exception in thread "main" java.lang.UnsupportedClassVersionError: br/com/bd2/teste/TesteDatabase : 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)

And the JVM opens an alert window with this message : "The Java Exception has ocurred!"

Below are the prints:

inserir a descrição da imagem aqui

Eclipse Errors: The project in question is the "Exemplohibernate":

inserir a descrição da imagem aqui

And this is the code of my main class:

package br.com.bd2.teste;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

import br.com.bd2.exemplo.model.Fruteira;

public class TesteDatabase {
    public static void main(String[] args) {
        // Inicializacao da fabrica de objetos
        // persistenceUnitName = "bd2", ou seja, relaciona com a conexao na
        // persistence.xml (podera ter "n" unidades de persistencia)
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("bd2_persistence_unit");
        EntityManager em = emf.createEntityManager();

        // Os comandos a seguir devem ser executados "um por vez"
        // Inserindo...
        em.getTransaction().begin();
        em.persist(new Fruteira("Fruteira"));
        em.persist(new Fruteira("Fruteira 3"));
        em.getTransaction().commit();

        // Atualizando...
        // Buscara a fruteira com id = 1. Observe o numero do id pelo pgAdmin3!
        Fruteira fruteira = em.find(Fruteira.class, 1L); 
        if (fruteira != null) {
            em.getTransaction().begin();
            fruteira.setNome("Fruteira Legal");
            em.merge(fruteira);
            em.getTransaction().commit();
        }
        // Recuperando "n" objetos...
        TypedQuery<Fruteira> q = em.createQuery("SELECT f " +
                                                "FROM Fruteira f", Fruteira.class);
        for (Fruteira each : q.getResultList()) {
            System.out.println(each.toString());
        }       

        // Excluindo...
        // Buscara a fruteira com id = 1. Observe o numero do id pelo pgAdmin3!
        fruteira = em.find(Fruteira.class, 1L); 
        if (fruteira != null) {
            em.getTransaction().begin();
            em.remove(fruteira);
            em.getTransaction().commit();
        }

        // Recuperando "n" objetos...
        q = em.createQuery("FROM Fruteira f", Fruteira.class);
        for (Fruteira each : q.getResultList()) {
            System.out.println(each.toString());
        }

        System.exit(0);
    }
  • 2

    Add code and error in textual form, not in foma image.

  • Articuno, it’s not about the source code. It’s the environment configuration, and this is what I wanted to show you: the project details in the left tab of the eclipse, and the error message.

  • 1

    Remove the driver, download again and reset, it might just be corrupted.

  • OK Articuno. I did this, I downloaded from the original source included, and it didn’t work, it remains exactly the same error... Let’s try other ideas. : D

  • 4

    Put the exception in text form and not screenshot. Although you say that the Java version is compatible, you can see by the error that it is a version incompatibility. Review the project properties for compiler version, library version differences, etc (and yes, the code is important for us to know when the exception occurs and have an idea if it has to do with the JDBC driver, Hibernate, etc).

  • 1

    I’m particularly used to this mistake "Unsupported major/minor". This means what Edjane put in the answer: it was compiled with JDK 8. This number (52.0) refers to the format of the class file (read more). Java recognizes this by reading from the fifth to the eighth byte of the file (bytes 7-8 represent a shorts with the version major, which in this case is 52). I recommend abandoning Java 7 and diving into Java 8 (or even Java 9), the beauties of Java 8 make up for everything

Show 1 more comment

1 answer

3

You are using the Java 8 drive

As you are using Java 7 the compatible drive is:

Postgresql JDBC 4.1 Driver, 42.2.1.jre7 //this is the latest version, if you want you can search by 42.0.0.jre7

You can download the correct drive directly from the website Postgresql

  • The JDBC driver I’m using is from Java 8?

  • 1

    @Lucaspletsch.

  • 1

    Yeah, just take a look at the documentation!

  • Ok. I went here: https://jdbc.postgresql.org/download.html and downloaded a driver jdbc 4.0 that is supported even by jdk 6. And the error persists. Can I go crazy now? kkkk

  • 2

    For Java 7, it has to be JDBC 4.1 You tried this: https://jdbc.postgresql.org/download/postgresql-42.2.1.jre7.jar

  • Edjane and Piovezan, I think I figured it out: I think the version of Hibernate I downloaded is 5.2. And it only supports Java 8 up. -

  • 3

    "Current Version 42.2.1 This is the Current version of the driver. Unless you have Unusual Requirements (running old Applications or Jvms), this is the driver you should be using. (...) If you are using Java 7 then you should use the JDBC 4.1 version."

  • We have no way of knowing if it’s Hibernate or JDBC because the exception log you posted is incomplete.

  • I will download an older version of Hibernate, which runs with Java 7 tomorrow and test. I don’t upgrade to Java 8 because I would have to upgrade my Eclipse as well, and I don’t intend to do that this week. There are many compatibility issues involved there. Anyway, thank you guys. Hug!

  • Piovezan, worse than I put the whole log. Have to scroll the bottom bar right to see everything. : S

  • I put all the log that appears on the Eclipse console. There is something else ?

  • I go to sleep, tomorrow I try with an older Hibernate. Thanks!

  • I was wrong, sorry. This must be the whole log.

  • It did not help to put a Hibernate that supports Java 7. I will have to try other things

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.