My chat program was working fine in my system when i changed the IP and compiled in my system. Then I copied the class file into another and it worked fine in that system, too. However, when the client sends a message it gives the following error :
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: Server$WorkerThread
I am using Windows 7. What can be wrong?
You missed a file named Server$WorkerThread.class. If you create one inner class in a *.java file, the compiler will generate two *.class files (one for the enclosing class and one for the inner class). The name of the inner class name will be prefixed by the enclosing class’s name and seperated by a $ character.
Edit:
Something I should add (I guess): Java loads classes lazily. This means that the jvm does not load the
Server$WorkerThread.classuntil it’s really required. The first time its code is needed is (no surprise I think), when the server wants to create an instance of aforementioned class to manage the communication with a client.You may want to have a look at e.g. Java Concurrency in Practice by Joshua Bloch, where he advocates against the usage of the Double Checked Locking Idiom for thread-safe Singletons. It’s a more elaborate example/explanation.