111

I have my JAVA_HOME set to:

C:\Program Files (x86)\Java\jdk1.6.0_18

After I run maven install, I get this message from Eclipse:

Reason:

Unable to locate the Javac Compiler in:
  C:\Program Files (x86)\Java\jre6\..\lib\tools.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.

I'm certain that this is the tricky part

Please ensure you are using JDK 1.4 or above and not a JRE

When I run configuration it's set to JRE6. How do I change it to JDK 1.6 which I have already installed?

I even tried to modify the plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
        <executable>C:\Program Files (x86)\Java\jdk1.6.0_18\bin</executable>
    </configuration>
</plugin>

Still I get the same error.

I use the Eclipse Maven plugin. How can I change from JRE to JDK in Eclipse?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263
  • Have you double-checked using the command-line `set` command to make sure JAVA_HOME is set to what you think it is? – Powerlord Feb 08 '10 at 15:56
  • I'm using java on daily basis .. everything works fine here it is : `java -version .6.0_18-ea" time Environment (build 1.6.0_18-ea-b04) ) Client VM (build 16.0-b11, mixed mode, sharing)` – Gandalf StormCrow Feb 08 '10 at 15:58
  • 1
    Set true when setting the executable. – Robin Feb 08 '10 at 16:01
  • That doesn't actually tell us which Java install `java -version` is running; it could be the JRE or JDK install. `javac -version` would be a little more useful. However, even then if PATH is correct and JAVA_HOME is wrong, these would still show up how you would expect. – Powerlord Feb 08 '10 at 16:03
  • 1
    Also, executable should be ...\bin\javac, you have to specify the actual executable, not just the path. – Robin Feb 08 '10 at 16:15

32 Answers32

143

You could try updating the JDK Eclipse is using, as follows:

Add and set the JRE in menu WindowPreferences...JavaInstalled JREs:

JRE type: Standard VM JRE
Name: jdk1.6.0_18
JRE home directory: C:\Program Files (x86)\Java\jdk1.6.0_18

If this is not the case, it's possible that the brackets and spaces in the JAVA_HOME path are causing issues. Try copying your JDK to a different location and updating your JAVA_HOME.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lucrussell
  • 5,032
  • 2
  • 33
  • 39
  • 1
    It is indeed the first part of your question, JAVA_HOME thing is ok – Gandalf StormCrow Feb 08 '10 at 16:43
  • 47
    For me an extra step was needed: going to the properties of said project->java build path->libraries->select "JRE System Library", click Edit and select "Workspace default JRE" – maayank Jan 30 '11 at 09:56
  • 1
    was not enough for me, had to edit eclipse.ini. I believe there is a bug. – ruhsuzbaykus Jun 30 '11 at 14:21
  • It worked for me only when I checked the appropriate jdk in the Execution Environment -> Compatible JREs. – Vedran Aug 21 '12 at 10:47
  • 9
    In addition, if you run Maven from an eclipse run configuration that you created before these configuration changes, remember going to Run configuration window / JRE tab and selecting the right JRE. Otherwise you could be using the old wrong JRE even after updating your eclipse and project settings to the new one. – Diana Sep 22 '13 at 16:25
  • Also, if you're running maven commands from command line, remember that you have to close the window and reopen it, for changes in system variables to take effect, unless you edit the system variables comand line as well.Might be obvious to most, but this took me forever to figure out. – jumps4fun Mar 14 '19 at 09:23
20

File eclipse.ini needs the vm argument to point to your JDK location.

Edit the eclipse.ini file to point to your JDK home, something as follows -

-vm
C:\Program Files\Java\jdk1.6.0_06

This ensures that Eclipse would be running off the JDK and not any default JRE on your machine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sai Hegde
  • 623
  • 5
  • 12
  • 5
    This fixed my problem. From researching on the web, it looks like the Maven uses JAVA_HOME, but the Maven Plugin knows nothing about JAVA_HOME and instead defaults to the VM used to start Eclipse. – jnosek Jul 14 '11 at 15:18
13

You try:

Go to Preferences/Java/Installed JREs and add one for the location "C:\Program Files\Java\jdk1.6.0_27\jre or something like that.

