Vijay Bhoomireddy
Vijay Bhoomireddy

Reputation: 576

Passing a String array in Java command line arguments

Just wondering how to pass a String Array in the -D command line argument in Java.

Essentially I wanted to pass 3/4 local paths as the command line arguments which can be used in the program. However, I'm wondering what is the best way to handle this, instead of passing each path as a -D argument.

Thanks in advance.

Upvotes: 3

Views: 7993

Answers (3)

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16060

Using -D defines parameters for the VM, to be obtained via System.getProperty() whereas command-line parameters are usually understood to be those passed to public static void main( String[] argv ).

Having said that, you could simply pass all your paths as command line arguments and use them in main as indicated below:

java YourClass path1 path2 path3 etc.

where YourClass contains

public static void main( String[] argv ) {
    for (String path : argv ) { /* do something with this 'path' */ }
}

Cheers,

Upvotes: 6

roeygol
roeygol

Reputation: 5028

An important thing to say, that when you're using Eclipse (or any other IDE), you can send arguments using the IDE itself.

In Eclipse:

  • Right click on your main class
  • Choose: Run As -> Run Configurations... or: Debug As -> Debug Configurations...
  • Choose Arguments tab
  • Enter your arguments seperated by one blank space (same as used on command line)

This way you can check your code without creating any jars.

Upvotes: 4

Manish Kothari
Manish Kothari

Reputation: 1712

pass your arguments like this

java yourprogram arg1 arg2 arg3 ... argn

these get passed on to your main method, which you can use in your program:

public static void main(String[] args){
    //use agrs here
}

Upvotes: 3

Related Questions