Maven : java.lang.ClassNotFoundException: Xmx512m

localhost:~ code2care$ mvn --version
Exception in thread "main" java.lang.NoClassDefFoundError: Xmx512m
Caused by: java.lang.ClassNotFoundException: Xmx512m
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

If you get the above stack trace when you run some maven command in Terminal (Mac OS or Linux) or on Command Prompt (Windows OS) then it is because you have not set the environment variable MAVEN_OPTS correctly, to fix this issue,

On macOS / Linux:
  1. Open terminal
  2. Type the following command: export MAVEN_OPTS=Xmx512m
On Windows:
  1. Right-click on the My Computer icon -> Properties -> Advance System Settings -> Environment Variables -> New
  2. Check the value of Variable Name: MAVEN_OPTS
  3. You must correct Variable Value by adding a hyphen: -Xmx512m
  4. Click OK.

MAVEN_OPTS is used to set the maven heap size it can be set as -Xmx256m, -Xmx512m, -Xmx1024m, -Xmx2048m and so on depending upon your project memory requirement!


This is not an AI-generated article but is demonstrated by a human.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!