UnsupportedClassVersionError when trying to generate a jar

-2

I have compiled my code and everything else and when I open the file .jar created it 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);
    }

}
    
asked by anonymous 23.10.2017 / 01:14

1 answer

2
  

This issue occurred after updating the question with the error stack, I did not remove the second part of the response 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 stated in the comments, the JRE version needs to be of the same or greater than the JDK, the reverse can not.

You need to install a jRE from java 9 or recompile it using jdk in version 8.

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

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

Output from it:

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:

Andtheprogramopenednormallywithoutanykindoferror:

    
23.10.2017 / 01:51