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:
Eclipse Errors: The project in question is the "Exemplohibernate":
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);
}
Add code and error in textual form, not in foma image.
– user28595
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.
– Lucas Pletsch
Remove the driver, download again and reset, it might just be corrupted.
– user28595
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
– Lucas Pletsch
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).
– Piovezan
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
– Jefferson Quesado