[eclipse] Eclipse "this compilation unit is not on the build path of a java project"

I can't get autocompletion to work on Eclipse.

I'm working on the project on svn. I set up project in Eclipse by going into

File -> Import -> Checkout As a Project -> New Project Wizard.

I choose Enterprise Java Application and it's being downloaded. And everything seems to work fine except instead autocompletion working as I expect I get a popup dialog displaying the message

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

I've Googled it but everyone says that project must be Java project. But hey it is! What is the problem?

Update

The catalog structure on svn looks like this:

-Project_name
  -application
     -META-INF
       application.xml
       MANIFEST.MF
  +build
  +db
  +deploy
  +dist
  +lib
  +properties
  +script
  -src
     -META-INF
        someother.xml (datasource info)
        persistence.xml
        folder hierarchy with source files (should be package)
  -web
     some folders
          .
          .
     files
       .
       .
     -WEB-INF
        faces-config.xml
        jboss-web.xml
        web.xml
     build_win.xml

How do I tell Eclipse where the source files folder, application.xml and other configuration xml files are?

This question is related to eclipse svn

The answer is


If you're a beginner (like me), the solution may be as simple as making sure the parent folder that the file you're working in is a Java project.

I made the mistake of simply creating a Java folder, then creating a file in the folder and expecting it to work. The file needs to live in a project if you want to avoid this error.


This is what was missing in my .project file:

    <projectDescription>
        ...
        <buildSpec>
            <buildCommand>
                <name>org.eclipse.jdt.core.javabuilder</name>
                <arguments>
                </arguments>
            </buildCommand>
        </buildspec>
        ...
        ...
        ...
        <natures>
            <nature>org.eclipse.jdt.core.javanature</nature>
            <nature>org.eclipse.m2e.core.maven2Nature</nature>
        </natures>
        ...
    </projectDescription>

I might be picking up the wrong things from so many comments, but if you are using Maven, then are you doing the usual command prompt build and clean?

Go to cmd, navigate to your workspace (usually c:/workspace). Then run "mvn clean install -DskipTests"

After that run "mvn eclipse:eclipse eclipse:clean" (don't need to worry about piping).

Also, do you have any module dependencies in your projects?

If so, try removing the dependencies clicking apply, then readding the dependencies. As this can set eclipse right when it get's confused with buildpath sometimes.

Hope this helps!


I have this issue from time-to-time and often it's because the project wasn't marked as a java project. You can change this by going to the properties for the project > Project Facets > and selecting java. You may then need to properly configure that project, but this is probably part of the problem


  1. Delete your project (DO NOT DELETE THE ORIGIN FILE IN THE FILE SYSTEM)
  2. Right click at Package explorer area and right click import.
  3. Go to Maven folder and hit import an existing maven project.

Hope it helps


Like the message says, is the file somewhere on the project's Java Build Path (e.g. a Source folder)?


I also had this problem after converting my java project into a maven project. I solved it as follows:

Right click on your project ->Project Facets -> click on Java, then Apply.


Had the same problem (but with Maven). The reason was incorrect choice of executor: my project used global settings that are not avilable from Embedded installation of Maven. Changed it to external (Window -> Preferences -> Maven -> Installations) and that fixed the problem.


Your source files should be in a structure with a 'package' icon in the Package Explorer view (in the menu under Window > Show View > Package Explorer or press Ctrl+3 and type pack), like this:

Java project in Eclipse

If they are not, select the folder containing your root package (src in the image above) and select Use as Source Folder from the context menu (right click).


For example if there are 4 project and a root project, add the other child projects to build path of root project. If there is not selection of build path, add below codes to .project file.

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>rootProject</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>

Run "mvn eclipse:eclipse" from terminal.


I did copy the .classpath and .project from another project and adjusted the values properly.

Close the project before editing those files, when you are sure they reflect the reality (your reality anyway), re-open the project in Eclipse.

The workspace is rebuilt and all should work from then on.


What worked for me was copping the .settings/ directory from another project.


You may want to try running eclipse with the -clean startup option - it tries re-building eclipse's metadata of the workspace.


Did you have your .project file in your folders?

I got the same problem. Than i realized that I didn't have the .project file.


What i did is after importing the project from svn , deleted the project from workspace and imported it as a maven project from local . Then in preferences->maven->usersettings->in usersettings box gave path of settings.xml which will be in apache maven folder-> conf->settings.xml

and it solved the issue for me.


in my case it's a maven project

delete the project from eclipse leaving the sources close eclipse delete from filesystem

.target/ .classpath .project .settings/ open eclipse Again Import Maven Projects

This solved the problem


When you have a multimodules maven project under a parent project, make sure you're not editing the file in the maven parent project.


mvn eclipse:eclipse

solved my problem


Here the steps for creating a source folder in eclipse.

  1. Right click on the project and go to properties
  2. Select "Java Build Path" from the properties dialog box
  3. Select the source tab and check that the source folders are correct if not click on the remove button to remove source folders, or the add button to add source folders.

You can control the order in which source folders appear in a project on order and export tab on the configure build path option.


What I did to make one of my projects to check out properly is by

1) Import your project from svn

      file-->import-->SVN-Checkout Projects From SVN

2) Find your Project and then in the "Check Out As" dialogue make sure you have the radio button selected "Check out as a project configured using the New Project Wizard"

3) Go through regular steps.

The wizard pulls the project properly and then setups your eclipse....

without using the wizard I find that all hell breaks loose.....

Hope this helps...


I found that I was getting this error due to having my files, including my main class, outside of the .src folder.


Had the same problem. Solution: Context menu -> Maven -> Enable dependency management

Do not know why that was lost, when checking out.


I solved this by adding projects in Java Build Path

Right click on the project -> properties -> java build path -> Add

enter image description here


I had same issue after importing maven project consisting of nested micro services. This is how I got it resolved in Eclipse:

  1. Right Click on Project
  2. Select Configure
  3. Select "Configure and Detect Nested Projects"