1

I have a self-contained Java application packaged with the javapackager tool (version 8.0, Windows). How do I pass it system property values at application runtime (not at package time) on the command line?

The doc does not seem to address this.

I tried the standard Java way as in:

mypackagedapp.exe -Dmyprop=myvalue

but that does not appear to have an effect.

Al.
  • 25
  • 1
  • 8
  • What do you mean by "not appear to have an effect"? How do you go about to retrieving the property? Also, I strongly doubt that the standard Java way includes running an `exe`-file. – Tobb Jun 03 '15 at 12:43
  • System.getProperty("myprop") returns null. When I invoke my code instead using java.exe -Dmyprop=myvalue -jar... it returns "myvalue" as expected – Al. Jun 03 '15 at 12:50
  • Ah misread your question.. See https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/packaging.html#CEGBCJEB – Tobb Jun 03 '15 at 12:55
  • @Tobb: Same link I posted?? How does it address my question? Can you elaborate? – Al. Jun 03 '15 at 13:02
  • FWIW, I just posted a [similar but more general question](http://stackoverflow.com/q/30809330/1207769) about passing arguments to the JVM via the JavaFX self-contained application launcher, which would include system properties. – ctrueden Jun 12 '15 at 18:03

1 Answers1

0

Here is a code that validates if an argument exists in the command line.

See if the next code can help you.

public static void main(final String[] args) throws Exception {

    CommandLine line = validateArgs(args);
    if (null == line) {
        return;
    }
}

private static CommandLine validateArgs(String[] args) {
    Options flags = getArgs();
    CommandLineParser parser = new BasicParser();
    CommandLine line = null;

    try {
        // parse the command line arguments
        line = parser.parse(flags, args);
        if (line == null) {
            return null;
        }
    } catch (ParseException exp) {
        System.out.println(exp.getMessage());
    }
    return line;

}

static Options getArgs() {
    Options flags = new Options();
    Option dmyprop = OptionBuilder.withArgName("dmyprop")
            .hasArg()
            .withDescription("add description")
            .create("Dmyprop");
    flags.addOption(dmyprop);

    return flags;
}

In order to get environment variable you need to use:

        String env = System.getenv(option);

where option is your desired environment variable.

Hope it helped.

Igor
  • 846
  • 1
  • 11
  • 25
  • Could you please explain whats going on in your code? – try-catch-finally Jun 07 '15 at 07:14
  • @Igor: Thanks! I understand I can potentially write my own parsing for setting system properties. I just have a hard time believing that Java stops doing that for you as soon as you package your app to be self-contained. So I guess I'm hoping for an answer that either shows how Java does it in the case of self-contained apps or that points to this as a documented limitation of self-contained apps. – Al. Jun 08 '15 at 07:50