12

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.

1
  • Does the CarService class belong to the package my.demo.service, and not just the folder? Apr 7, 2011 at 10:25

10 Answers 10

34

u just need go to Project ---> clean See http://philip.yurchuk.com/2008/12/03/eclipse-cannot-be-resolved-to-a-type-error/

1
  • right clicking the project and then clicking on Build Project solved my problem....
    – Grim
    Jun 10, 2016 at 18:43
3

You probably missed package declaration

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

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:)

1
  • sorry, i didn't get the solution , can you illustrate how can i solve it ?
    – user1
    Sep 8, 2015 at 4:17
0

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.

1
  • Saving a file will perform a new compilation by Eclipse. That's why your errors were magically resolved! Apr 26, 2016 at 9:32
0

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

0

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.

0

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>
0

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.
0

Verifying the package and classes location will help. Usually, classes that are being used should be in same package or you need to import them in referenced class.

if that does not help then update build path and run clean is an option.

1
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Aug 29, 2021 at 4:03
-1

Project > Clean

this one did it for me

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.