I'm running an xSocket server so would need to launch a chat.jar, it doesn't seem to call that portion. What wrong with my code?
If I create a xSocketserver.jar, would the exec have the ability to launch any external jar?
import java.io.*;
import org.xsocket.connection.*;
public class xSocketServer
{
protected static IServer srv = null;
public static void main(String[] args)
{
try {
srv = new Server("127.0.0.1",8090, new xSocketDataHandler());
srv.run();
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
try {
System.out.println("setup exec");
Process p = Runtime.getRuntime().exec("cmd java -jar D:\\chat.jar -n 0");
p.waitFor();
System.out.println("call exec");
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
protected static void shutdownServer() {
try {
srv.close();
}
catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
-
and what is the problem? Exception? "It does not work" is not descriptive enough.Bozho– Bozho2010年11月10日 13:47:30 +00:00Commented Nov 10, 2010 at 13:47
-
No error, did not print out any message in the Eclipse IDE's console outputProyb2– Proyb22010年11月10日 13:58:03 +00:00Commented Nov 10, 2010 at 13:58
-
What's the question exactly? I don't understand what you want to do.Buhake Sindi– Buhake Sindi2010年11月10日 14:00:58 +00:00Commented Nov 10, 2010 at 14:00
4 Answers 4
You should read the outputstream and the errorstream of the process. Your command is probably failing and you can't see the error because you are not reading the error stream.
Take a look at this article.
Comments
- srv.run(); --> this call won't return as xSocket document said, so the rest code won't execute.
Process p = Runtime.getRuntime().exec("cmd java -jar D:\chat.jar -n 0");
You should call exec with array of argument like this:Process p = Runtime.getRuntime().exec(new String[]{"cmd","java -jar D:\chat.jar -n 0"});
it'll clear program to run and arguments.
maybe this's you mean?
public class XServer implements IDataHandler {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
IServer server = new Server(8090,new XServer());
server.start();
}
@Override
public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException,
ClosedChannelException, MaxReadSizeExceededException {
String data = nbc.readStringByDelimiter("\r\n");
nbc.write(data + "\r\n");
System.out.println("setup exec");
Process p = Runtime.getRuntime().exec(new String[]{"cmd","java -jar D:\\chat.jar -n 0"});
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("call exec");
return true;
}
}
telnet to your localhost on port 8090, enter a line of text and server will exec you chat program.
1 Comment
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import java.util.*;
import org.xsocket.*;
import org.xsocket.connection.*;
public class xSocketDataHandler implements IDataHandler, IConnectHandler, IDisconnectHandler
{
private Set<INonBlockingConnection> sessions = Collections.synchronizedSet(new HashSet<INonBlockingConnection>());
public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException
{
try
{
String data = nbc.readStringByDelimiter("0円");
//if(data.trim().length() > 0)
//{
//System.out.println("Incoming data: " + data);
//nbc.write("+A40円");
/*
if(data.equalsIgnoreCase("<policy-file-request/>"))
{
nbc.write("<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"8090\"/></cross-domain-policy>0円");
return true;
}
*/
//String[] message = data.split("~");
String message = data;
sendMessageToAll(message);
//if(message.equalsIgnoreCase("SHUTDOWN"))
// xSocketServer.shutdownServer();
//}
}
catch(Exception ex)
{
System.out.println("onData2: " + ex.getMessage());
}
return true;
}
private void sendMessageToAll(String message)
{
try
{
synchronized(sessions)
{
Iterator<INonBlockingConnection> iter = sessions.iterator();
while(iter.hasNext())
{
INonBlockingConnection nbConn = (INonBlockingConnection) iter.next();
if(nbConn.isOpen())
nbConn.write(message+"0円");
}
}
//System.out.println("Outgoing data: " + message);
}
catch(Exception ex)
{
System.out.println("sendMessageToAll: " + ex.getMessage());
}
}
////////////////////////////////////////////////////////////////////////////////////
public boolean onConnect(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException
{
try
{
synchronized(sessions)
{
sessions.add(nbc);
}
//System.out.println("onConnect");
}
catch(Exception ex)
{
System.out.println("onConnect: " + ex.getMessage());
}
return true;
}
public boolean onDisconnect(INonBlockingConnection nbc) throws IOException
{
try
{
synchronized(sessions)
{
sessions.remove(nbc);
}
//System.out.println("onDisconnect");
}
catch(Exception ex)
{
System.out.println("onDisconnect: " + ex.getMessage());
}
return true;
}
}