-3

I want to pass in custom options to run compiled java code from command-line. For example:

java --output-dir "output_dir" --action Main

where --output-dir supplies an output directory and --action serves as a boolean to either execute a function or not.

How should I go about this?

Thanks.

qahoang
  • 7
  • 2
  • 4
  • 1
    What specifically is the issue? You can either access the command line arguments directly, or use a library to assist in setting up argument naming/handling/validation/etc. Why not just search the web? There are many tutorials, and SO isn't a tutorial site. – Dave Newton Apr 05 '19 at 20:36
  • 1
    Is there a reason you don't want to pass the option to the java class program instead of the java vm? Like this? java -jar yourProgram.jar --output-dir "output-dir" – Gustavo Ulises Arias Méndez Apr 05 '19 at 20:37
  • @DaveNewton I'm not talking about arguments. I'm talking about custom options. I have searched the web and read the docs but I think I'm searching the wrong keywords, hence this question on SO for directions. – qahoang Apr 05 '19 at 20:41
  • @qahoang You mean the `output-dir` is where your JAR/whatever lives? You can specify that by passing your classpath. You specify the class to run by providing the FQN of the class, e.g., https://stackoverflow.com/q/5474666/438992. Whatever this boolean is you're talking about would need to be an argument to your code, not to the JVM. – Dave Newton Apr 05 '19 at 20:46
  • @DaveNewton answer by user1933888 is what I'm looking for. I missed a section in the documentation that *clearly* answers my question. My bad here. Lesson learned! – qahoang Apr 05 '19 at 20:53
  • @qahoang So you want command line arguments--system properties are one way to do it, but kind of manky, unless you're explicitly overriding existing properties. Not what I'd use for general command line arguments at all. – Dave Newton Apr 05 '19 at 21:05

1 Answers1

1

While running java process you can pass properties, using following syntax

-Dproperty=value
Sets a system property value.

If value is a string that contains spaces, then you must enclose the string in double quotation marks:

java -Dmydir="some string" SomeClass

You can read more here: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

user1933888
  • 2,564
  • 3
  • 24
  • 34