Eclipse reading stdin (System.in) from a file
Asked Answered
G

11

68

Is it possible for Eclipse to read stdin from a file?

Graber answered 9/10, 2008 at 18:25 Comment(2)
Still not in 3.5.2 for input. Is this raised as an eclipse bug?Prosaism
@ThorbjørnRavnAndersen bugs.eclipse.org/bugs/show_bug.cgi?id=155411Grouse
M
59

Pure Java

You can redirect System.in with a single line of code:

System.setIn(new FileInputStream(filename));

See System.setIn().

Eclipse config

In Eclipse 4.5 or later, the launch configuration dialog can set System.in to read from a file. See the announcement here.

Common tab of Launch Configuration dialog

Mandy answered 9/10, 2008 at 18:49 Comment(0)
L
12

[Update] As it has been pointed out in the comments, this answer was misleading so I have updated it to correct the errors. [/Update]

On bash or command prompt you can do: C:\myprogramm < file.txt (Windows) or ./myprogramm < file.txt (Linux)

Unfortunately in Eclipse it is not possible to achieve the same result, since there is no option to bind the stdin to a file in eclipse. Instead you will have to manually open a file stream in your code, and then read from it instead. One way to do this is by using a program argument to enable it and another one with the file parameter. See Scott's answer on what kind of code you need to add to parse the -d option from the program arguments array.

When you want to point to some file inside your Eclipse Project, you need to be careful to get the location right and use the ${resource_loc:} variable:

-d ${resource_loc:/MyProject/file}

of course you can also put an absolute path:

-d path/to/file or -d C:\path\to\file

The resource_loc parameter refers to your workspace. That is the folder where all you eclipse projects are stored in. From there you still have to reference your project folder and then the file that you want to load.

You may have to tweak the slash direction, depending if you use Linux or Winodws.

Limnetic answered 14/5, 2011 at 19:44 Comment(3)
the '-d' option in Run configuration -> Arguments doesn't seem to work. At least not in Eclipse 3.7.1Mord
You will also need to add some kind of logic in your program to deal with these arguments or else this alone won't work.Desta
This answer has nothing to do with std.in. It teaches you to open files instead of reading std.in. Why does wrong and misleading answer recieve so many upvotes?Assonance
E
11

2015 update

There is a new Eclipse version, Eclipse 4.5 (Mars) which has fixed the issue!

This is the announcement and description of the feature: 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.

Eutectoid answered 8/7, 2015 at 8:50 Comment(2)
This is the right answer - provided that you live in 2015 :)Etymology
Correct answer as of 2016 too. Eclipse Neon 4.6 provides ease of reading an input file (.in) exactly the same as reading user input from console. Hence using Scanner sc = new Scanner(System.in); with the configured settings works flawlessly. To be exact, you can find the above screen in Run > Run Configurations > Common tab.Remus
A
8

On the "common" tab of the run dialog, under "Standard Input and Output" there's a checkbox for "file". but it appears to be only for output...

I'd expect to see 2 file fields there, one for standard in, one for standard out with the append options.

Angelaangele answered 9/10, 2008 at 19:39 Comment(1)
This only worked for output for putting the file name in Standard Input and OutputAllegedly
D
6

You will need to tweak your code some to get this working within eclipse. The other answers above did not work when I tried. Another I've seen saying to change Run..>Common tab>Standard Input and Output>File only changed the the stdout behavior to the file, not the input.

The solution was to take the "-d" option to a workable solution. If you put anything in the program arguments it's just going to be passed into your main. So what you can do at the beginning of main is something like this:

    Scanner in;
    if (args!=null && args.length>0 && args[0].equals("-d")){
        in = new Scanner(new File(args[1]));
    } else {
        in = new Scanner(System.in);
    }

You will still need to set your program arguments in the Run.. menu as described in this answer.

Desta answered 8/6, 2012 at 23:26 Comment(1)
Assuming your code is in the public static void main(String [] args) method then you don't need to check args for null - it will contain an empty array as a minimum.Tomkins
F
5

