This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help Networking book example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help Networking book example" Watch "Help Networking book example" New topic
Author

Help Networking book example

Al Stumpel
Greenhorn

Joined: Mar 25, 2006
Posts: 12
I am using teach yourself java by O'Neil and am going through some of their examples. The current problem I have is a socket example. It is compiling correctly, but the program is not running for me at all in netbeans 4.1 or in the command prompt. Any help is greatly appreciated.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It looks OK. You'd run the ServerSocketDemo, which will appear to do nothing, and then just leave it running. Now run the SocketDemo. It will contact the ServerSocketDemo, obtain a single random number, print it, and exit. When you run each program you need to supply a number as an argument; you must use the same number for each. "6543" is a personal favorite for this sort of program.


[Jess in Action][AskingGoodQuestions]
Al Stumpel
Greenhorn

Joined: Mar 25, 2006
Posts: 12
When I try to run it I get the error message exception in thread "main" java.lang.NoClassDefFoundError: ServerSocketDemo
I get the same message for the Socket Demo.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

1) From the command prompt, use "cd" to make the current directory be the location of the ServerSocketDemo.class file.

2) Type

java -cp . ServerSocketDemo 6543

3) Open a new command window, "cd" to the location of the SocketDemo.class file

4) Type

java -cp . SocketDemo 6543

It should print a number, then quit.

Why are you having a problem? The short answer is that you've got the CLASSPATH environment variable set, and it doesn't include a "." for the current directory. See this document for more information; read the whole thing, including the linked document and the caveats about not relying on CLASSPATH too much!
[ May 22, 2006: Message edited by: Ernest Friedman-Hill ]
Al Stumpel
Greenhorn

Joined: Mar 25, 2006
Posts: 12
Thank you
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help Networking book example