Skip to main content
gamedev.net
🔒 Locked

[java] So that Java Doesn't Lock the Console

Started by Lednakashim 7 replies 2.4k views
Original Post
Lednakashim
Lednakashim
Hello,
I wrote the back-end of a socket server in java. My server is a fancy text based linux terminal.

When I run the application I can't type anything else in the console until its server is done running (which should never happen).

I need to launch two functionally independent socket servers, at once and coding them into the same application isn't a feasible solution. How can I code a command line java application that runs in the background? Maybe there is some fancy terminal command that will launch them and return me the command line?
Trying to get over the learning curve
clearz
clearz
It's very hard to understand what exactly your question is. Can you explain yourself a little clearer please.

If you are trying to run a server in your application and still be able to type into the console then you should look at threads

public class ServerConsole extends Thread{	public ServerConsole()	{		start(); // start thread		while(true)		{			// get input			// process input		}	}	public void run()	{		//server code here	}}
Lednakashim
Lednakashim
When I launch a java application with

root# java -jar myserver.jar

I can't type anything further into the Linux terminal until the program is finished running. This is a problem.
Trying to get over the learning curve
clearz
clearz
Here is a sample as simple as I can make it of what I think you are looking for. You should be able to get what you want from it.

import java.net.*;import java.io.*;import java.util.*;public class TestConsole implements Runnable{	public void run()	{		Scanner s = new Scanner(System.in);		while(true)		{			String str = s.next();			System.out.println("You Said: " + str);		}	}	public TestConsole()	{		try		{			Thread t = new Thread(this);			t.start();			ServerSocket socket = new ServerSocket(5555);			Socket s;			while(true)			{				s = socket.accept();				BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));				out.write("Hello From Server");				out.close();				s.close();			}		}		catch(Exception e){System.out.println(e);}	}	public static void main(String[] args)	{		new TestConsole();	}}
HuntsMan
HuntsMan
Quote:
Original post by Lednakashim
When I launch a java application with

root# java -jar myserver.jar

I can't type anything further into the Linux terminal until the program is finished running. This is a problem.


Execute it as:


java -jar myserver.jar&

The & at the end will make it run in the background.
aryx
aryx
Since you're on Linux, just append an ampersand to the end of the command to run it in the background:
    root# java -jar myserver.jar &
To bring it to the foreground you can use the fg [jobid] command (use the jobs command to see the ids for all the jobs).

Or you could simply open up another terminal window to run other commands.

[EDIT]
Beat to the chase!
clearz
clearz
OMG I didn't understand what the question was. I thought you where trying to accept text in your program. Ah well you got your answer now.

That's handy to know all the same about the amp at the end. I'd presume that will work in FreeBSD also. I've been running a JBoss server on my BSD box and would always leave a terminal open while it was running because of the way it blocks.
SimonForsman
SimonForsman
You might also want to take a look at the screen command.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
elFarto
elFarto
Job control (the & and the bg/fg commands) are features of the shell (e.g. Bash), rather than of any specific OS.

Putting a job in the background with a &, is usually paired with 'nohup', e.g:

root# nohup java -jar myserver.jar &

It's not required, but using nohup will prevent the process being killed if you log out. This is very useful if you start the process on a remote machine via SSH and then log out.

You can also use Control+Z to suspend a currently running process, then bg to put it in the background:

root# java -jar myserver.jar
<press control+z>
[1]+ Stopped java -jar myserver.jar
root# bg 1

Regards
elFarto

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.