3

So I've found a few questions similar to mine, but none have specifically addressed the issue I'm having.

My program will be invoked by a command line argument such as:

java Main "inputFile.txt" "inputFile2.txt"

I'm programming in Eclipse and am using the public void main(String[] args) to expect an arg[0] and arg[1]

However when I run in eclipse there is no way for me to enter which files I use. To work around this I added the 2 files I need into my project works-pace. Then in run configuration, I specified those to files as: inputFile.txt inputFile2.txt

So when I run the program now it is simply saying it cannot find inputFile.txt

Basically I'm not sure where the issue is, whether I'm entering the arguments in run configuration wrong, whether I'm placing them in my work-space incorrectly, or whether my code is incorrect.

hamid
  • 2,033
  • 4
  • 22
  • 42
Gasper Gulotta
  • 159
  • 3
  • 9
  • Have you tried specifying the full path (like "C:\User\Batman\BatDocs\BatText.txt")? – Janis F Jun 04 '13 at 15:33
  • possible duplicate of [How to pass console arguments to application in eclipse?](http://stackoverflow.com/questions/7574543/how-to-pass-console-arguments-to-application-in-eclipse) – Patrick Jun 04 '13 at 15:35
  • take a look at this question: http://stackoverflow.com/q/2850674/630384 – DHall Jun 04 '13 at 15:35

2 Answers2

7

You need to go to: Run Configuration > Argument > Program argument. Then copy and paste

 "inputFile.txt" "inputFile2.txt"

inside the box.

enter image description here

hamid
  • 2,033
  • 4
  • 22
  • 42
  • Yea I did this, yet it still cannot find it. This helps though, I can eliminate that as a possible reason why my code is not working. – Gasper Gulotta Jun 04 '13 at 15:43
  • My two text files are located in my src folder with my java files, is this the correct place? – Gasper Gulotta Jun 04 '13 at 15:44
  • 1
    There's also a place in the run configuration to set the program's working directory -- you'll want to set that to the directory that contains the two files. – Ernest Friedman-Hill Jun 04 '13 at 15:44
  • Ah got it! I moved them out of the src folder and into the project folder itself. This helped! – Gasper Gulotta Jun 04 '13 at 15:45
  • So now that this runs correctly. If I were to go to the command line and run it with these two text files as arguments it will perform the same way? – Gasper Gulotta Jun 04 '13 at 15:46
  • @GasperGulotta Or you could do: `"C:/Users/Desktop/inputFile.txt" "C:/Users/Desktop/inputFile2.txt"`. Yes, it should perform the same way. – hamid Jun 04 '13 at 15:47
  • Note that you can rarely rely on the default value of "current working directory". There are variables you can refer to to expand to e.g. project root etc. – Thorbjørn Ravn Andersen Jun 04 '13 at 19:12
0

"Run Configuration" program arguments are the place to give the arguments, And also, it just like running from a command line.
You can specify a project and then use ${project_path} to locate the file if it is in your eclipse project. Or you can use any other defined variables.

Also take a look on How to pass console arguments to application in eclipse?

Where to put a textfile I want to use in eclipse?

Community
  • 1
  • 1
awsome
  • 2,143
  • 2
  • 23
  • 41