I can not start my program from the command line

I have a main method that instantiates a boolean class

 public static void main(String[] args) {
        try {
            Logic logic = new Logic(args[0]);
            ....... do some stuff here

        } catch (Exception e) {
            System.out.println("Error Encountered Details: " + e);
        }
    }

The fact is that the program needs to run the csv file, I placed it in the same directory as the .jar file, but when I run from the command line, I just get java.lang.arrayindexoutofbounds (0) error

what am I doing wrong

thank

+1
source share
6 answers

Your command line should look like this:

java -cp {name_of_jar} {name_of_class} {name_of_csv}

It looks like you are not supplying the .csv file name (what will be in args[0])?

The above assumes that the main class is not defined in your .jar manifest. if so, use:

java -jar {name_of_jar} {name_of_csv}

args[] , btw , " .csv" , /.

+7

. new Logic(argv[0]) , , :

java -jar ... somearg
+1

, :

java -jar MyAppJar myFile.csv

. , .

+1

args [0] , , . :

java -jar myjar.jar c:\myfile.csv

args [0] c:\myfile.csv, .

+1

, args - . , args , ., , ...

+1

-

$ java -jar yourApp.jar csvFileName.csv

, JAR,

$ java com.yourco.YourMainClass csvFileName.csv

, ArrayIndexOutOfBoundsException, , args, , ( ). , - .

, , , :

public static void main(String[] args)
{
   // Check mandatory argument(s) passed in
   if (args.length < 1)
   {
      System.err.println("Must supply filename of CSV input as first argument!");
      System.exit(1);
   }

   // rest of method, now you know you args[0] exists...

}
+1

Source: https://habr.com/ru/post/1729638/


All Articles