java.lang.NoClassDefFoundError error while executing application

0

I made a program in java that uses JDateChooser so that the user chooses the date that he wants. It works normally inside Eclipse but when I export the project to a .jar and try to run through the cdm it has the following error:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/toedter/calendar/
JDateChooser
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544
)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)

Caused by: java.lang.ClassNotFoundException: com.toedter.calendar.JDateChooser
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

Class that uses JDateChooser :

import com.toedter.calendar.JDateChooser;
public class Login implements ActionListener {
    private JFrame ourFrame = new JFrame("Login");

    JTextField user_text = new JTextField();
    JPasswordField pass_text = new JPasswordField();
    static JDateChooser calendario = new JDateChooser();

    JButton yesButton = new JButton("Confirmar");
    JButton noButton = new JButton("Cancelar");

    public Login() {
        ourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ourFrame.setBounds(600, 300, 400, 250);

        Container container = ourFrame.getContentPane();
        container.setLayout(null);

        JLabel logo1 = new JLabel("LOGIN CLEARQUEST");
        logo1.setBounds(130, 10, 250, 20);

        JLabel user_label = new JLabel("Usuário:");
        user_label.setBounds(20, 35, 250, 30);

        JLabel pass_label = new JLabel("Senha:");
        pass_label.setBounds(20, 70, 250, 30);

        user_text.setBounds(70, 40, 250, 20);
        pass_text.setBounds(70, 75, 250, 20);

        JLabel logo2 = new JLabel("INSIRA A DATA DO ÚLTIMO RELATÓRIO");
        logo2.setBounds(83, 100, 250, 30);
        calendario.setBounds(83, 130, 210, 25);

        yesButton.setBounds(70, 170, 100, 30);
        yesButton.addActionListener(this);

        noButton.setBounds(210, 170, 100, 30);
        noButton.addActionListener(this);

        container.add(logo1);
        container.add(user_label);
        container.add(pass_label);
        container.add(user_text);
        container.add(pass_text);
        container.add(logo2);
        container.add(calendario);
        container.add(yesButton);
        container.add(noButton);
        ourFrame.setVisible(true);
    }

    @SuppressWarnings("deprecation")
    public void actionPerformed(ActionEvent aE) {
        if (aE.getSource() == yesButton) {
            String user = user_text.getText();
            String pass = pass_text.getText();

            RESTInvoker rest = new RESTInvoker(user, pass);
            String json = rest.getDataFromServer();

            try {
                // TODO pega os dados do json (arquivo txt retirado do clearquest) e joga em um Array de testes
                Teste[] teste = null;
                teste = Json.criarJson(teste, json);

                DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
                Date date = new Date();

                String caminhoTemplate = Variaveis.getCaminhoTemplate(); 
                String caminhoDestino = Variaveis.getCaminhoExcel() + dateFormat.format(date) + ".xls";

                // verificar o caminho
                CriacaoExcel.criandoArquivo(caminhoTemplate, caminhoDestino, teste);

            }

            catch (InvalidFormatException e) {
                CaixaDialogo.exibirErro(e.getMessage());
            }

            catch (ParseException e) {
                CaixaDialogo.exibirErro(e.getMessage());
            }

        } else if (aE.getSource() == noButton) {
            System.exit(0);
        }

    }

    // envia a data, do relaatório antigo, escolhida pelo usuario
    public static Date enviaData() {
        return calendario.getDate();
    }

    public static void main(String[] args) {
        // para setar o foco nos botões
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
                new Login();
            }
        });
    }
}

I put Jar of JCalendar as shown in the image below:

AndIcreateda"Runnable Jar" of my project, as shown in the image below:

Iranaroundthecmdwiththefollowingcommand:java-jargeradorStatus.jar-cp./lib/jcalendar-1.4.jar

Andthefollowingerrorappeared:

    
asked by anonymous 28.06.2018 / 19:30

2 answers

3

Before generating the jar in eclipse, the jcalendar lib must belong to the classpath of your project. To do this, add it by right clicking on the project and select the option as print:

Inthewindowthatopens,clickAddExternalJARsandincludethejcalendarlib.

Then,right-clickontheprojectagain,andselecttheExportoptiontocreatethejar.SelectJava->RunnableJarfile.

Inthenextscreen,selectthemainclassofyourapplicationinLaunchConfigurationandjustbelowchecktheoptionExtractrequiredintogeneratedJAR,sothatdependenciesareincludedinyourjarcorrectly:

By following these steps correctly, the jar will be run without problems with the dependencies.

    
28.06.2018 / 19:50
2

You have to run your application like this:

java -jar geradorStatus.jar -cp ./lib/jcalendar-1.4.jar
    
28.06.2018 / 19:43