The solution for me (running on a Mac, using Eclipse CDT for a C application) was to add "< path/to/somefile" at the end of other arguments in the "Arguments" tab of the "Run Configuration" dialog.

Also check this other question for an answer involving launching a Java program in a suspended state and then attaching the debugger using Eclipse.

Fluke answered 18/5, 2011 at 8:37 Comment(2)
This also works on Linux with CDT 7.0. Probably works on older versions too.Monster
This does not work on Linux with Eclipse Java EE Indigo SR 1 :(Qualify
C
4

I don't see a nice way to do it using the standard Eclipse Run dialog. However, you might be able to do it with an External tool launcher and either use the standard "< infile" syntax or call a .bat/.sh script to do it for you.

It's not automated, which is what I'm guessing you want, but you can copy & paste the contents of your infile to the Eclipse console when you launch your program.

Courses answered 9/10, 2008 at 19:17 Comment(0)
J
4
< ${workspace_loc:/MyProject/file}

in debug configuration/arguments/program arguments:

works well for me.

Janiecejanifer answered 3/6, 2012 at 17:52 Comment(4)
An intriguing possibility. I'm curious as to what version of Eclipse you're using, and on which system. I've tried this with Eclipse Juno on Mac OS X 10.8, and the redirection operator < is interpreted literally as a command-line argument.Overtone
Eclipse IDE for Java Developers, Version: Juno Release, Build id: 20120614-1722 running on Mac OS X 10.7.5 here, and this does NOT work for me. :(Snooze
doesn't works also for me with Juno Service Release 2 in windows 7 x64Elayneelazaro
I use the Eclipse that comes with ADT on Windows 7 x64.Janiecejanifer
B
2

For linux: I think, the easiest way is to write an sh-script.

1) write run.sh like this:

your program with params < input.txt

2) In the Eclipse profile configuration specify

[tab Main] C++ Run Application = /bin/sh

[tab Arguments] Program Arguments = run.sh

That's it. When you will run your project, you will see the script's output in the eclipse console. You can do something similar in Windows.

Blas answered 4/11, 2013 at 12:45 Comment(0)
F
1

This is surprisingly not supported in Eclipse and it seems there are no current plans to add it. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=155411 to follow and vote.

I will update here if it they do implement it.

Faze answered 9/3, 2013 at 21:24 Comment(2)
I don't know how to contribute to Eclipse, but this bug is so annoying that I would study the code enough to add a standard output specification box myself. The only problem is that I need to use it for code that's due tomorrow, so I don't have time. Still, it's on my agenda for my next serious coding project – unless someone else gets to it first. (Consider this a challenge to other developers to beat me to the punch.)Coir
@Faze i guess the bug is verified and fixed and the feature is planned to be added in Version: Mars (4.5) Build id: I20141208-2000. Am i right?Fantom
P
0

What I did was to create an Ant target and launch it as "Run External" from Eclipse, here are the steps:

  • I have one input file to read from: res\in.txt and one for the output: res\out.txt
  • Create a build.xml with the targets you require (this is just an example):

    <project basedir="." default="run" name="Tests">
    <target name="clean">
    <delete dir="bin"/>
    </target>
    
    <target name="compile">
    <mkdir dir="bin"/>
    <javac srcdir="src" destdir="bin" includeantruntime="false"/>
    </target>
    
    <target name="build" depends="clean,compile"/>
    
    <target name="run" depends="build">
    <java classname="Main" input="res\in.txt" output="res\out.txt" classpath="bin" />
    </target>
    </project>
    
  • In Eclipse go to: Run->External Tools->External Tools Configurations->Ant Build-> New Launch Configuration use the following configuration:

Section Main

Buildfile: ${workspace_loc:/Tests/build.xml}

Base Directory: ${workspace_loc:/Tests}

*Note: Tests is the name of my Eclipse project

Now you can run your project by clicking in the Run Extenal tool bar button and change the input or output files as you needed

Primogenitor answered 25/3, 2013 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.