2010-11-10 81 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; 
    } 
} 
相關問題