Command line arguments passed to main: how do I access them from other classes? ...

Command line arguments passed to main: how do I access them from other classes? ...

Post by donkeybo » Fri, 25 May 2007 18:53:14


Hi all,

I'm trying to *adapt* a simulation I found, by passing command line
arguments to the simulation to change the behaviour of the simulation,
instead of manually changing the code by hand each time I want to
change the simulation parameters.

The simulation has 4 classes involved from what I can tell: the main
class, which calls a simulation setup class (which sets up the 3d
environment), that calls a mammal behaviour class, that in turn
extends an animal behaviour class. (I appreciate extends is no longer
used -- old simulation obviously!) Now I need to get the arguments out
of the main class and into either the mammal or animal behvaour class
-- any suggestions?

If I pass the arguments, they need to go through the simulation setup,
and in turn into the behaviour class, which seems messy. Can I set the
arguments I'm interested in as constants in main (do global variables
exist in Java?) and access them from the behaviour class?

Or can I create some kind of a getArgumentValue method in main, which
the later classes can access? And if so, how do you reference the
object created through the main method?

Hope this helps -- let me know if more info. is req'd!! Needless to
say, I'm stumped, so any help you can provide would be of *great*
help!

Cheers,

Ron
 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by Ed » Sat, 26 May 2007 07:02:12


Firstly, if you're unwilling to pass the parameters out via method
calls from main(), then one way to achieve your aim is to save the
parameters to a field variable in the main(), so that they can be
accessed later; something like:

public class Test {
public static String[] args = null;

public static void main(String[] args) {
this.args = args;
}
}

Then any other class can access them via:
String[] args = Main.args;

But this is walking all over encapsulation in size tens.

Slightly better is having the arguments saved privately in Main and
accessed via accessor the method:
public class Test {
private String[] args = null;

public static void main(String[] args) {
this.args = args;
}

public static String[] getArgs() {
return args;
}
}

Next, you could, instead, export the args from the main() method into
a dedicated singleton called, for example, SystemParameters, which
would store the args[] and offer them for retrieval via accessor
method.

There are other ways, but for a four-class example, something of the
above should suffice.

.ed

www.EdmundKirwan.com - Home of The Fractal Class Composition

 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by Roedy Gree » Sat, 26 May 2007 17:34:02

On 24 May 2007 02:53:14 -0700, donkeyboy < XXXX@XXXXX.COM >
wrote, quoted or indirectly quoted someone who said :


It is duck simple.. Your main method gets passed an array of strings,
preparsed. See http://www.yqcomputer.com/
Roedy Green Canadian Mind Products
The Java Glossary
http://www.yqcomputer.com/
 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by Mark Spac » Sun, 27 May 2007 16:26:34


Just curious, but does this always work? (It's late and I'm fuzzy.) If
class Main and the simulation are in different packages... hmm, you'd
have to include the Main package in the code that's accessing...

import local.simulrun.Main;

//...

String [] args = Main.args;

Yeah ok, I guess it does, with a little help from import...
 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by Lew » Sun, 27 May 2007 23:35:49


import is just a hint to the compiler to help it discern the package for a
class. Essentially you always refer to every class by its fully-qualified
name, implicitly or explicitly.

import "goes away" in the bytecode.

--
Lew
 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by Lew » Sun, 27 May 2007 23:47:27


Initialization to null is entirely redundant.


Of course, public static mutable members are a Bad Thing. Better to create an
object and make the array an instance member with an appropriate accessor.

--
Lew
 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by Ed » Tue, 29 May 2007 02:17:58


Yeah, I've a bad habbit of doing that.

And as I look back at the snippet I proposed, I see I have a "this."
in a static method ... not too clever.

.ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by smieszni » Tue, 05 Jun 2007 23:13:07

Hi

To obtain command line arguments from any piece of code you can use:

final RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
List<String> strList = mx.getInputArguments();

But it is not a nice design pattern to do such things. Probably you
should find something more elegant.

Kind regards,
Adam Wo niak
 
 
 

Command line arguments passed to main: how do I access them from other classes? ...

Post by Andrew Tho » Tue, 05 Jun 2007 23:25:41


..

Any? Seems this code ..


.would only work in a 1.5+ VM, where either the code
is trusted, or it has no SecurityManager.

--
Andrew Thompson
http://www.yqcomputer.com/

Message posted via http://www.yqcomputer.com/