4

I have instruction to run program in command line, for example:

java SetTest < alice30.txt

I wonder how to do this in Eclipse. I tried to put this in Run Configuration like this:

enter image description here

Another thing I don't know is where to put this file (alice30.txt). Is this in root of project or in src folder where source files are located?

I know these are beginner questions but I am stuck and need help.

EDIT: As @Kane suggested I passed File and opened stream. Instead of:

Scanner in = new Scanner(System.in);

I now use:

Scanner in = new Scanner(new File("alice30.txt"));
Иван Бишевац
  • 13,811
  • 21
  • 66
  • 93
  • Already discussed before at leaast 2 times: stackoverflow.com/questions/188547/… and stackoverflow.com/questions/799250/i-o-redirection-in-eclipse – soulcheck Nov 12 '11 at 22:55
  • Making URLs clickable: [ http://stackoverflow.com/questions/188547/eclipse-reading-stdin-system-in-from-a-file ] [ http://stackoverflow.com/questions/799250/i-o-redirection-in-eclipse ] – realPK Mar 19 '14 at 04:54

4 Answers4

3

You can pass full file path in arguments (e.g. c:/.../alice30.txt))

Sergey Gazaryan
  • 1,013
  • 1
  • 9
  • 25
  • That's ok but I wonder where to put file to become accessible when I forward it by the relative path, for example to type just alice30.txt? – Иван Бишевац Nov 12 '11 at 22:42
  • 1
    Full file paths are pretty bad style anyway. It means you have to have the exact same folder structure on another computer, and moving files around is a pain. Relative paths are the way to go :) – Kane Nov 12 '11 at 22:50
2

The eclipse root directory is the base directory of the project (i.e., not the src/ directory, directly under the project.)

It's generally good style to have a 'resources' folder for txt, graphics, etc.

Rather than trying to pass a stream you could just pass the filename and open the stream yourself.

The reason what you're doing in Eclipse isn't working is because your command prompt/shell/dos/bash/whatever is handling creating the input stream out of the file for you. Eclipse doesn't do this. So, from the command line: < alice.txt means "run this program with no arguments, and create a stream to system.in", while doing that in Eclipse means "run this program with two arguments '<' and 'alice.txt'

Kane
  • 4,047
  • 2
  • 24
  • 33
1

you need do like this: add: import java.io.IOException; import java.nio.file.Paths;

then: replace"Scanner in = new Scanner(System.in);"to"Scanner in =new Scanner(Paths.get("alice30.txt"));" . and you also need do this : "public static void main(String args[]) throws IOException "

ting
  • 11
  • 1
1

With information from this link/page and several tries, I figure out a way to pass argument and file using the local route in eclipse Run -> Run Configurations.. , though it is not recommended as Kane said.

For my case: I need to do " $java someClass tinyW.txt < tinyT.txt " (This is an example from Algorithms book by Robert Sedgewick)

In my case, " tinyW.txt " is a argument, so in the eclipse environment, you can set in Run -> Run Configurations -> Arguments -> Program arguments: /local address/tinyW.txt. For my Ubuntu: /home/****/tinyW.txt enter image description here " < tinyT.txt " is a file that pipe to the main arguments, so you can set the route and file in " Run -> RUn Configurations -> Common ", click the "Input File", use the File System icon and select the file from local compute. See the figure. So in Input File: /local_address/tinyT.txt. My case is: /home/***/tinyT.txt. Hope it also works for you. enter image description here

June7
  • 19,874
  • 8
  • 24
  • 34
tairen
  • 379
  • 3
  • 9