• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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! :-)
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the replies. @Karthik- when I try javaw I get this error: -bash: javaw: command not found
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of your environment variable PATH?
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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> &
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic