0

I'm currently working on a Java application implying multicast connection.

My development machine is a Mac, and I'm having troubles connecting to the multicast group. I fixed it by adding the argument -Djava.net.preferIPv4Stack=true is my running configuration. I can't set this property to true at runtime since the JVM reads it once at startup and not at runtime.

My problem is that when I will release my application, I want the user to simple 'double-click' on the icon and it will launch without any problem, even if the user is using Windows, Mac or any Linux distribution.

So my question is, how can I force the JVM to launch the .jar with this argument? Is there any way to tell maven to do that?

Many thanks in advance

faku
  • 411
  • 4
  • 19
  • Possible duplicate of [How to add jvm parameters for a runnable jar?](http://stackoverflow.com/questions/13787353/how-to-add-jvm-parameters-for-a-runnable-jar) – Kenster May 03 '17 at 13:50

2 Answers2

0

Since this is tagged in maven,

You can pass such JVM parameter via script. So, create a launch script for each type of machine. Like start.sh for unix start.bat for windows.

Use maven profiles to copy proper machine dependant script to final artifact directory i.e. target directory.

Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46
0

For Mac at least, this same problem occurs on Minecraft. You can force the Java flags through the Info.plist file in your app bundle.

It'll look something like this...

</dict>
</plist>
    <key>NSHighResolutionCapable</key>
    <string>YES</string>

    <!-- ######## ADD ONLY CONTENT BELOW THIS LINE ######## -->

    <key>LSEnvironment</key>
    <dict>
        <key>_JAVA_OPTIONS</key>
        <string>-Djava.net.preferIPv4Stack=true</string>
    </dict>

    <!-- ######## ADD ONLY CONTENT ABOVE THIS LINE ######## -->

</dict>
</plist>

For Windows, you can set a user environmental variable through a launch script or a desktop installer that sets _JAVA_OPTIONS=-Djava.net.preferIPv4Stack=true.

Technique was taken from here which offers a more thorough explanation : https://gaming.stackexchange.com/a/320864/199492

If you're trying to do this directly from a jar, you'll want a stub to detect and re-launch Java with overridden settings. This isn't trivial, but can be done with some Java tools combined with Runtime.exec(...) with the parameters provided.

Long story short, if you distribute a Java application, you should distribute a proper launcher (and usually an installer) so that you can control these types of things.

tresf
  • 7,103
  • 6
  • 40
  • 101