hidden_machine
hidden_machine

Reputation: 191

jpackage how do you pass in the insatllation directory itself as an argument to the jar file?

my final jar file requires an argument to be passed to it at runtime. The argument is the installation directory itself. I can't modify jar file or any java code, only the argument to be passed to it in jpackage.

If it were located in C:\path\to\jar\ I would call the jar file through java -jar jarFile.jar "C:\path\to\jar", but since I'm making the msi installer with the --win-dir-chooser , the installation directory could be anything, so I don't know what to pass in --arguments.

My current solution involves a "middle man" jar file as the --main-jar. The .exe file calls the "middle man" jar which in turn calls the final jar with the needed argument(by finding the current directory through java code). However, this is seems daftly unnecessary and I would like to find a replacement for this.

Could anyone help me out? Is there a better way to do this? Any suggestions would be helpful.

Upvotes: 0

Views: 968

Answers (1)

DuncG
DuncG

Reputation: 15196

Don't rely on the current directory as this could be wrong if you use shortcuts. Normally you would work out the installation directory of jpackage using System.getProperty("jpackage.app-path").

However as you are unable to change the code of the jar you can achieve the same result by defining fixed command line argument to your jar's main class by using --arguments parameter or arguments property in a launcher which references a special jpackage variable $APPDIR.

The value of $APPDIR variable is expanded at launch-time so will be filled in by the actual installation directory path to the app folder. There are three ways to hardwire the arguments to a generated EXE:

  1. Command line flag - note that on Linux you must escape the values or the shell will fill in $APPDIR from its own environment variable:

    jpackage  ... --arguments $APPDIR\relpathto\yourjar.jar
    
  2. With a configfile of parameters use jpackage @configfile with file configfile containing:

    --arguments $APPDIR\\relpathto\\yourjar.jar
    
  3. With a launcher properties file use jpackage ... --add-launcher yourappname=yourappname.properties with file yourappname.properties containing:

    arguments=$APPDIR\\relpathto\\yourjar.jar
    

After installation your launcher definitions config RELEASEDIR\app\yourappname.cfg should contain something like:

 [ArgOptions]
 arguments=$APPDIR\relpathto\yourjar.jar

For above to work the jar must be packaged somewhere into the release structure such as with jpackage --input somedir and that you use the new main class or --main-jar to replace your wrapper Main - check inside the jars MANIFEST.MF.

Note that running the EXE with any command line args will replace the hardwired argument.

Upvotes: 1

Related Questions