• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parameter Passing Styles

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone please explain to me why it would be better to pass in parameters like this:
java -Dhost=Me -Dport=123 ReservationSystem
Instead of creating your own flags in your program for specific parameters allowing the user to supply the parameters in any order like this:
Java ReservationSystem -HMe -P123
or
Java ReservationSystem -P123 -HMe
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone please explain to me why it would be better to pass in parameters like this:
java -Dhost=Me -Dport=123 ReservationSystem

I don't think this way works. At least it didn't for me. I did get to work:
java -Dmypkg.myclass.myvar=100 ReservationSystem
But just to set variables I could not. Is this working for you?
In my final submission I had any variables to be passed after the class name, not before. However I did have some variables that are used to control my background timer thread to set the frequency.
java -Dsuncertify.fbnserver.FBNServer.TimerLength=20 FBNServer
And I only specified that they could be set in the comments to the class, not in the documentation since this variable is not one of the acceptable parameters we are allowed to pass.
 
David Reck
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works for me because I have a method that loops through the args parameter of the main method searching for specific flags like -H for host. When a flag is found is stores the attached value like localhos for -Hlocalhost in a class field. So why can we not do it this way? It would allow you to create your own flags and not to us the -D java parameter over and over again.
 
Rick Fortier
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Reck:
It works for me because I have a method that loops through the args parameter of the main method searching for specific flags like -H for host. When a flag is found is stores the attached value like localhos for -Hlocalhost in a class field. So why can we not do it this way? It would allow you to create your own flags and not to us the -D java parameter over and over again.



So do the parameters before AND after the class name show up in args[] of main() ?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like all this -D stuff is not quite clear. The idea of using command line parameters of the form -Dparameter=value, is that these parameters are read by the virtual machine at startup and stored as system properties, which can later be accessed using the System.getProperty() method. THESE ARE NOT VISIBLE AS COMMAND LINE ARGUMENTS (those you see on the args[] array passed to the main method).
In regards to the choice you decide to make (whether to use system properties or arguments), I personally believe that there isn�t big difference. You are not going to get more points if you do it one way or the other.
Hope this helps!!!
Benjam�n
 
David Reck
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right about not getting any additional points either way I just want to make sure points are not deducted one of the ways I listed.
Rick,
To answer your question only the parameters listed after the class name show up in the args array.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the comments on this topic I've looked at are right but we've missed out on one important part - "Coding Standards".
The marking outline for the exam gives this 23 marks in the General Considerations section. So what we should ask is what is the "Most appropriate" way to pass parameters to a java application? Is it using the Java's inbuilt 'system properties'? or is it by passing the parameters to the main(...) function's command line?
I think this is important.
Akanimo.
 
David Reck
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akanimo,
I think you are correct. I can see good arguments for passing the parameters either way, but since you have to use the -D for the codebase and the policy file it would probably be better to us -D for all parameters just to stay consistent. Besides I reread my assignment and it specifies that "Designs should not involve building new code if the effect can adequately be achieved using facilities in the standard Java packages" and I did have to write a method to parse the args array. Which will be removed if I use -D.
Thanks for your insight.
Dave
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic