I'm trying to pass a .txt file as a paramater into my java program. The line that reads in the file is:

Scanner in = new Scanner(System.in);

My program is titled SetTest and the file I'm trying to read is Ted.txt. If I go to a Windows 7 command prompt I create a temp folder and compile my porgram there creating SetTest.class. Also in that folder is Ted.txt. From that temp directory I then issue the command:

java SetTest < Ted.txt

Everything works great.

My question is how do I duplicate this using Eclipse? Where does the file (Ted.txt) need to be located and how do I pass it into my program?

Scanner in = new Scanner(System.in);

this line doesn't read a file. what exactly are you saying?
just pass the right url to your application, and it'll work like a charm.

That looks like you're piping the file to the std input stream. I don't know if that's possible when executing within Eclipse.

Far better to create your Scanner using the file directly, not via System.in.

You can pass the file name into your java program as a parameter when you invoke it - that's what the String[] args parameters on the main method are for.

Sorry, I'm pretty new at this and can't follow you. As I said, when running the program from a command prompt:

java SetTest < Ted.txt

Everything works perfect. I run into trouble when I try and do this from Eclipse. I added Ted.txt to my project in Eclipse. I see it listed under JRE System Library for this package.

Next I try Run -> Run Configurations
I go to the (x)= Arguments tab Ted.txt in the Program arguments section then click on Run. Unfortunately nothing happens.

< is the input redirection operator in the shell. When you write java SetTest < Ted.txt, you're executing the command java SetTest without passing any arguments to SetTest and feeding Ted.txt into the program's standard input stream. This is not possible to do in Eclipse.

If you want to read from a file, you should pass a File object as the argument to Scanner's constructor rather than System.in. That's what James meant by "create your Scanner using the file directly".

seep2k, thanks for the reply. I've searched the web for a solution here and it appears you are 100% correct, what you can do from a command prompt "java SetTest < Ted.txt" you can't do from Eclipse. Thanks for the help.

For the record I can change my code as you said to use the line: Scanner in = new Scanner(new File("Ted.txt")); and everything works fine in Eclipse. However, going back to my initial question, I'm learning to use Eclipse at the same time I'm learning Java and I wanted to see if I could use Eclipse to do this. Now I know I can't. Thanks again for this; I just wanted to understand what was possible. Please consider this matter closed.

Instead of hardcoding "Ted.txt" like that you can also use a command line argument as James suggested (which will be accessible through the array that's passed as an argument to main). Command line arguments can be passed through Eclipse (in the run configuration there's a text field for them) or through the command line (by writing java SetTest Ted.txt). So you don't have to change your code and recompile any time you want to use a different file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.