java.lang.Noclassdeffounderror running application

Asked

Viewed 1,676 times

0

I made a java program that uses JDateChooser for the user to choose the date they want. It works normally within Eclipse but when I export the project to a .jar and try to run by cdm it shows 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 using the 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 the Jar of JCalendar as shown in the image below:

inserir a descrição da imagem aqui

And I created a "Runnable Jar" of my project, as shown in the image below:

inserir a descrição da imagem aqui

I drove by cmd with the following command: java -jar geradorStatus.jar -cp ./lib/jcalendar-1.4.jar

And the following error appeared:

inserir a descrição da imagem aqui

  • How is your classpath? What command are you using to run the application? Where is the jcalendar JAR and what version of it?

  • Possible duplicate of How to avoid Noclassdeffounderror?

  • I have this jar of Jcalendar and here it worked normally, you did not include their jar as dependency of your application on the classpath, this is the cause of your error.

  • I just added it to Referenced Libraries. His version is 1.3.3 but I tested with 1.4 and also gave the same error.

  • That’s not where you have to add it, it’s the classpath. And when generating the jar, you have to tell him to consider the dependencies too

  • What is the command line you use in cmd?

  • @Victorstafusa is by eclipse.

  • @Articuno as I put it by the classpath?

  • @Articuno Of the question: "It works normally inside Eclipse but when I export the project to a . jar and try to run by cdm it shows the following error:" - I mean, the eclipse works, and the cmd doesn’t.

  • @Victorstafusa I use the java -jar geradorStatus.jar

  • @Victorstafusa as I understand it, this "by cmd" means when she tries to rotate the jar exported by the eclipse. If you do not correctly configure the IDE to include dependencies, it goes from the problem even if the command line is correct.

Show 6 more comments

2 answers

3


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

inserir a descrição da imagem aqui

In the window that opens, click Add External JARs and include jcalendar lib.

Then, click again with the right button on the project, and select the option Export to create the jar. Select Java->Runnable Jar file.

On the next screen, select the main class of your application in Launch Configuration and just below check the option Extract required into generated JAR, so that dependencies are included in your jar correctly:

inserir a descrição da imagem aqui

By following these steps correctly, the jar will run smoothly with the dependencies.

  • so that’s exactly what I did, and it still won’t go

  • @Mariteixeira I did exactly these steps using your same class and it worked, Oce must be doing something wrong. Redo the whole answer procedure that will work.

  • made again and continues with the same error, I do not know if it is something configured on my machine

  • @Mariteixeira do me a favor, follow the steps of the first image of my reply and send a print of the window that open, please.

  • the prints are in my asked, edited and put my step by step there

  • @Mariteixeira if you compile the jar as I explained in the reply, you should run only java -jar geradorStatus.jar .Try to recompile and run again this way.

  • made the same mistake

  • AH, I did it without it! Thank you

Show 3 more comments

2

You have to run your application like this:

java -jar geradorStatus.jar -cp ./lib/jcalendar-1.4.jar
  • went around and made the same mistake

  • @Mariteixeira In which folder is jcalendar-1.4. jar Loclized? Same as generatorStatus.jar?

  • no, it is in a "lib" folder within the project

  • I just put it into the same page, and it wasn’t too

  • @Mariteixeira Reply edited. But the Articuno should be better.

Browser other questions tagged

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