Remove the one for C:\Program Files\Java\jre6

As you can see, the path C:\Program Files\Java\jre6\..\lib\tools.jar only makes sense if the first part (til the /..) is replaced by C:\Program Files\Java\jdk1.6.0_27\jre.

cuocdoibian1971
  • 131
  • 1
  • 2
10

Setting fork to true resolved the issue for me.

<configuration>
    <fork>true</fork>
    <source>1.6</source>
    <target>1.6</target>
</configuration>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prash
  • 101
  • 1
  • 2
9

I had exact the same problem! I had been searching and searching for days because all the babble about "put the -vm c:\program files\java\jdkxxxxx\bin" in the ini ar as argument for a shortcut did not at all help!

(Do I sound frustrated? Believe me, that's an understatement! I am simply furious because I lost a week trying to make Maven reliable!)

I had very unpredictable behavior. Sometimes it compiled and sometimes not. If I did a maven clean, it could not find the compiler and failed. If I then changed something in the build path, it suddenly worked again!!

Until I went to menu WindowPreferencesJavaInstalled JRE's. I added a new JRE using the location of the JDK and then removed the JRE. Suddenly Maven ran stable!

Maybe this is worth putting in letters with font-size 30 or so in the Apache manual?

With all due respect, this is simply outrageous for the Java community! I can't imagine how many days were lost by all these people, trying to work out their problems of this kind! I cannot possibly imagine this is released as a final version. I personally would not even dare to release such a thing under the name beta software...

Kind regards either way.... After a week of tampering I can finally start developing. I hope my boss won't find out about this. It took me lots of effort to convince him not to go to .NET and I already feel sorry about it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lorenzo
  • 1
  • 1
  • 1
  • 1
    bottom line, use it from console not from eclipse you won't have this kind of issues, that is what I've realized after few months of using it, now pretty comfortable with maven – Gandalf StormCrow May 11 '10 at 12:43
  • Its more of an eclipse...m2e problem.. I have the same problem and th other IDEs(netbeans and IDEA) works perfectly. – joshua Aug 20 '12 at 18:02
3

I have just installed Maven 3.0.4, set M2_HOME, M2 and JAVA_HOME accordingly. I then ran the following commands:

mvn archetype:generate....

mvn compile

I got failed message such as: maven unable to locate javac compiler.

On my computer, I have by default JRE installed at C:\program files\java\jre.1.6. And I have the other C:\java\jdk1.6 and JAVA_HOME set to this C:\java\jdk1.6.

I didn't see anything wrong with my setting. After searching, I decided to look into mvn.bat, I echoed %JAVA_HOME% every step and it displayed correctly as C:\java\jdk1.6, but when I run the command mvn --version, it displayed Java home: C:\program files\java\jre1.1.6.

Finally, I found out that I just need to set -Djava.home=c:\java\jdk1.6 as in the command below, and working fine now:

%MAVEN_JAVA_EXE% %MAVEN_OPTS% -classpath %CLASSWORLDS_JAR% "-Dclassworlds.conf=%M2_HOME%\bin\m2.conf" "-Djava.home=%JAVA_HOME%" "-Dmaven.home=%M2_HOME%" %CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS%

Note: This is for mvn on the command line and not with Eclipse.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
smngo
  • 31
  • 1
  • Hey, you don't need to go and always explain each step you did. Please see [How to answer to the point](http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question). – mtk Jun 24 '12 at 10:18
  • That seems like a hack (but hey... if it works for you!) but its interesting that the maven command line picks up the wrong java home - this is our current issue. – plasma147 Oct 12 '12 at 08:44
3

I got the same problem using Run as -> Maven install in Eclipse. JAVA_HOME and eclipse.ini were OK and pointing to my latest JDK. But m2clipse used JRE. Using mvn install outside of Eclipse worked fine!

In my case, I solved the problem as follows:

  1. Navigate in Eclipse: menu WindowPreferencesJavaInstalled JREs
  2. There were two inactive entries for a JRE and JDK. The active one was the latest installed JDK. Delete all entries but the one Maven should use.

I think Maven doesn't take into account which one is active...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Markus
  • 31
  • 1
3

Based on Compiling Sources Using A Different JDK, you will have to set the fork to true in your pom.xml file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Yang
  • 31
  • 1
2

I solved the issue by changing location of Installed JRE to the directory present in JDK itself. Setting proper value of JAVA_HOME environment variable did not solve the issue.

In my case, there are two directories

  • C:\Program Files\Java\jdk1.7.0_03 --> For JDK

  • C:\Program Files\Java\jre7 --> For JRE

Initially I set C:\Program Files\Java\jre7 as Installed JRE in Eclipse and I was getting the same error in case of Maven(Ant was working fine).

By changing the installed JRE to C:\Program Files\Java\jdk1.7.0_03\jre7 as Installed JRE in Eclipse the issue is solved.

Marko
  • 20,385
  • 13
  • 48
  • 64
Nikhil
  • 21
  • 2
2

Don't forget that if you are running your project from a particular Eclipse configuration, you should change the Java Runtime within 'Run Configurations' --> 'Your maven configuration' --> 'JRE tab'after you add the right JDK to your Eclipse preferences.

Ivan Fernandez
  • 4,173
  • 5
  • 25
  • 30
2

Eclipse setup only

Do the following steps:

  1. Right click on Eclipse project Properties

  2. Java Build Path → Libraries

  3. Select JRE System Library → Click the Edit button

  4. Click "Installed JREs..." button

  5. Edit JRE as: Set JRE Home = JAVA_HOME or JAVA_HOME\jre

Screenshot:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Parthasarathy B
  • 1,942
  • 1
  • 16
  • 10
2

It sounds like Maven is using the JRE, not the JDK. Perhaps you installed Maven before installing the JDK?

Check the settings files. There are three in total, system, user, and project. It's most likely in the installation configuration ($M2_HOME/conf/settings.xml) or, possibly, the per-user configuration (${user.dir}/.m2/settings.xml).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
1

As mentioned by several others, the Eclipse WindowPreferences...JavaInstalled JREs should point to the JDK you installed, not to the JRE. Only then it can find the ../lib folder mentioned in the error message.

Even with this, the problem may recur. My way out in Eclipse v4.2 (Juno) is to do a menu MavenUpdate project... after which the problem disappears.

I suspect the reason is that some of the Eclipse generated files (.classpath, .project, .preferences) are in Subversion for the project in which I'm having these problems. Thus, an SVN update introduces the problem, and an configuration update from Maven in Eclipse resolves it again.

Real solution: omit Eclipse generated .files from version control, and let the Maven Eclipse plugin handle project configuration. (Additional pointers/suggestions are welcome).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
avandeursen
  • 8,458
  • 3
  • 41
  • 51
1

In my case, in Eclipse menu RunDebug Configurations.

In the JRE tab selecting the JDK instead of the JRE solved the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

I had the same issue on Amazon Linux. It turns out I had to install the developer version of the JDK:

sudo yum -y install java-1.7.0-openjdk-devel
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Evan Siroky
  • 9,040
  • 6
  • 54
  • 73
1

Right click on menu ProjectPropertiesJava Build Path.

Select the JRE System Path → Edit. Select the Workspace Default JRE, and point it to JDK > 1.4.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
asHIS
  • 11
  • 1
1

Go to Eclipse's menu WindowPreferences...JavaInstalled JREs should point to the JDK you installed, not to the JRE.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sudipto
  • 11
  • 1
1

Use menu WindowPreferencesJavaInstalled JREsExecution Environments -> click the checkbox on the right side.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gambotic
  • 51
  • 2
1

The solution is simple. Sometimes the Eclipse WindowsPreferencesJavaInstalled JREs pointing to C:\Program files\Java\jre.

When I changed it to C:\Program files\Java\JDK 6.0\jre it is worked 100%.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
santhosh
  • 11
  • 1
0

You can also make sure that Eclipse has all of the updated changes. To do this, right-click your project and then press the "Refresh" menu item.

GregNash
  • 1,316
  • 1
  • 16
  • 25
0

Many other answers tell you to point your Eclipse to a JDK instead of a JRE.

Here is another answer with more specific/clear [than I have seen in any answer here] step-by-steps on how to do that: How do you install JDK?

That is what for solved me for this same problem.

Community
  • 1
  • 1
cellepo
  • 4,001
  • 2
  • 38
  • 57
0

Make sure the "-vm" in your eclipse.ini is on two sperate lines, ie:

-vm
C:\Program Files\Java\jdk1.6.0_06
0

I had the same error, because of the JUnit version. I had three 3.8.1, and I have changed to 4.8.1.

So the solution is:

You have to go to the POM, and make sure that you dependency looks like this

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8.1</version>
  <scope>test</scope>
</dependency>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ibo
  • 89
  • 4
0

I changed the configuration of maven-compiler-plugin to add executable and fork with value true.

<configuration>
    <fork>true</fork>
    <source>1.6</source>
    <target>1.6</target>
    <executable>C:\Program Files\Java\jdk1.6.0_18\bin\javac</executable>
</configuration>

It worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Soumyajit Swain
  • 1,298
  • 1
  • 21
  • 35
0

I would guess that the location of the compiler is defined in a POM for the compiler plugin to be in the JRE location displayed, instead of the JDK location you have JAVA_HOME pointing to.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robin
  • 24,062
  • 5
  • 49
  • 58
  • @Robin Where exactly should I look for that ? the pom looks fine to me `org.apache.maven.plugins maven-compiler-plugin 2.0.2 1.6 1.6` – Gandalf StormCrow Feb 08 '10 at 15:32
  • If there is not an defined, then this is not what is causing your problem, unless the pom you are looking at inherits from a parent pom which has it defined. – Robin Feb 08 '10 at 15:42
  • When defining the executable, you also have to set to true. – Robin Feb 08 '10 at 16:00
0

Do Echo %JAVA_HOME% and then mvn --version.

The JRE path should be same... then go menu WindowPreferencesJavaInstalled JRE's location should be same as what Java_Home is showing.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Go to the Properties dialog and right click on the project.

In Linked resources, add a new path called JAVA_HOME and put in your JDK location, something like "C:\Program Files\Java\jdk1.8.0_73".

That's all!!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I don't think you should add any variable.. just change the JDK config under Run seetings to workspace JDK – Phani Feb 10 '16 at 20:21
0

The solution to this problem is very simple...

If you don't have Ant build file then generate it. In Eclipse you can easily create a Ant file.

Refer to the link to create Ant build file [http://www.codejava.net/ides/eclipse/how-to-create-ant-build-file-for-existing-java-project-in-eclipse].

Now follow the given steps:

1) Add your Ant build file in Ant view that is in view window.

2) right click on your Ant build file and select Run As and the second option in that "Ant Build".

