0

What is the best way to parse this command line arguments in java?

$ ant run -Dfilename=(filename_value) -Dmemory_size=(memory_size) -Dk=(k_value)

I am not actually using ant but my project will be evaluated on this command line. Therefore, I am trying to test this on netbeans by adding this example arguments in run configurations.

-Dfilename="C:\\Users\\y_guz_000\\Dropbox\\active projects\\ExternalSort2\\resources\\input.xml" -Dmemory_size="3"
-Dk="2"

I am outputting the result with:

System.out.println(System.getProperty("filename"));
System.out.println(System.getProperty("memory_size"));
System.out.println(System.getProperty("k"));

However the result is

null
null
null
djechlin
  • 54,898
  • 29
  • 144
  • 264
Yunus Eren Güzel
  • 2,699
  • 9
  • 34
  • 58

2 Answers2

1

You are not passing the system variables to your java program, you are passing them to ant, so this is as expected. Googling "ant run set system properties" this looks to be the information you need: http://ideoplex.com/id/372/setting-java-system-properties-with-ant

djechlin
  • 54,898
  • 29
  • 144
  • 264
1

assuming that you have a standard Netbeans Java SE or Maven Java app project:

In Netbeans "Projects" tree on the left, right click on your project icon. Select "Run" list item for the "Categories" list, and you have textfields there to input your program and VM arguments (-D here).

linski
  • 4,896
  • 3
  • 18
  • 32
  • The VM options were not in where you defined. I found it on Run > Configuration – Yunus Eren Güzel Jan 08 '13 at 07:15
  • It depends on your Netbeans version and the type of the project. Yes it is done automatically by ant, every time you start your program it will be started with values from those textfields - it is called a "Run configuration". You can have multiple run configurations for one project (it is "Configuration" list item in the "Categories" list at my computer). – linski Jan 08 '13 at 08:41