score: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

score: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.

score: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.

score: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.

score:4

< ${workspace_loc:/myproject/file}

in debug configuration/arguments/program arguments:

works well for me.

score: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.

score: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.

score: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.

score: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.

score:12

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.

score: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


Related Query

More Query from same tag