Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing string buffer to java program in IntelliJ debug/run

How does one accomplish equivalent of running following line on command line in IntelliJ or Eclipse .... :

java MyJava < SomeTextFile.txt

I've attempted to provide location of the file in Program Arguments field of Run/Debug Configuration in IntelliJ

like image 565
DblD Avatar asked Aug 18 '12 13:08

DblD


2 Answers

As @Maba said we can not use Input redirection operator (any redirection operator) in eclipse/intellij as there no shell but you can simulate the input reading from a file through stdin like the below

       InputStream stdin = null;
        try
        {
        stdin = System.in;
        //Give the file path
        FileInputStream stream = new FileInputStream("SomeTextFile.txt");
        System.setIn(stream);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
                    br.close(); 
                    stream.close()

        //Reset System instream in finally clause
        }finally{             
            System.setIn(stdin);
        }
like image 96
Dungeon Hunter Avatar answered Oct 30 '22 08:10

Dungeon Hunter


You can't do this directly in Intellij but I'm working on a plugin which allows a file to be redirected to stdin. For details see my answer to a similar question here [1] or give the plugin a try [2].

[1] Simulate input from stdin when running a program in intellij

[2] https://github.com/raymi/opcplugin

like image 35
raymi Avatar answered Oct 30 '22 08:10

raymi