views:

85

answers:

3

Is there a way to create a JAR-file that contains some arguments that are passed to the main class? (It does not matter whether it prepends or appends the arguments to potential command line arguments.)

I know I could simply write a bootstrapping class and specify this as main class (calling the real main class with the arguments), but this seems a bit awkward.

+1  A: 

I would design the application to have default settings built in, and override the defaults if the user gives different arguments.

In your setup, what happens if the user gives arguments that are different? Wouldn't end up with multiple similar arguments and have to awkwardly handle that?

Ben S
+1  A: 

To the best of my knowledge, no. You'll have to do that kind of thing yourself, in code.

A lot of people find it useful to write a little main class that sets up an environment and then acts as a ClassLoader for the "real" main program. Typically, such pre-mains fiddle with the classpath of their application, but your kind of problem is something else that could be solved like this.

Carl Smotricz
Creating a ClassLoader is overkill. You can just call OtherClass.main(String[]) from the main that adds the default args.
Laurence Gonsalves
Right you are, Laurence. It hadn't occurred to me that this is a degenerate case of what custom class loaders are usually used for.
Carl Smotricz
+3  A: 

Take a look at Java - VM arguments in manifest?, also check Can runtime arguments be included in the manifest?

Nathan Campos
+1 since you're actually answering the question rather than proposing a change.
Ben S