[java] Why I got " cannot be resolved to a type" error?

I am developing a dynamic web project (RESTful jersey) under Eclipse IDE.

Under src/my/demo/service folder I have CarService.java class

public class CarService {
  ...
}

Under src/my/demo/controller folder I have UserController.java class

import my.demo.service.CarService;

public class UserController{
    private CarService carService; //ERROR: CarServcie cannot be resolved to a type

}

I do have imported the CarService, why eclipse give me the error "CarServcie cannot be resolved to a type" in my UserController.java?

--------Edit------------------

I found the cause: for some reason, my.demo.service has the same level as src/ in eclise project explorer view. After I move my.demo.service under src/, it is fine. Seems I should not create new package in "Project Explorer" view in Eclipse...

But thank you for your response:)

This question is related to java eclipse

The answer is


I had wrong package names:

main.java.hello and main.test.hello rather than com.blabla.hello.

  1. I renamed my src-folder to src/main/java and created another src-folder src/test/java.
  2. I renamed my package within src/main/java to com.blabla.hello
  3. I created another package with the same name in src/test/java.

In my case the missing type was referencing an import for java class in a dependent jar. For some reason my project file was missing the javabuilder and therefore couldnt resolve the path to the import.

Why it was missing in the first place I don't know, but adding these lines in fixed the error.

<buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
</buildCommand>

I had my own instance of this error, and in my case none of the above solutions resolved the "cannot be resolved to a type" error by themselves, although they were necessary steps toward doing so. I found something silly that did though.

This seemed to be due a bug in Eclipse (Luna Service Release 1a (4.4.1) in my case). In the file where you're seeing the error, try saving after making and then undoing a trivial change (e.g. deleting one character and then typing it back in). For some reason this caused all my class references to resolve.


I had this problem while the other class (CarService) was still empty, no methods, nothing. When it had methods and variables, the error was gone.


Maybe wrong path..? Check your .classpath file.


You probably missed package declaration

package my.demo.service;
public class CarService {
  ...
}


for some reason, my.demo.service has the same level as src/ in eclise project explorer view. After I move my.demo.service under src/, it is fine. Seems I should not create new package in "Project Explorer" view in Eclipse...

But thank you for your response:)