Reputation: 993
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());
}
}
}
Upvotes: 0
Views: 1321
Reputation: 993
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("+A4\0");
/*
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;
}
}
Upvotes: 0
Reputation: 8127
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.
Upvotes: 0
Reputation: 8127
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.
Upvotes: 2
Reputation: 274838
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.
Upvotes: 3