9

There used to be a bug in eclipse that prevented a run configuration from redirecting a file to stdin but supposedly it's fixed. However, I can't find any documentation that tells how to do it now that it's fixed.

It's easy to misunderstand what I'm trying to accomplish so let me be clear. I'm not looking for code that will read a file. I'm not looking to pass a filename as an arg. I'm not looking to redirect stdout or stderr.

I have existing code that takes normal standard input without knowing if it's coming from the keyboard or a file:

scanner = new Scanner(System.in);
number = scanner.nextInt();

Redirecting input from a file can be done at a command prompt of course but I'm trying to get eclipse configured to do it automatically at the touch of a ctrl-F11 keystroke.

Fair warning, I've done a number of web searches trying to answer this myself and many of them claim the answer is in Run Configuration | Common but they end up only working for stdout.

candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • 1
    This is the announcement and description of the feature for eclipse 4.5: https://www.eclipse.org/mars/noteworthy/#_assigning_stdin_to_a_file – Petr Janeček Jul 08 '15 at 07:10
  • @Stanec your comment is the best answer. Might note that if you try this in 4.4 you can end up overwriting your input file with your output before you know what's going on. – candied_orange Jul 08 '15 at 08:37
  • @Stanec unfortunately, despite your linked documentation to the contrary, after downloading eclipse mars 4.5.0 M1 I've found that `Common` looks and "works" the same as in luna 4.4. : ( – candied_orange Jul 08 '15 at 09:25
  • Wait, 4.5.0 M1 is old! "M1" means "Milestone 1". The bug was fixed in M4. Are you sure you downloaded the final release version of Eclipse? – Petr Janeček Jul 08 '15 at 10:00
  • @Stanec Apparently searching for "eclipse mars download" isn't a good strategy to get the latest. Finally found a page that would show the build date here: http://download.eclipse.org/eclipse/downloads/ – candied_orange Jul 08 '15 at 10:14

3 Answers3

7

I hope I understand correctly.

The file needs to have new line at the end.

public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        System.out.println(scanner.next());
    }
}

File to contain a text (and ends with an new empty line, not only a text).

Then Run Configuration | Common | Input File

And the program will print the file content.

Edit: Build id: 20150621-1200

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
7

This is the announcement and description of the feature for Eclipse 4.5 (Mars): http://eclipse.org/mars/noteworthy/#_assigning_stdin_to_a_file

Stdin can now be assigned to a file in the Common tab of launch configuration dialogs. enter image description here

As expected, this is a new feature of the just released Eclipse 4.5 and will therefore not work in an older version.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • Since some mars 4.5 builds do not have this feature can you be more specific with the minimum version? I know M1 doesn't have it but the SDK with Build id: I20150603-2000 does. – candied_orange Jul 08 '15 at 10:20
  • Whatever is downloadable directly from https://www.eclipse.org/downloads/ (and/or the new Oopmh installer) does contain the feature. Only development builds (marked with "M" as "Milestone") will not contain the fix. – Petr Janeček Jul 08 '15 at 10:58
2

You will need Eclipse Mars for this, as the bugfix was not backported.

Go into Run Configurations, open your Java Application. In the Common tab, there is a Standard Input and Output group, where you can define an input file.

llogiq
  • 13,815
  • 8
  • 40
  • 72