【发布时间】:2013-12-01 07:44:21
【问题描述】:
我有一个 BufferedReader ,从另一个 Jarfile 读取输入(错误)流。我在另一个线程中运行另一个 jarfile,然后我循环读取行,然后重置标签文本。但它不会重绘。我读过 JavaFX 是单线程的。但我不知道该怎么办。代码如下:
Thread start = new Thread(){
@Override
public void run(){
try{
Process proc = Runtime.getRuntime().exec("javaw -Xmx2048M -jar craftbukkit.jar -o true --nojline");
data.setInputStream(proc.getErrorStream());
data.setOutputStream(proc.getOutputStream());
}catch(IOException ex){
ex.printStackTrace();
}
BufferedReader readr = new BufferedReader(new InputStreamReader(data.getInputStream()));
data.setServerStarted(true);
String line;
while(data.serverStarted()){
try {
if((line = readr.readLine()) != null){
data.cons.setText(data.cons.getText() + line + "\n");//cons is the label
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
readr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}};
start.start();
但是如果我尝试在同一个线程中运行这个,程序将没有响应。我正在做一些标签重写但是,它在同一个线程中,然后,在重写之后立即。我从 jar 的输入流中得到一条线。这是代码:
public void sendCommand(String command){
if(data.serverStarted()){
if (!(command.equalsIgnoreCase(""))){
if(!(command.equalsIgnoreCase("stop"))){
try {
data.cons.setText(data.cons.getText() + ">" + command + "\n");
data.bw.write(command);
data.bw.newLine();
data.bw.flush();
data.tl.setText("Type your command here...");
} catch (IOException e) {
e.printStackTrace();
}
}else{
data.cons.setText(data.cons.getText() + "This command is not allowed!" + "\n");
}
}else{
data.cons.setText(data.cons.getText() + "Please make sure that you have entered a command" + "\n");
}
}else{
data.cons.setText(data.cons.getText() + "Please start your server first!" + "\n");
}
}
如果我尝试发送命令,无论我在哪个阶段执行,如果我弄乱了 cons.setText(); ,它会给我这样的东西: http://prntscr.com/27w6yv ,而标准的 MS-DOS 启动是这样的: http://prntscr.com/27w79v 。这与我分配的内存量或 CPU 功率无关。我有 8GB 的 ram,我在 JavaFX 程序和服务器上都分配了 2 个。(无论如何它是一个 minecraft 服务器)。 CPU是i5-3570K。这很奇怪,因为这个程序是我以前用 JFrame 构建的重制版。那里一切顺利!请帮我! (出现在 GUI 上的行是第一行,不管它是什么。如果我在循环之前添加另一个 setText(),只会出现它。)
我得到这个错误:
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-6
---------------更新-------- 我现在使用 Platform.runLater();但是我得到“系统找不到指定的文件错误”
产生错误的代码是:
BufferedReader readr = new BufferedReader(new InputStreamReader(is));
这里是所有需要的代码:
try{
data.updateConsole("Starting server!");
Process proc = Runtime.getRuntime().exec("start javaw -Xmx2048M -jar craftbukkit.jar -o true --nojline");
data.setOutputStream(proc.getOutputStream());
is = proc.getErrorStream();
}catch(IOException ex){
ex.printStackTrace();
}
BufferedReader readr = new BufferedReader(new InputStreamReader(is));
我是否需要 - 在此过程中 - 包含文件的完整路径,还是只包含文件名?两个jar都在同一个目录下!
【问题讨论】:
标签: java multithreading user-interface javafx redirect