3) Now a dialog box will open with various options and tabs.

4) Select the JRE tab.

5) You will see three radio buttons and they will be having JRE or JDK selected as an option.

6) Look carefully if the radio button options are having JRE as selected then change it to JDK.

7) Click apply.

That's it...!!!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nishat Lakhani
  • 733
  • 1
  • 8
  • 20
0

I had the same issue. None of the above worked, but this worked for me:

I changed JAVA_HOME from ...\jdk.1.8.0\ to ...\jdk.1.8.0\jre\

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Coen Damen
  • 2,009
  • 5
  • 29
  • 51
0

Go to Eclipse → PreferencesJavaInstalled JREs → select the JRE location you have pointed to and click on EditAdd External JARs and select the tools.jar file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

You need to indicate JAVA_HOME in mvn.ini (it's in the Maven folder /bin), and your problem will disappear.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Javicha
  • 3
  • 2
0

If you set the JAVA_HOME, and the problem persists try the following.

(I had the same problem, on a fresh installed PC.)

Assuming you are using Eclipse.

  • Open WindowPreferencesJavaInstalled jre's. Remove all you have there, add the desired you want to use (JDK, no JRE!)
  • In the same window open maven - installations, add the Maven installation you downloaded and extracted somewhere. (Do not use the embedded Maven installation. It has some known bugs.)

That's all. Your project will not suffer from such problems.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcell
  • 973
  • 1
  • 6
  • 13