问题
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:
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.
Make the Main-Class name the fully qualified name of the class with the main method. (i.e. myproject.Test).
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