1

I would like to modify/set JVM flags as soon as my program starts. I cannot do it on the command line, because I work with people who don't even know that exists. So it has to be automatically done in the program.

I am particularly interested by these three flags: -Xms4G -Xmx8G -noverify

I found in this discussion (or that one) that it is possible to modify some flags using the Interface HotSpotDiagnosticMXBean. And this code shows how to modify the flags. Unfortunately, the flags Xms or just ms are not recognized and then an exception is thrown.

I've also found that capsule may do the work, but it seems pretty heavy to use.

Is there any easy way to do it?

Community
  • 1
  • 1
FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • 2
    I would say you can't. But you could start a second jvm from your code with the desired parameters using a `ProcessBuilder`. – joshiste Apr 28 '16 at 20:16
  • I am not sure how it would work. Start an other JVM ok, but my program would have had already started on the first one. How can I make it switch on the second? – FiReTiTi Apr 28 '16 at 20:49

1 Answers1

3

You need to write two programs: one that is just a launcher to provide the correct parameters to run your other program. This is how Eclipse works, and Jitsi, and the now-end-of-life InstallShield Multiplatform launchers. It may be that you can write a trivial (eg one line or close to it) shell, .bat, or VBS script to do the job.

dsh
  • 12,037
  • 3
  • 33
  • 51
  • 1
    Thank you for your answer! So I have to create a first very simple executable jar file, called let's say Launcher, that is going to start a second executable jar file but with all the required argument. So in my case `java -noverify -Xms4G -Xmx8G -jar MyMainJar.jar`. Am I correct? If yes, would you know the command to start just a command? – FiReTiTi Apr 28 '16 at 21:10
  • Btw, I said jar launcher, because the soft has to be multi-plateforms. – FiReTiTi Apr 28 '16 at 21:49
  • 1
    Yes, you can write the launcher program in Java. (that is how the InstallShield Multiplatform launchers are) You can use [`Runtime.exec()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String:A-) or for more control [`ProcessBuilder`](https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) – dsh Apr 29 '16 at 12:41
  • Thanks, I am going to look into it asap, and I'll let your know! – FiReTiTi Apr 29 '16 at 17:16