Can I set Java max heap size for running from a jar file?

Solution 1:

Write a batch or shell script containing the following line. Put into the folder, where MyApp.jar is stored.

java -Xmx256M -jar MyApp.jar

After that always open this batch/script file in order to launch the JAR file. Although, This will not embed specification of virtual memory size into jar file itself. But, It can override the problem to write the same command often on command line.

Solution 2:

you could also use a wrapper like launch4j which will make an executable for most OS:es and allows you to specify VM options.

Solution 3:

This isn't great, but you could have it as an executable JAR, and then in it's main, have it execute itself via the command-line as a non-daemon thread with the proper params stored in a properties file or calculated or whatever, then exit the original. You could even have it execute the jar with another "real" entry-point that expects those parameters.