我正在尝试在我的 java 应用程序 GUI 中动态运行 java 代码。我尝试了以下代码:
Sring tempfile="java -classpath "+wrkdir+"/bin "+runfile;
CommandLine cmdLine = CommandLine.parse(tempfile);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.setWatchdog(watchdog);
try {
executor.execute(cmdLine, resultHandler);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
resultHandler.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
结果是,当我的输入文件(tempfile)由打印语句组成时;那是,
public class Sample2 {
public static void main(String[] args) {
System.out.println("It Works..!!");
}
}
它能够显示结果。但是如果输入文件是这样的,
import java.io.DataInputStream;
import java.io.IOException;
import java.util.*;
public class Count
{
public static void main(String args[]) throws IOException
{
int n;
System.out.println("Enter the number: ");
DataInputStream din=new DataInputStream(System.in);
String s=din.readLine();
n=Integer.parseInt(s);
System.out.println("#"+n);
}
}
结果是 NumberFormatException。这是什么原因?在这种情况下如何通过键盘输入值?