Java JAR - Exception in thread “main” java.lang.NoClassDefFoundError

我怕爱的太早我们不能终老 提交于 2019-12-25 17:19:12

问题


Im working on Ubuntu. I made simple GUI app which uses 3 classes. Main is Test.class which contains main method. I have packed them all into JAR from console level. Now I have "kalkulator.jar" which contains: Test.class, MyFrame.class, MyPanel.class and META-INF folder. In META_INf there is MANIFEST.MF. What is in MANIFEST.MF is: "Main-Class: Test" and newline character. Now I try to open my JAR from console using: java -jar kalkulator.jar I get: "Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: myproject/Test") myproject is package of all my classes. So how can I solve it?


回答1:


You need to do the following things:

  1. make sure that the directory hierarchy in your jar matches the package hierarchy, absolutely. The classes have to be in a directory named 'myproject' in the jar. If they are just at top level, it won't work.

  2. Make the Main-Class name the fully qualified name of the class with the main method. (i.e. myproject.Test).

  3. Learn to use a build tool, e.g. ant, to do all this.



来源:https://stackoverflow.com/questions/14186372/java-jar-exception-in-thread-main-java-lang-noclassdeffounderror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!