Common Issues and Workarounds While Building EAR files in Eclipse
In my Java Programming class at Penn State – Great Valley, we are learning all about Enterprise Java; session beans, JPA, JMS, and all kinds of other Java goodness. In the process, I am teaching them how to build their projects with Eclipse and Maven using the M2Eclipse plugin. The plugin has improved a lot in the last few months, but we still had to work out the kinks for a couple of issues.
The Default Maven Install Run Configuration Fails
This issue can be seen when running a project that has multiple modules in it. In my case we have an ejb module and then an ear module that is dependent on the ejb module. When we try to run the default maven install task for the project (see image to the right), we get an error:
The following mojo encountered an error while executing:
Group-Id: org.apache.maven.pluginsArtifact-Id: maven-ear-pluginVersion: 2.3.1Mojo: earbrought in via: packaging: ear
While building project:Group-Id: edu.psu.gv
Artifact-Id: classtwo-earVersion: 0.0.1-SNAPSHOTFrom file: /Users/rdimarco/Documents/workspace/classtwo/ear/pom.xmlReason: Cannot copy a directory: /Users/rdimarco/Documents/workspace/classtwo/ejb/target/classes; Did you package/install active project artifact:artifact = edu.psu.gv:classtwo-ejb:ejb:0.0.1-SNAPSHOT:compile;project: MavenProject: edu.psu.gv:classtwo-ejb:0.0.1-SNAPSHOT @ /Users/rdimarco/Documents/workspace/classtwo/ejb/pom.xml?
The problem is that there is a bug when the “Resolve Workspace Artifacts” button is checked. To resolve the problem, we need to create a custom run configuration, set the goals to install, and make sure the “Resolve Workspace Artifacts” is unchecked.
The build will now proceed correctly
Unable to Locate the Javac Compiler
Many of my students saw the following error:
</font></pre> > > <pre>
[INFO] ------------------------------<wbr></wbr>------------------------------<wbr></wbr>------------
[INFO] Class Two ..............................<wbr></wbr>............... SUCCESS [1.483s]
</font></pre> > > <pre>
</font></pre> > > <pre>
</font></pre> > > <pre>
</font></pre> > > <pre>
</font></pre> > > <pre>
</font></pre> > > <pre>
C:\Program
Files\Java\jre6\..\lib\tools.<wbr></wbr>jar
Please ensure you are using JDK 1.4 or above
and not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.
</font></pre>
As the error message suggests, this issue occurs when you are trying to run Maven and your the runtime JRE used to run the Maven executable is from a JRE and not a JDK. To fix this, you need to do the following things:
- Install a valid JDK on to your computer. You will need to download the JDK from Sun and then install it on your computer.
- Tell Eclipse about the JDK. To do this, you will
- Go to the Preferences.
- On Windows, choose the top menu “Window” and then select sub-menu “Preferences”
- On a Mac, choose the top menu “Eclipse” and then select the sub-menu “Preferences”
- Click on >Java and then on > Installed JREs
- You will see a box listing one or more JREs. Click on the button to the right of this box “Add”
- You will be given a list of types of JREs to use. Choose Standard VM
- You will then get a screen asking for the JDK directory. Click on the Directory button and navigate to the newly installed JDK directory
- On Windows, this will be something like C:\Program Files\Java\jdk1.6_xx
- On Mac, this will be something like /System/Library/Frameworks/JavaVM.framework/Versions/1.6.xx/Home
- After choosing your directory, Eclipse will think for a bit and then fill in some additional fields on the form. You do not need to make any changes, just hit Finish.
- Go to the Preferences.
- Now we will need to tell Maven to use this JDK when it runs
- Right click on your project and choose Run As -> Run Configurations (as you normally would to build)
- In the Run Configuration dialog, click on the tab JRE.
- Click on the radio button for Alternate JRE
- From the drop down list, choose your JDK
You will now be able to run Maven without the error.