9

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.

2

2 Answers 2

7

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.

3
  • 4
    Creating a ClassLoader is overkill. You can just call OtherClass.main(String[]) from the main that adds the default args. Nov 6, 2009 at 19:30
  • 2
    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. Nov 6, 2009 at 19:42
  • I think ClassLoader extra projects mentioned here are in use to set different vm variables etc. For main args you are right, there is no need. Mostly they do some license check etc. startup ops and start process by Runtime.getRuntime().exec(processCmd); . Just not to use a .sh/.bat file but to use java runnable jar. Oct 22, 2018 at 13:40
1

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.