53

When I try to use ctrl+space this error is shown:

This compilation unit is not on the build path of a Java project.

I see that there are similar topics but my work environment is Eclipse and i pull my project from Git (I import project as general project) and i use Apache Ant. Can anyone help me?

Technext
  • 7,887
  • 9
  • 48
  • 76
  • Did you put that class' jar on build path in : `right click on project -> Properties -> Java Build Path -> Libraries` ? – Daniel Aug 20 '14 at 09:26
  • On section build path is text "no action available". So i cant enter in build path – Милош Керкез Aug 20 '14 at 09:28
  • Try to import the project as _Java_ _project_ instead of _general_ _project_. – Jack Aug 20 '14 at 09:35
  • 1
    When i try like that, there is no project to import, that is why i import as general project. Sorry if my English is not so good. – Милош Керкез Aug 20 '14 at 09:43
  • 1
    A note: not your case, but the same error mesage can also happen in cases where you have a multimodule project where there parent projec is only a mvn project. If you open the java file from there it will show this message. The solution is to simply open the same file from the module that has the java nature. – user1708042 Feb 17 '22 at 10:24

11 Answers11

77

Since you imported the project as a General Project, it does not have the java nature and that is the problem.

Add the below lines in the .project file of your workspace and refresh.

<natures>
      <nature>org.eclipse.jdt.core.javanature</nature>
</natures>
Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
  • @loganathan in my file i have these entries but still getting org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature – ImranRazaKhan May 08 '18 at 14:51
  • @ImranRazaKhan - This should work actually. If it didn't, please remove the project in your eclipse and import the project as a Java or Maven project. – Loganathan Mohanraj May 15 '18 at 13:29
  • this solution + on project properties -> project facets -> java version drop-down (just change and back to working version)..this solved issue in my eclipse (oxygen 2) – Akhil S Kamath Oct 01 '18 at 05:26
  • @LoganathanMohanraj I actually imported my project as existing maven project, I didn't see above code in .project file, but yea maven2Nature is present – Akki Jun 19 '19 at 08:17
  • where is this .project file?? – john k Jul 21 '20 at 15:19
30

Another alternative to Loganathan Mohanraj's solution (which effectively does the same, but from the GUI):

  1. Right-Click on your project
  2. Go to "Properties"
  3. Choose "Project Natures"
  4. Click on "Add"
  5. Choose "Java"
  6. Click "Apply and Close"
Pradeep Anchan
  • 1,469
  • 1
  • 11
  • 11
16

If it is a Maven project then sometimes re-importing of it helps:

  1. Right-click the project in the Project Explorer and choose Delete.
  2. File > Import... > Maven > Existing Maven Projects > Next > Root Directory > Browse your project from Disk.

Hope it will resolve the issue.

RAM237
  • 903
  • 11
  • 17
kumar
  • 441
  • 4
  • 9
  • 2
    As a general recommendation, try to do Maven->Update Project before re-importing, it's quicker and in my case fixed the problem – chomp Jul 27 '19 at 16:44
9

Go to Project-> right Click-> Select Properties -> project Facets -> modify the java version for your JDK version you are using.

5

If your project is imported as an existing maven project then --> Just right click on project and do maven update. It resolved my similar issue.

Chinmoy Kundu
  • 51
  • 1
  • 2
  • 1
    Welcome to SO. Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should vote it up once you have enough reputation – Rick Oct 16 '20 at 14:03
2

For those who still have problems after attempting the suggestions above: I solved the issue by updating the maven project.

Jero Dungog
  • 379
  • 1
  • 10
1

Add this to .project file

 <?xml version="1.0" encoding="UTF-8"?>
        <projectDescription>
            <name>framework</name>
            <comment></comment>
            <projects>
            </projects>
            <buildSpec>
                <buildCommand>
                    <name>org.eclipse.wst.common.project.facet.core.builder</name>
                    <arguments>
                    </arguments>
                </buildCommand>
                <buildCommand>
                    <name>org.eclipse.jdt.core.javabuilder</name>
                    <arguments>
                    </arguments>
                </buildCommand>
                <buildCommand>
                    <name>org.eclipse.m2e.core.maven2Builder</name>
                    <arguments>
                    </arguments>
                </buildCommand>
                <buildCommand>
                    <name>org.eclipse.wst.validation.validationbuilder</name>
                    <arguments>
                    </arguments>
                </buildCommand>
            </buildSpec>
            <natures>
                <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
                <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
                <nature>org.eclipse.jdt.core.javanature</nature>
                <nature>org.eclipse.m2e.core.maven2Nature</nature>
                <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
            </natures>
        </projectDescription>
JeremyW
  • 5,157
  • 6
  • 29
  • 30
Pavan
  • 11
  • 1
0

In my case, I have Eclipse Maven project. I had the same issue and I posted detailed explanation of the issue and answer here Eclipse Maven - Code Completion fails "This compilation unit is not on the build path of a Java project" and "Failed to Download Index" Error

pixel
  • 9,653
  • 16
  • 82
  • 149
0

Thought I'd add this one as a potential cause. In my case, I encountered this issue when performing a File search. When I opened the found file(s), I encountered this issue.

In my case, I have a nested Maven module in a POM project and when I opened the file it opened not the file in the module, but the file from the parent POM project. Simply reopening that file from the module project resolved the issue.

0

If you open any existing java file in Ctrl+Shift+R and try to write a code in that file, it will show that error message.

Please open the existing java file in Ctrl+Shift+T and write the code in that file, you will not get that error message.

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
Durga
  • 1
-1

Please remember only open 1 project in one eclipse window, otherwise eclipse gets confused. and this works for me.

Rebecca Li
  • 21
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34570203) – HiroCereal Jun 22 '23 at 19:18
  • but that is how I solve my problem. I am trying to help – Rebecca Li Sep 01 '23 at 18:02