Java 无法调用Runtime.getRuntime().exec

Java 无法调用Runtime.getRuntime().exec,java,runtime.exec,Java,Runtime.exec,我正在运行一个xSocket服务器,所以需要启动一个chat.jar,它似乎没有调用该部分。我的代码怎么了 如果我创建一个xSocketserver.jar,exec是否能够启动任何外部jar import java.io.*; import org.xsocket.connection.*; public class xSocketServer { protected static IServer srv = null; public static void main(St

我正在运行一个xSocket服务器,所以需要启动一个chat.jar,它似乎没有调用该部分。我的代码怎么了

如果我创建一个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());
        }
    }
}

您应该读取进程的outputstream和errorstream。您的命令可能失败,您无法看到错误,因为您没有读取错误流


看看。

您应该阅读进程的outputstream和errorstream。您的命令可能失败,您无法看到错误,因为您没有读取错误流

看一看

  • srv.run();-->这个调用不会像xSocket文档所说的那样返回,所以rest代码不会执行
  • 进程p=Runtime.getRuntime().exec(“cmd java-jar D:\chat.jar-n0”)
    应使用如下参数数组调用exec:

    进程p=Runtime.getRuntime().exec(新字符串[]{“cmd”,“java-jar D:\chat.jar-n0”})

  • 它将清除要运行的程序和参数

  • srv.run();-->这个调用不会像xSocket文档所说的那样返回,所以rest代码不会执行
  • 进程p=Runtime.getRuntime().exec(“cmd java-jar D:\chat.jar-n0”)
    应使用如下参数数组调用exec:

    进程p=Runtime.getRuntime().exec(新字符串[]{“cmd”,“java-jar D:\chat.jar-n0”})

  • 它将清除要运行的程序和参数

    也许这就是你的意思

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

    也许这就是您的意思

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

    import java.io.IOException;
    
     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;
        }
    }
    
    导入java.nio.BufferUnderflowException; 导入java.nio.channels.ClosedChannelException; 导入java.util.*; 导入org.xsocket.*; 导入org.xsocket.connection.*; 公共类xSocketDataHandler实现IDataHandler、IConnectHandler、IDisconnectHandler { 私有集会话=Collections.synchronizedSet(新HashSet()); 公共布尔onData(INonBlockingConnection nbc)引发IOException、BufferUnderflowException、ClosedChannel Exception、MaxReadSizeExcepedException { 尝试 { 字符串数据=nbc.readStringByDelimiter(“\0”); //如果(data.trim().length()>0) //{ //System.out.println(“传入数据:+数据”); //nbc.write(“+A4\0”); /* if(data.equalsIgnoreCase(“”) { nbc.write(“\0”); 返回true; } */ //String[]message=data.split(“~”); 字符串消息=数据; sendMessageToAll(消息); //if(message.equalsIgnoreCase(“SHUTDOWN”)) //xSocketServer.shutdownServer(); //} } 捕获(例外情况除外) { System.out.println(“onData2:+ex.getMessage()); } 返回true; } 私有void sendMessageToAll(字符串消息) { 尝试 { 已同步(会话) { 迭代器iter=sessions.Iterator(); while(iter.hasNext()) { INonBlockingConnection nbConn=(INonBlockingConnection)iter.next(); if(nbConn.isOpen()) nbConn.write(消息+“\0”); } } //System.out.println(“传出数据:+消息”); } 捕获(例外情况除外) { System.out.println(“sendMessageToAll:+ex.getMessage()); } } //////////////////////////////////////////////////////////////////////////////////// 公共布尔onConnect(INonBlockingConnection nbc)抛出IOException、BufferUnderflowException、MaxReadSizeExceedeException { 尝试 { 已同步(会话) { 会话。添加(nbc); } //System.out.println(“onConnect”); } 捕获(例外情况除外) { System.out.println(“onConnect:+ex.getMessage()); } 返回true; } 公共布尔onDisconnect(INonBlockingConnection nbc)引发IOException { 尝试 { 已同步(会话) { 删除会话(nbc); } //System.out.println(“onDisconnect”); } 捕获(例外情况除外) { System.out.println(“onDisconnect:+ex.getMessage()); } 返回true; } }
    导入java.io.IOException;
    导入java.nio.BufferUnderflowException;
    导入java.nio.channels.ClosedChannelException;
    导入java.util.*;
    导入org.xsocket.*;
    导入org.xsocket.connection.*;
    公共类xSocketDataHandler实现IDataHandler、IConnectHandler、IDisconnectHandler
    {
    私有集会话=Collections.synchronizedSet(新HashSet());
    公共布尔onData(INonBlockingConnection nbc)引发IOException、BufferUnderflowException、ClosedChannel Exception、MaxReadSizeExcepedException
    {
    尝试
    {
    字符串数据=nbc.readStringByDelimiter(“\0”);
    //如果(data.trim().length()>0)
    //{
    //System.out.println(“传入数据:+数据”);
    //nbc.write(“+A4\0”);
    /*
    if(data.equalsIgnoreCase(“”)
    {
    nbc.write(“\0”);
    返回true;
    }
    */
    //String[]message=data.split(“~”);
    字符串消息=数据;
    sendMessageToAll(消息);
    //if(message.equalsIgnoreCase(“SHUTDOWN”))
    //xSocketServer.shutdownServer();
    //}
    }
    捕获(例外情况除外)