Passing string buffer to java program in IntelliJ debug/run
Asked Answered
M

3

13

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

Mudpack answered 18/8, 2012 at 13:3 Comment(1)
I think that has more to do with what shell you are using when issuing the command. When running from IntelliJ/Eclipse I guess that no shell is being used at all.Hourly
A
6

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);
        }
Anesthesiology answered 18/8, 2012 at 14:47 Comment(0)
I
2

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

Impartial answered 22/2, 2014 at 22:32 Comment(0)
S
0

You can use BufferedReader for this purpose to read from system input:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}
Sweeps answered 18/8, 2012 at 13:22 Comment(1)
I'm using BufferedInputStream in MyJava application. Question is how to pass the number of arguments to this program trough the IntelliJ or Eclipse in similar manner as one would do that from the bash command line as illustrated above.Mudpack

© 2022 - 2024 — McMap. All rights reserved.