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:
And I created a "Runnable Jar" of my project, as shown in the image below:
I drove by cmd with the following command: java -jar geradorStatus.jar -cp ./lib/jcalendar-1.4.jar
And the following error appeared:
How is your classpath? What command are you using to run the application? Where is the jcalendar JAR and what version of it?
– Victor Stafusa
Possible duplicate of How to avoid Noclassdeffounderror?
– user28595
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.
– user28595
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.– Mari Teixeira
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
– user28595
What is the command line you use in cmd?
– Victor Stafusa
@Victorstafusa is by eclipse.
– user28595
@Articuno as I put it by the classpath?
– Mari Teixeira
@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.
– Victor Stafusa
@Victorstafusa I use the
java -jar geradorStatus.jar
– Mari Teixeira
@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.
– user28595