Unsupportedclassversionerror when trying to generate a jar

Asked

Viewed 282 times

-2

I compiled my code and everything and when I will open the file .jar valet says A JNI error has occured

cmd:

C:\Projetos\Java\Projetos\Projeto02>java -jar Gerar.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: principal has been compiled by a more recent version of the Java Runtime (class file version 53.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(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Code:

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;

class principal {

public static void main(String[] args){

        /*cria o layout e a janela */
        JFrame frame = new JFrame("Gerador");
        JPanel panel = new JPanel(new GridBagLayout());


        /*JLabel do lado dos objetos*/
        GridBagConstraints gbc8 = new GridBagConstraints();
        gbc8.anchor = GridBagConstraints.EAST;
        gbc8.gridx = 0;
        gbc8.gridy = 0;
        JLabel itml = new JLabel("Nome do Arquivo:");
        panel.add(itml, gbc8);

        /*Cria e posiciona o JTextField*/
        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.anchor = GridBagConstraints.WEST;
        gbc1.gridx = 1;
        gbc1.gridy = 0;
        JTextField texto = new JTextField();
        texto.setColumns(10);
        panel.add(texto, gbc1);

        /*Cria e posiciona o JComboBox*/
        String[] items = {".txt", ".java", ".html", ".php", ".xml", ".odt", ".ods", ".jar", ".sql", ".pdf"};
        JComboBox combo = new JComboBox(items);
        combo.setBackground(Color.WHITE);
        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.anchor = GridBagConstraints.WEST;
        gbc2.gridx = 1;
        gbc2.gridy = 1;
        panel.add(combo, gbc2);

        /*JLabel do lado dos objetos*/
        GridBagConstraints gbc9 = new GridBagConstraints();
        gbc9.anchor = GridBagConstraints.EAST;
        gbc9.gridx = 0;
        gbc9.gridy = 1;
        JLabel itm2 = new JLabel("Extens\u00e3o:");
        panel.add(itm2, gbc9);

        /*Cria e posiciona o JButton*/
        GridBagConstraints gbc3 = new GridBagConstraints();
        gbc3.anchor = GridBagConstraints.WEST;
        gbc3.gridx = 1;
        gbc3.gridy = 2;
        JButton butao = new JButton("Gerar");
        panel.add(butao, gbc3);


        butao.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {

            String arquivo = texto.getText() + combo.getSelectedItem();
            File file = new File(arquivo);

            texto.setText("");
            combo.setSelectedIndex(0);
            try{file.createNewFile();}catch(IOException ex){ex.printStackTrace();}




        }
        });

        /*Configurações da janela*/
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setSize(250, 250);
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
  • How are you riding that jar?

  • I’m compiling by cmd: jar cfe Gerar.jar principal principal.class principal$1.class "Gerador de arquivos.java"

  • Where did you get this command? And how is the manifest?

  • I always used this command to generate my . jar and all worked and manifest this Main-Class: principal

  • Add the complete stack of errors, because for sure it’s not just a row or error.

  • added the 2 errors when I click 2 in the file . jar it appears 2 windows each with 1 error.

Show 1 more comment

1 answer

2


this issue occurred after updating the question with the error stack, I did not remove the second part of the answer so as not to lose the context generated before the edits.

The message says that you have compiled your code using Java 9 (version 53 is Java 9), but the virtual machine installed on your machine is Java 8 (version 52), as said in the comments, the JRE version needs to be of the same version or higher than the JDK, the opposite cannot.

You need to install a java 9 JRE or recompile it using JDK version 8.


With me it worked with the command below, created according to documentation:

jar cfve Gerar.jar principal principal.class principal$1.class

Leaving him:

manifesto adicionado
adicionando: principal.class(entrada = 2091) (saÝda= 1271)(compactado 39%)
adicionando: principal$1.class(entrada = 1590) (saÝda= 840)(compactado 47%)

Files generated by the command javac principal.java and by the above command:

inserir a descrição da imagem aqui

And the program opened normally without any kind of error:

inserir a descrição da imagem aqui

  • mute nothing still continues with the same mistake

  • @Clockwork like I said, there’s no way to help you just with the information you’ve given, your problem is not reproducible. I tried to play with what gave and worked. Try to run the jar via cmd with java -jar Gerar.jar and see if it shows errors. If yes, edit the question and provide more information with this error.

  • I will post the errors they give after uninstalling td and installing dnv ta appearing on cmd errors

  • @Clockwork now yes, the error is clear, you have compiled your code in a version of jdk superior to the version of the virtual machine. Virtual machines need to be of the same version or higher than JDK, otherwise they cannot.

  • now it does not wheel of using java "Classe principal" I don’t understand, what should I do?

  • @Clockwork reread my last comment, it is the answer and solution of the problem..

  • Got it worked only I had to recompile the dnv vlw code by the help

  • @Clockwork I edited the answer, but I recommend that you review the installed verses of JDK and JRE, and make sure it’s according to the suggested edition

  • I just saw this, ta td certo jdk1.8.0_151 and jre1.8.0_151

Show 5 more comments

Browser other questions tagged

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