How to set the -Xmx when start running a jar file?
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.
try java -Xmx1024m
filename.
I found this on StackOverflow What does Java option -Xmx stand for? and use it when I start Netbeans for instance.
use it like this
java -Xmx1024m -jar JavaApplication.jar
info:
-Xmxn
Specify the maximum size, in bytes, of the memory allocation pool. This value must be a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. The upper limit for this value will be approximately 4000m on Solaris 7 and Solaris 8 SPARC platforms and 2000m on Solaris 2.6 and x86 platforms, minus overhead amounts.
Three methods:
- Command Line:
- Instruct your users to run your application using "java -Xmx1024m -jar SampleJavaApp.jar"
- Java Control Panel:
- Instruct your users to dedicate more memory to java by default: Win7 guide
- Restart your jar with the appropriate Xmx value.
The last option is "evil" but doesn't require any extra effort from your users. Here's a sample block of code:
public static void main(String[] args) throws IOException, URISyntaxException {
String currentPath=SampleJavaApp.class
.getProtectionDomain()
.getCodeSource().getLocation()
.toURI().getPath()
.replace('/', File.separator.charAt(0)).substring(1);
if(args.length==0 && Runtime.getRuntime().maxMemory()/1024/1024<980) {
Runtime.getRuntime().exec("java -Xmx1024m -jar "+currentPath+" restart");
return;
}
}