Maven - Could not reserve enough space for object heap error - maven

I am Maven to build my Java project. After several successful build, I am now getting the following error:
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
In my maven.sh file that my build is using, I have added the following as the first line
export MAVEN_OPTS=-Xmx512m
I am still getting the error.
Can anyone suggest a fix?

The solution was to set the forkMode of maven-surefire-plugin to 'never'. It seems that when tests are run, maven sure fire spawns a new JVM. Setting it to never fixed the issue.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
**<forkMode>never</forkMode>**
<argLine>-Xms1024m -Xmx1024m -XX:MaxPermSize=2048m</argLine>
<systemPropertyVariables>
<user.name>${user.name}</user.name>
</systemPropertyVariables>
</configuration>
</plugin>

I had the same issue and I figured Maven was not using the right jvm. You can check this with
mvn -version
In my case, the jvm specified in my JAVA_HOME and PATH was incorrect: I was using an x86 jdk 6 instead of an x64 jdk 7. Fixing this solved the issue.

I ran into this same issue when I was trying to build the Cloudera Navigator SDK examples. I am using a 32-bit JVM and the compile seemed to go OK but the test afterwards failed with this error:
Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap
I tried setting MAVEN_OPTS=-Xmx512m but this had no effect- it failed with the same message. Even the value of 2097152KB in the error message was still the same (strange!).
I finally figured out that the heap size value had been hard-coded for the test in the pom.xml file! It had
<argLine>-Xmx2048m ...
</argline>
I edited pom.xml and changed that to -Xmx1024m and then maven was able to build and test everything with no problems.
So the lesson from this is that if you are building something given to you by someone else (such as Cloudera) and you get an error like this, check pom.xml carefully to see if the setting is hard-coded in there.
FYI- I think that using a 64-bit JVM might have also possibly resolved this, but I can't switch to 64-bit. We have some other stuff that we need to get this to work with that says that it only works with a 32-bit JVM (I can't really explain in any more detail than that here).

I think you want to reserve more space than you have, with your settings:
MAVEN_OPTS=-Xmx512m
Try to set it lower, since Maven starts only if it can reserve the whole space.
This problem occurred to me and I could solve it like described.

Try to set variable:
MAVEN_OPTS = -Xmx512m -XX:MaxPermSize=128m

Changing to MAVEN_OPTS = -Xmx512m helped me on a Windows 64 bit.

Try Java 64 bits and configure your java_home to this Java 64.
obs.: javac -version and mvn -version needs return this Java 64.

As Didier L above said
This was exactly what problem I was having, we were running the x86 JVM instead of the 64 bit...
It fixed my issue when I changed to the 64bit JVM.

In IDEA IntelliJ IDE there is a setting on Maven -> Runner to use a specific JVM. The default for me was to use the JRE and not the JDK set on JAVA_HOME. Make sure to check that setting. Once i switched to use JAVA_HOME everything ran fine.

I tried this and its works for me-
maven version -3.2.5
java version -jdk1.7.0_10
-Xmx600m -XX:PermSize=256m

Related

How to add enable flag for Flight Recorder in Maven project?

I'm just about start using Java Mission Control 5.3.0.
I have added -XX:+UnlockCommercialFeatures -XX:+FlightRecorder into my web-app's jetty.template.
Then I start the web-app with mvn jetty:run.
But while I was starting Flight Recorder I got the problem occurred pop-up as below.
'Start Flight Recording.. (Last attempt failed)' have encountered a problem.Commercial features are not enabled. In JDK7u4 and above,the JVM must be started with -XX:+UnlockCommercialFeatures -XX:+FlightRecorder .
And I also try adding the flags as below into pom.xml but it doesn't work.
<jvmArgs>
<jvmArg>-Xmx128m</jvmArg>
<jvmArg>-Xms128m</jvmArg>
<jvmArg>-XX:MaxPermSize=512m</jvmArg>
<jvmArg>-XX:+UnlockCommercialFeatures</jvmArg>
<jvmArg>-XX:+FlightRecorder</jvmArg>
<jvmArg>-XX:StartFlightRecording=duration=160s,delay=20s,settings=profile,filename=target/recording.jfr</jvmArg>
</jvmArgs>
What should I do for enable Flight Recorder on my web-app?
That configuration should be added to the running VM of Jetty so I'd try this:
mvn jetty:run -XX:+UnlockCommercialFeatures -XX:+FlightRecorder
I could not test this, and I work most on Tomcat, but I think this could help you
I got the answer by using below.
export MAVEN_OPTS="$MAVEN_OPTS -XX:+UnlockCommercialFeatures -XX:+FlightRecorder"
If you are using jcmd command from jdk, then could enable JFR via:
jcmd 31118 VM.unlock_commercial_features
Then use JFR.
e.g
jcmd 31118 JFR.start name=a
After some tests, I'll add something that could help someone else :
This is the JVM (Azul Zulu) I am using for my tests :
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (Zulu 8.50.0.21-CA-linux64) (build 1.8.0_272-b17)
OpenJDK 64-Bit Server VM (Zulu 8.50.0.21-CA-linux64) (build 25.272-b17, mixed mode)
I created in the top of the project a file :
.mvn/jvm.config
That contains only one instruction :
-XX:StartFlightRecording=filename=build.jfr
Then, do whatever mvn goal you want to do and look for the build.jfr command you want.
Zulu's JVM has flight record enabled by default, so you this is one step less to worry.
It is important to mention that, in my case I had several JVM's installed in my machine and the mvn command uses JAVA_HOME variable, so make sure it is pointing to the right jvm.

