如何使用apache commons-exec运行Java程序? [英] How to run a java program using apache commons-exec?

查看:252
本文介绍了如何使用apache commons-exec运行Java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Java应用程序GUI中动态运行Java代码.我尝试了以下代码:

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)由打印语句组成时;即

The result is, when my input file (tempfile) consisting of printing statements; that is,

public class Sample2 {  
    public static void main(String[] args) {                               
                System.out.println("It Works..!!");  
    }  
}

它可以显示结果.但是如果输入文件是这样的,

it is able to display the results. But if the input file is something like,

   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是结果.这是什么原因呢?在这种情况下,如何通过键盘输入值?

a NumberFormatException is the result. What is the reason for this? How can I input values through keyboard in this case?

推荐答案

DefaultExector()构造函数没有附加到标准输入,因此您没有得到任何输入,因此没有要解析的内容.要附加到标准输入,请创建一个ExecuteStreamHandler并将其添加到您的DefaultExecutor中,如下所示:

The DefaultExector() constructor doesn't attach to standard input, so you are not getting any input, and there is nothing to parse. To attach to standard input, create an ExecuteStreamHandler and add it to your DefaultExecutor like this:

ExecuteStreamHandler streamHandler = new PumpStreamHandler(System.out, System.err, System.in);
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(streamHandler);

如果要从其他地方而不是System.in读取输入,请将合适的InputStream对象传递给PumpStreamHandler构造函数.

If you want to read your input from somewhere else instead of System.in, pass a suitable InputStream object to the PumpStreamHandler constructor.

这篇关于如何使用apache commons-exec运行Java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