Eclipse reading stdin (System.in) from a file

后端 未结 11 1885
轮回少年
轮回少年 2020-11-27 03:40

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

相关标签:
11条回答
  • 2020-11-27 03:44

    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.

    0 讨论(0)
  • 2020-11-27 03:46

    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.

    0 讨论(0)
  • 2020-11-27 03:49

    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.

    0 讨论(0)
  • 2020-11-27 03:51

    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.

    0 讨论(0)
  • 2020-11-27 03:52

    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.

    0 讨论(0)
  • 2020-11-27 03:57

    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

    0 讨论(0)
提交回复
热议问题