File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why don't i get the command prompt back when I run my java program from the terminal window? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why don Watch "why don New topic
Author

why don't i get the command prompt back when I run my java program from the terminal window?

Elsie Wilson
Greenhorn

Joined: Apr 24, 2011
Posts: 2
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! :-)
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
@Elsie Wilson:
If you want each client to run in its own process, start them with command line javaw ChatClient <username> (note that it's "javaw" and not "java").
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Come to think of it, if you're using Swing you're better off usingwhich will do pretty much the same thing, except the windows will come up in the order you started them.

Here's a very simple example that brings up 6 frames with numbers 1 to 6 in:
Elsie Wilson
Greenhorn

Joined: Apr 24, 2011
Posts: 2
Thank you for the replies. @Karthik- when I try javaw I get this error: -bash: javaw: command not found
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 475
    
    3
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> &
 
 
subject: why don't i get the command prompt back when I run my java program from the terminal window?
 
Similar Threads
No console window when running manifest file?
Java Chat Server
Java Network Question, pls help
Why does cons remain equal to null?
How a character save in 2 bytes in Java?