0

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());
 }
 }
}
Andrew Thompson
169k42 gold badges224 silver badges441 bronze badges
asked Nov 10, 2010 at 13:39
3
  • and what is the problem? Exception? "It does not work" is not descriptive enough. Commented Nov 10, 2010 at 13:47
  • No error, did not print out any message in the Eclipse IDE's console output Commented Nov 10, 2010 at 13:58
  • What's the question exactly? I don't understand what you want to do. Commented Nov 10, 2010 at 14:00

4 Answers 4

3

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.

answered Nov 10, 2010 at 14:04
Sign up to request clarification or add additional context in comments.

Comments

2
  1. srv.run(); --> this call won't return as xSocket document said, so the rest code won't execute.
  2. 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.

answered Nov 10, 2010 at 14:07

2 Comments

Thank, so it better than I move this into xSocketDataHandler() to run this code?
you can call srv.run() from a new Thread to avoid current thread be blocking.
0

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.

answered Nov 10, 2010 at 14:40

1 Comment

Hi, I have paste my handler code, maybe you can help me on that?
0
 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;
 }
}
answered Nov 10, 2010 at 15:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.