0

Sorry to bother you guys, but I cannot find answer when did a lot of searching.

Here is my code, it's pretty simple:

import java.io.IOException;
import java.io.ObjectInputStream;

public class Test {
    public static void main(String[] args) {
        System.out.println(args.length);
        ObjectInputStream is;
        try {
            is = new ObjectInputStream(System.in);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        String input = System.console().readLine();
        System.out.println("input: " + input);

    }
}

With command like : java Test, everything works find, and I can get input from console by typing something. BUT, with redirection in the command, I cannot type on console to input, the application will return immediately.

java Test /d/aa.dat < /d/example.dat
Exception in thread "main" java.lang.NullPointerException
        at Test.main(Test.java:17) 

I think the root cause is when I use redirection in command line with a file, the default System.in is override by file "example.dat".

My question is how can I restore the default System.in to let user input from console?

Thank you so much.

alisondong
  • 141
  • 1
  • 5
  • see http://stackoverflow.com/questions/25795568/how-to-redirect-a-file-as-an-input-to-java-program-with-bash – Scary Wombat Feb 16 '17 at 06:29
  • It's simply not possible see an explanation here: https://stackoverflow.com/questions/33555283/why-is-not-possible-to-reopen-a-closed-standard-stream#33555444 – SubOptimal Feb 16 '17 at 08:11
  • It's not possible. Please refer the link for description, https://stackoverflow.com/questions/33555283/why-is-not-possible-to-reopen-a-closed-standard-stream#33555444 – drink-a-Beer Mar 13 '17 at 12:03

0 Answers0