Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

java - How to set the -Xmx when start running a jar file?

As we know that we can set -Xmx1024M in window->preferences->java->installed jres->edit->default vm arguments in eclipse. But when I package this project into a runnable jar file, how can I set the -Xmx1024M when running the jar via java -jar A.jar?

Thanks a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Unfortunately, existing answers are wrong in one crucial point.

-Xmx must be passed to the Java runtime environment, not to the executed jar.

Wrong:

java -jar JavaApplication.jar -Xmx1024m 

Correct:

java -Xmx1024m -jar JavaApplication.jar 

More specifically, the java launcher needs to be used as follows:

java [options] -jar file.jar [arguments]

  • [options] are passed to the Java runtime environment
  • [arguments] are passed to the main function

The -Xmx parameter belongs to the (nonstandard) JVM options, and--being an option--needs to be listed before -jar (or at least before file.jar). The JVM will not recognize an -Xmx argument passed to the main function as proposed in other answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...