candied_orange
candied_orange

Reputation: 7344

Redirect stdin from a file in eclipse with run configuration

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.

Upvotes: 9

Views: 9343

Answers (3)

Petr Janeček
Petr Janeček

Reputation: 38444

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.

Upvotes: 7

Ahmed Ashour
Ahmed Ashour

Reputation: 5549

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

Upvotes: 7

llogiq
llogiq

Reputation: 14541

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.

Upvotes: 2

Related Questions