Maven jetty plugin using wrong resource file URL

I am running a project within a vagrant box setup over my eclipse workspace. I am working on setting up a new maven project but I am having problems with the plugin using windows paths instead of the vagrant path. My windows workspace is setup in C:\Dev, so when I 'vagrant up', my entire workspace is available within my VM. In other words, /vagrant in the VM contains the contents of C:\Dev.
When I execute mvn jetty:run, everything starts up fine and all paths use the vagrant versions (/vagrant/mvn_project/target...). However, once the plugin starts scanning the project for changes, it throws the following error:
2014-02-26 01:18:53.756:WARN:oejw.WebAppContext:Scanner-0: Failed startup of context o.e.j.m.p.JettyWebAppContext#591f46d8{/,[file:/vagrant/mvn_project/web, file:/vagrant/mvn_project/target/webapps/ROOT/],STARTING}{/ROOT/]}
javax.servlet.UnavailableException: Malformed URL 'file://C:\\Dev\\mvn_project/target/webapps/ROOT/WEB-INF/dtd/web-app_2_3.dtd' : For input string: "\\Dev\\mvn_project"
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:402)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:561)
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:351)
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:840)
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:300)
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1347)
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:745)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:492)
at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:282)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
at org.eclipse.jetty.maven.plugin.JettyRunMojo.restartWebApp(JettyRunMojo.java:532)
at org.eclipse.jetty.maven.plugin.JettyRunMojo$1.filesChanged(JettyRunMojo.java:485)
at org.eclipse.jetty.util.Scanner.reportBulkChanges(Scanner.java:681)
at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:539)
at org.eclipse.jetty.util.Scanner.scan(Scanner.java:391)
at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Is this a bug with the plugin or is there a configuration setting I can use to set this value?
Edit: A little more context... It seems that the problem has something to do with filtered resources.
<resources>
<resource>
<directory>${project.basedir}/src/main/filtered-resources</directory>
<filtering>true</filtering>
<targetPath>${project.basedir}/target/webapps/ROOT</targetPath>
</resource>
</resources>
Changing ${project.basedir} to /vagrant/mvn_project seems to fix the problem, but clearly this is just a workaround and not a solution (won't work in CI for example).
UPDATE: It turns out, the blame is on Eclipse. Eclipse is occasionally building the project and when it does so, ${project.basedir} refers to C:\Dev\mvn_project instead of /vagrant/mvn_project. Is there a way to override ${project.basedir} without hard coding?
Simple answer: disable builds in eclipse. Uncheck Project -> Build Automatically. Always run builds from within vagrant (mvn compile). JSP hot changes still work automatically.

Maven: vm args in MAVEN_OPTS arent working, but they are if specified in the command line

I have a particular test case that is run by surefire which fails when this vm arg is not set.
I am able to get the test to successfully pass if i run:
mvn test -Dtest=TestClass -Djava.net.preferIPv4Stack=true
This works great. Now I want to generalize this so that the vm arg is set for all maven builds.
In my .bashrc I've added
MAVEN_OPTS='-Djava.net.preferIPv4Stack=true'
I have confirmed via echoing the env variable that it is set. This unfortunately does not make a difference for my test case. What other considerations am I missing that would interfere here?
Thanks!
R
Based on the documentation you can give a supplemental configuration to the maven-surefire-plugin:
<configuration>
<argLine>-Djava.net.preferIPv4Stack=true</argLine>
...
</configuration>

Maven Ant deploy - OutOfMemoryError: Java heap space

Scenario
While using the Maven Ant Task artifact:deploy, I'm encountering the error java.lang.OutOfMemoryError: Java heap space.
I'm only getting the error if the size of the file being deployed is greater than 25 MB. My artifacts are not greater than 50 MB in size.
What could the reason be? And, what can I do to fix it?
Code snippet
<artifact:deploy file="#{app.name}.jar">
<pom file="#{pom.file}"/>
<remoteRepository url="http://xxx.com:xxx/xxx-webapp/content/repositories/xxx-releases/">
<authentication username="xxx" password="xxx" />
</remoteRepository>
</artifact:deploy>
Existing solutions
Most online results indicate that it's something to do with the JVM default heap size and that it can be fixed by setting the appropriate environmental variables.
However, I would want the Ant scripts to run on any computer and not to depend on the environmental variables.
Is there a way to configure these settings in the Ant scripts or the
POM file?
EDIT
The install-provider task (http://maven.apache.org/ant-tasks/examples/install-deploy.html) seems to work for some people. I keep getting download errors when I use it.
Answer
It turns out that I'm not getting the Java heap error when I run my Maven Ant task on a different machine (which probably has more memory allocated to the JVM heap). Hence, I haven't attempted the solution mentioned by #Attila, though it seems to be going in the right direction.
Once ant is running, you cannot change the heap size of the JVM runing ant. So your only option is to run the task that comsumes a large amount of memory in a separate JVM, specifying enough heap space. Note this relies on the task allowing you to fork a new JVM to execute the task
Update: I could not find a way to specify to fork the maven (deploy) task, but this page specifies how you can define a macro to run maven using the java task (note that this relies on maven beeing installed and properly configured on the machine) (see the "Using the Java Task" section)
please try to increase VM memory, eg.: -Xmx512m
if you are using ANT, you can add it to the ANT_OPTS environment variable: ANT_OPTS="-Xmx512m"

"PWC6345: There is an error in invoking javac." error when using Jetty WTP plugin to deploy a JSP page on Jetty

I'm trying to deploy a JSP-page on Jetty, using the Jetty WTP plugin for Eclipse. But I get the error below. It looks like Jetty can't find javac. Is there any settings I have to do for the Jetty WTP plugin in Eclipse or how do I fix this?
The JSP page works fine if I export my project as a .war-file to jetty\webapps and then start Jetty manually using java -jar start.jar. But it doesn't work if I deploy using the Jetty WTP plugin for Eclipse as described.
I have my JAVA_HOME set to C:\Program Files (x86)\Java\jdk1.7.0_01 and I use Jetty 8.0.4 on Windows 7.
Servlets is working fine with the current setup. Any suggestions on how to solve this for JSP pages?
HTTP ERROR 500
Problem accessing /MyJavaWeb/formProcess. Reason:
PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required
Caused by:
org.apache.jasper.JasperException: PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:92)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:378)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:119)
at org.apache.jasper.compiler.Jsr199JavaCompiler.compile(Jsr199JavaCompiler.java:208)
at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:384)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:453)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:625)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:558)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:488)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:520)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:973)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:417)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:907)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:271)
at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:98)
at com.example.FormProcessServlet.doPost(FormProcessServlet.java:39)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:558)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:488)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:483)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:973)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:417)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:907)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
at org.eclipse.jetty.server.Server.handle(Server.java:346)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:442)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:941)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:801)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:224)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)
at java.lang.Thread.run(Unknown Source)
As Jan Bartel suggested, from Jetty: How to configure JSP:
by adding the line below in the start.ini-file in Eclipse, it works fine.
-Dorg.apache.jasper.compiler.disablejsr199=true
I too happened to suddenly get the org.apache.jasper.JasperException: PWC6345 with my Eclipse installation and the Jetty Maven Plugin (which I use to start up Jetty from within Eclipse).
The reason was that I updated my JDK installation shortly before. Inside Eclipse, this caused to change the "Installed JRE" to point to a plain JRE installation instead of a full-fledged JDK installation. A JDK installation is needed for Jasper, as Jasper needs javac (which is not provided by JRE).
In my case, the solution was to provide Eclipse with the JDK installation. To do so, select Window -> Preferences -> Java -> Installed JREs. Here, click Add..., then point to your JDK installation (in my case, JRE home should point to C:\Program Files (x86)\Java\jdk1.7.0_17). Click Finish. Then back in the Installed JREs overview, remove the old JRE reference and select the newly added JDK installation.
I got the PWC6345 error when running a webapp with Run-Jetty-Run plugin.
The fix was to change the Execution environments settings in Preferences.
Since I'm using java 7 I needed to change the setting for JavaSE-1.7.
Probably other execution environments will behave similarly.
Window -> Preferences -> Java -> Installed JREs -> Execution environments
Select JavaSE-1.7 on the left -> pick the *jdk*1.7.x_xx on the right hand side
Ok, solved this after a lot of searching around.
Download any jdk and point JAVA_HOME to this jdk.
Inside this jdk folder you will find ./bin and ./jre/bin
Copy the javac from ./bin and paste it in ./jre/bin [remember copy not cut]
Now in eclipse, Run->External-Tools->External-Tools-Configurations, select the build.xml for of your project in the left pane and click on the JRE tab in the right pane.
Click on installed JREs and add the jdk in the previous step.
Once done, select this new JRE (jdk1.6.0_31) as the 'Separate JRE', click on apply at the bottom and run.
This assumes you are doing a jetty:run using ant on eclipse.
Strange because this thread suggests:
The solution was to install the package jdk7-openjdk.
Perhaps this should be added to the list of dependencies (or maybe for jetty, and not solr itself)?
But that doesn't help much on Windows, where the binary for openjdk7 aren't easily available.
(In "OpenJDK availability for Windows OS" comments, eckes mentions having found some openjdk binaries for Windows, as part of the Build b146. However, said releases aren't maintained any more)
The bug report filled by the OP Jonas is closed with a simple reference to the Eclipse wiki page "Jetty/Howto/Configure JSP".
You have planty solutions guys but I thought that it exist a solution better than fix the start.ini
I lived the same problem and then I realize that it may be because I did have a JDK7 for 64bit while I should have a JDK7 for 32.
So I fixed my problem by downloading and use the JDK7 for 32Bit.
Also, you would still need to
Use the JDK and not the JRE
Set your JAVA_HOME
I am running on Windows by the way ...
I have the same problem. When I run where java, the output is:
C:\Windows\System32\java.exe
D:\Program Files\Java\jdk1.7.0_25\bin\java.exe
Of course the first one is incorrect. So my problem lies in the PATH. I can rectify this by either removing the java.exe in the directory C:\Windows\System32\ because I'm sure it's useless, or putting the second entry in front of the first one.
So When encountering this problem, please check your PATH for the jdk. I hope that my problem and my solution is helpful in some way.
I got the PWC6345 error when running jetty-runner 9.2.1 standalone from Windows 7 cmd (or batch file) with Oracle JDK 1.7.0_60, and none of the other suggestions in this or other forums worked. I'll leave my workaround here, in case it helps someone...
After installing the JDK, running 'where java' showed two different java.exe paths, the first being in the Windows system folder, and the second in the JDK folder (which is the one I added to the path manually). Both of these are actually installed by the same JDK installer. The java.exe in the system folder was taking precedence and causing the full JDK to go undetected.
Placing the JDK bin folder (e.g. "C:\Program Files\java\jdk1.7.0_60\bin") on the path before the other system paths fixed the issue.
In addition to updating the JAVA_HOME (see above)...
If you're encountering this error from command line (e.g. starting Jetty server from dos window), check where windows is finding the java.exe:
where java
If windows comes back with a path showing to your jre, then check your PATH. Chances are the %PATH% contains a path pointing to the jre instead of jdk.
if you set environment variable JRE_HOME , please unset or delete it
and start server again .
In liferay 7 : I got this error fixed by changing the runtime environment of the server from jre to jre with jdk
I got this error when deploy war package to our server.
My deploy command is (applied web container you may find):
java -jar jetty-runner.jar --port 8020 xx.war
I write this answer because it confused me almost one day. I tried some methods above but failed.
Finally I found there is only jre on my servers but not full jdk.
$ls /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-2.b11.el7_3.x86_64
jre
I fix it with searching available jdk and install one.
$yum search java | grep 'java-'
$sudo yum install java-1.8.0-openjdk-devel.x86_64
(Notice the -devel)
I added this line to 'eclipse.ini' file which is present inside eclipse folder.
-vm
C:\Program Files\Java\jdk1.8.0_131\bin\javaw.exe
and I also change the JRE path in eclipse
windows -> preferences -> java -> Installed JREs
and provided path upto jdk
C:\Program Files\Java\jdk1.8.0_131
I spent good amount of time on this issue, but it turned out to be very simple. All you have to do is:
choose the JDK instead of JRE while configuring the server runtime!
It worked like a champ, I had this issue with glassFish 4.0

Resources