简体   繁体   中英

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.

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,

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM