【发布时间】:2011-04-08 08:16:34
【问题描述】:
我有一个 Java 应用程序,用户可以在其中提供任何可执行文件 (.exe),该应用程序将在系统上运行它。如 cmd.exe、notepad.exe 或 unix a.out 等。
现在我在经历了许多示例后编写的代码似乎不适用于用户创建的文件 notepad.exe 工作正常,但使用 TC++ 编写的文件都不起作用。谁能指出这里错误的原因是什么?
import java.io.*;
class NewThread implements Runnable{
Thread t;
NewThread(){
t = new Thread(this, "Demo Thread");
System.out.println("child thread:" + t);
t.start();
}
public void run(){
try
{
String line;
Process p = Runtime.getRuntime().exec("C:\\TC\\BIN\\AA.EXE");
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
InputStream err = p.getErrorStream();
BufferedReader br= new BufferedReader(new InputStreamReader(in));
System.out.println("Chid running");
while((line=br.readLine())!=null){
System.out.println(line);
}
//p.destroy();
}
catch (Exception e)
{
System.out.println("ERROR");
}
System.out.println("Child thread exiting");
}
}
class ThreadDemo {
public static void main (String args[]){
new NewThread();
try {
for(int i=05;i>0;i--){
System.out.println("Main Thread:" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e){
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting");
}
}
....好的...不起作用意味着当我使用 Eclipse 运行它时-> 子线程退出 [所有 system.out 消息都打印在控制台上,但不是由 .exe AA 打印的消息。 exe根本不运行。其他几点:
- 它运行正常,没有抛出异常,唯一的问题是输出 AA.exe 在任何地方都不可见。
- 它打印进程 p 的退出代码 7...任何线索????
- notepad.exe 或 MSWord.exe 甚至 TC.exe 运行正常 通过此代码调用时。
这是 AA.exe 的代码:
#include <stdlib.h>
#include <stdio.h>
#include <values.h>
#include <time.h>
int main(void)
{
int i,j;
for(j=0;j<150;j++)
{
// randomize();
for(i=0;i<200;i++)
printf("%d\n", rand() % MAXINT);
}
return 0;
}
【问题讨论】:
-
首先:定义“不起作用”:如何不起作用。第二:至少使用
e.printStackTrace()打印catch-block 中的堆栈跟踪。第三:阅读WhenRuntime.exec()won't。 -
你得到什么输出?您必须阅读输出以防止它阻塞并可能试图告诉您您忽略的错误消息。另外,不要忽略异常最好将它们打印出来。
-
@Joachim @Peter:点注意到异常..:) 我已经编辑了问题
标签: java development-environment exe