Pedro Almeida
Pedro Almeida

Reputation: 13

Run java code passing file as input argument on Jetbrain's Intelij IDEA

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.

Upvotes: 0

Views: 450

Answers (2)

CharanM
CharanM

Reputation: 31

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

Upvotes: 1

Arif Akkas
Arif Akkas

Reputation: 54

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.

Upvotes: 0

Related Questions