为 Java jar 分配更多的堆空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/812883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:53:59  来源:igfitidea点击:

Allocate more heap space to a Java jar

javamemoryjarheap

提问by

When you run a program through the command line, you can use java -Xms -Xmxto specify the heap sizes. If the program is going to be run by double clicking a .jar file, is there a way to use more heap than the standard?

当您通过命令行运行程序时,您可以使用java -Xms -Xmx来指定堆大小。如果要通过双击 .jar 文件来运行程序,有没有办法使用比标准更多的堆?

采纳答案by Michael Myers

No. That's why I often make a .bat or .sh file with those parameters and direct users to run it instead of the .jar. Unfortunately, it's a little ugly to have to pop up a command-prompt window, but that can't be helped.

不。这就是为什么我经常使用这些参数制作 .bat 或 .sh 文件并指导用户运行它而不是 .jar 的原因。不幸的是,不得不弹出一个命令提示符窗口有点难看,但这无济于事。

As a side benefit, if your application freezes, you can direct users to put pauseinto the batch file (or do it yourself), and then you can see any stack trace that occurs.

作为附带的好处,如果您的应用程序冻结,您可以指导用户放入pause批处理文件(或自己做),然后您可以看到发生的任何堆栈跟踪。

Edit:You could also use an executable wrapper such as JSmoothor Launch4Jinstead of a batch file. You would lose some cross-platform compatibility, though.

编辑:您还可以使用可执行包装器(例如JSmoothLaunch4J)而不是批处理文件。但是,您会失去一些跨平台兼容性。

回答by Welbog

Instead of double-clicking the .jar file directly, you can use a batch file that runs java -jar -Xms -Xmx your_file.jar. From the user's point of view it's the same, but it gives you more control over the command that's actually run.

您可以使用运行 .jar 的批处理文件,而不是直接双击 .jar 文件java -jar -Xms -Xmx your_file.jar。从用户的角度来看,它是相同的,但它使您可以更好地控制实际运行的命令。

回答by Peter Lawrey

You can have the jar start itself again with Runtime.getRuntime().exec() with the options you want. The jar can contain more than one main() method (in different classes) and you can have one call the other via an exec().

您可以使用 Runtime.getRuntime().exec() 和您想要的选项让 jar 再次启动。jar 可以包含多个 main() 方法(在不同的类中),您可以通过 exec() 调用另一个方法。

回答by Esko Luontola

You can use JSmoothor a similar wrapper, which creates an EXE file that launches the JVM with the necessary parameters. That way you can avoid having a .bat file and its console dialog. Still one more way is to start the actual program in new process by using Runtime.exec or ProcessBuilder.

您可以使用JSmooth或类似的包装器,它会创建一个 EXE 文件,使用必要的参数启动 JVM。这样您就可以避免使用 .bat 文件及其控制台对话框。还有一种方法是使用 Runtime.exec 或 ProcessBuilder 在新进程中启动实际程序。