我正在运行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());
}
}
}
答案 0 :(得分:3)
您应该阅读流程的输出流和错误流。您的命令可能失败,您无法看到错误,因为您没有读取错误流。
看看this article。
答案 1 :(得分:1)
进程p = Runtime.getRuntime()。exec(“cmd java -jar D:\ chat.jar -n 0”);
你应该用这样的参数数组调用exec:
进程p = Runtime.getRuntime()。exec(new String [] {“cmd”,“java -jar D:\ chat.jar -n 0”});
它将清除程序运行和参数。
答案 2 :(得分: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;
}
}
在端口8090上telnet到您的localhost,输入一行文本,服务器将执行聊天程序。
答案 3 :(得分: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;
}
}