0

I'm trying to run a Java code using on Intelij IDEA passing a .txt file as argument.

I followed this up this discussion but it didn't help much Run Program from IntelliJ with Command Line File Input

Also, I trid the EXACT same thing (I guess..) in another computer and it worked, but in my personal computer I have a newer version of Intelij and some little things are different. I don't know if something has changed or if I'm doing something wrong...

Here is my code:

 public static void main(String[] args) {
        // read the n points from a file
        StdOut.println("Args: " + Arrays.toString(args));
        StdOut.println("Args: " + args[0]);
    }

My configurations:

run configuration

And the output:

Args: []
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at BruteCollinearPoints.main(BruteCollinearPoints.java:89)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:128)

Process finished with exit code 1

If I run this code at the console everything goes fine.

2 Answers2

1

You should provide the path to file in the Program arguments section instead of "Redirect input from" as explained in this answer https://stackoverflow.com/a/32951928/3012352

"Redirect input from" option will allow you to redirect input stream (stdin) to your program from provided file. https://www.jetbrains.com/help/idea/run-debug-configuration-application.html

CharanM
  • 31
  • 5
  • but what I want is EXACTLY to pass the content of the file as argument to my program. If I pass the arguments in the "Program arguments section" it works fine. – Pedro Almeida Jun 02 '20 at 01:58
0

Try giving the name of the file on program arguments. Put one space between each argument. Since you didn't give any argument, you are facing with this problem. Also the files must be in the same folder with your project folder.

Arif Akkas
  • 54
  • 6
  • I already had tried that. I get this output in this case: `Args: [/media/home/home/code/courses/coursera-algorithms-part1/week3/programmingAssignment3Collinear/input10.txt]` `Args: /media/home/home/code/courses/coursera-algorithms-part1/week3/programmingAssignment3Collinear/input10.txt` – Pedro Almeida Jun 02 '20 at 02:00
  • I couldn't understand exactly. I guess the problem is about the path of the file. This answer may solve your problem. https://stackoverflow.com/a/32953179/10837115 – Arif Akkas Jun 02 '20 at 02:05
  • To explain: when I pass the name of the file on program arguments, the program uses the path AS an argument. My args[0] became a string with the path to the file instead of the content of the file. – Pedro Almeida Jun 02 '20 at 02:10
  • I understand. main hold the arguments in a String array. Everything you pass will be recognized as a String. You can easily use fileoutputstream to get the contents of the passed file. You can find an appropriate example on this link. https://www.cs.swarthmore.edu/~newhall/unixhelp/Java_files.html – Arif Akkas Jun 02 '20 at 02:21