2010-11-10 79 views
0

我正在运行一个xSocket服务器,因此需要启动一个chat.jar,它似乎没有调用该部分。我的代码有什么问题?无法调用Runtime.getRuntime()。exec

如果我创建了一个xSocketserver.jar,那么exec是否可以启动任何外部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()); 
     } 
    } 
} 
+0

问题是什么?例外? “不起作用”的描述不够。 – Bozho 2010-11-10 13:47:30

+0

没有错误,没有在Eclipse IDE的控制台输出中输出任何消息 – Proyb2 2010-11-10 13:58:03

+0

究竟是什么问题?我不明白你想做什么。 – 2010-11-10 14:00:58

回答

3

您应该阅读过程的输出流和errorstream。您的命令可能失败,并且您不能看到错误,因为您没有阅读错误流。

看看this article

+0

当然,会补充说。 – Proyb2 2010-11-10 14:16:02

+0

对于着名的文章+1 :-) – 2010-11-10 14:17:25

1
  1. srv.run(); - >这个调用不会像xSocket文件那样返回,所以其余代码不会执行。
  2. 进程p = Runtime.getRuntime()。exec(“cmd java -jar D:\ chat.jar -n 0”);
    你应该调用exec带有参数的阵列是这样的:

    方法P =调用Runtime.getRuntime()EXEC(新的String [] { “CMD”,“Java的罐子d:\ chat.jar -n 0 “});

它会清除程序运行和参数。

+0

谢谢,所以它比我移动到xSocketDataHandler()运行此代码更好吗? – Proyb2 2010-11-10 14:15:29

+0

您可以从新线程调用srv.run()以避免当前线程被阻塞。 – secmask 2010-11-10 14:21:33

+0

对不起,我是Java的初学者,我如何从新线程调用? – Proyb2 2010-11-10 14:25:12

0

也许这是你的意思吗?

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到您的本地端口8090上,输入一行文本和服务器将执行您的聊天程序。

+0

嗨,我粘贴我的处理程序代码,也许你可以帮助我呢? – Proyb2 2010-11-10 15:02:21

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("+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; 
    } 
} 
相关问题