I am trying to run java code dynamically in my java application GUI. I have tried the following code:
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)由打印語句組成時;即,如何使用apache commons-exec運行java程序?
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是結果。這是什麼原因?在這種情況下,我怎樣才能通過鍵盤輸入數值?
包括例外的stracktrace是一個好主意。 – 2013-03-14 18:22:25
在我的程序中包含堆棧跟蹤的地方?對不起,我對Java不太熟悉。所以,請幫助。 – Salini 2013-03-15 01:26:38