0

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();
            }

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);
     } 
  }

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

2
  • including the stracktrace of the exception is a good idea. Commented Mar 14, 2013 at 18:22
  • Where to include this stacktrace in my program? Sorry, I am not much familiar with java. So, please help. Commented Mar 15, 2013 at 1:26

2 Answers 2

0

Did you consider that your parseInt in the input file may actually be throwing a legitimate parse exception?

Try changing the following line

n=Integer.parseInt(s);

to

try {
 n=Integer.parseInt(s);
} catch(NumberFormatException e) {
  System.out.println("Unable to parse string into Integer. String: " + s);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but the the same exception occurs.
0

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);

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

2 Comments

An error message is the result, "the constructor DefaultExecutor(ExecuteStreamHandler) is undefined". What will I do now?
Would you please mention that where to attach this code snippet in my code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.