Inner classes are not included in jar file

I created a runnable jar file from six classes from hava:

Main: Contains the main method and is specified in the manifest (I included a new line)

Basic $ 1 and basic $ 2: 2 are anonymous inner classes that are in the main class. (Basic $ 2 in the main method, but I don’t think it really matters.)

The form

Form $ 1: anonymous inner class in form

Wraplayout

I define these inner classes when creating the jar file, but when I look into it (I'm on a mac), inner classes are not in the bank! So when I run it, I get the following:

Exception in thread "main" java.lang.NoClassDefFoundError: Main$2 at Main.main(Main.java:64) Caused by: java.lang.ClassNotFoundException: Main$2 at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 1 more 

I can’t understand what happened. Can anybody help?

EDIT: I get it! Turns out you need the escape character (\) in front of the dollar signs for the team to recognize them.

+4
source share
1 answer

You have already found your specific answer, but here is more general.

As your program changes, the set of classes with automatically generated names (for example, Main$2 ) will change. Also, if you move your classes to a named package, your jar file will need to have a parallel directory structure. You do not want to update your makefile or create a script every time this happens. Instead, you should use javac -d to specify the destination directory for the compiled class files, and then jar for this entire hierarchy.

+6
source

All Articles