This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
I feel like there is some basic concept I am missing here. I have a simple chat server program I have written for a class. I don't need any help with the mechanics of the program. That is working fine. My question has to do with running the programs from the command line.
First, I launch the server from one command prompt window. The server is a console program and does not have a user interface. It spews out some messages to the console as different things happen, which is what I want at this point. So, I don't care that this window waits for the server to end before coming back to the command prompt - that makes sense to me.
After the server side is running, I launch clients one at a time from different console windows. The client has a simple swing gui interface. But when I run the client (java ChatClient <username>) I do not get a command prompt back - it waits until I quit the client user interface before giving me the command prompt back. Although it does not matter for the purposes of this class project, this is not what I want. How can I start the client side and not have the command window just sitting there waiting for me to quit the user interface?
I am coding in Eclipse on a Mac OS X 10.6 machine. The server and clients are all running on my local machine. I want to test 50 clients without having to open 50 terminal windows. Thank! :-)
I guess the reason you can't start a java program from a console and leave the JVM running would be because then you have no way of terminating the JVM... you could put it into an infinite loop and have it churning away at 100% CPU until you go to Task Manager (or Mac equivalent) and kill the process. At least as it is you can Ctrl+c out of it.
One thing you could do would be to put any code you have in your main method int a run() method. I do this all the time - in fact I have a template on NetBeans which looks like this:
Then you can easily kick off 50 instances of this class in separate threads. Just change the main method to something like
Obviously if you want each instance to behave differently, you can write a method to change that instance's state before running it, or make sure your constructor can have the appropriate arguments passed to it.
What is the value of your environment variable PATH?
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Apr 04, 2009
Sorry, now I observe javaw isn't available on other platforms - I'd no idea it's Windows only.
So instead, you can launch your chat client as background application by suffixing an "&" to the command line, like this:
java ChatClient <username> &