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

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?

Upvotes: 54

Views: 198947

Answers (11)

Rebecca Li
Rebecca Li

Reputation: 19

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

Upvotes: -2

Durga
Durga

Reputation: 1

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.

Upvotes: 0

Uthanda
Uthanda

Reputation: 1

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.

Upvotes: 0

Chinmoy Kundu
Chinmoy Kundu

Reputation: 51

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.

Upvotes: 5

pixel
pixel

Reputation: 10577

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

Upvotes: 0

Jero Dungog
Jero Dungog

Reputation: 379

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

Upvotes: 2

Pavan
Pavan

Reputation: 11

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>

Upvotes: 1

kumar
kumar

Reputation: 451

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.

Upvotes: 16

Pradeep Anchan
Pradeep Anchan

Reputation: 1901

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"

Upvotes: 31

Shaleen Agarwal
Shaleen Agarwal

Reputation: 91

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

Upvotes: 9

Loganathan Mohanraj
Loganathan Mohanraj

Reputation: 1874

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>

Upvotes: 79

Related Questions