5

I hava made a runnable jar file out of six classes:

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

Main$1 and Main$2: 2 anonymous inner classes that are in the main class. (Main$2 is in the main method, but I don't think that really matters.)

Form

Form$1: An anonymous inner class in Form

WrapLayout

I specify these inner classes when making the jar file, but when I look inside it (I am on mac) the inner classes are not in the jar! So when I run it, I get this:

 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 figure out what's wrong. Can somebody please help?

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

user1605892
  • 127
  • 2
  • 8
  • 1
    *"Turns out, you need an escape character (\) in front of the dollar signs for the command to recognize them."* - Don't blame the command. This is basic **shell** stuff. – Stephen C Aug 19 '12 at 01:27
  • 1
    I reccommend you post your edit as an answer. This is a very good time to do that (see http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer-before-asking). – gobernador Aug 19 '12 at 01:28
  • Ironically, you also need an escape character in front of the backslash when making a post! – user1605892 Aug 19 '12 at 06:22

2 Answers2

7

You already found your specific answer, but here's a more general one.

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

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
0

Whilst creating or updating the jar, you can wrap inner class/ anonymous class names in single quotes to avoid the shell interpretting the $ in their names.