问题
I'm trying to write a game engine, but it needs more memory than the default settings. I know that if you double-click a jar, the default settings are used. So is there anyway I can specify that the JAR requires custom heap sizes in it's manifest file (or elsewhere?)
Also, the engine is in it's own jar and the game is in another, with the engine as a library. If I specify different limits in both JARs, which one will the JVM give preference to?
回答1:
There aren't any properties in the manifest specifications that you can use to control heap space. Instead, you can write a launcher main method in your application that executes a second process with your real main method using one of the Runtime.exec implementations. Since you have access to the java.home system variable, you can use the version of Java that's running your launcher to also launch your game.
// Fix to use the version appropriate to the OS
String javaPath = System.getProperty("java.home") + "\bin\java.exe";
Runtime.exec("\"" + javaPath + "\" mygame.jar <heap_args>");
Using java.home
to get the Java path avoids path issues. This will also give you control over launching in the future if you decide to change how your program is launched. For example, this can be changed to use Process so that you can also wait for the process to terminate in your launcher, giving you control over post-run cleanup when the game's JVM is fully terminated.
回答2:
You cannot specify command line option in a JAR (except libraries for the class path)
What you can do is have the JAR restart itself with the options you want with Runtime.exec()
来源:https://stackoverflow.com/questions/12201878/specifying-jvm-heap-sizes-in-jar