12

I am using maven project in Eclipse, where I am not able to access classes under src/main/java in src/test/java class. I could use the classes in maven dependacies jars though. What am I missing ?

5 Answers 5

19

I have found that sometimes this error appears and by simply going Project -> Clean... the error will stop.

However if this does not work there is a blog with some more suggestions here

Updated 11/07/2017

The original link is no longer available, refer to web archive here

Or just have them all here..

  • ‘Clean’ Your Eclipse Project: Go to Project > Clean in Eclipse [This seems to work for me]
  • Refresh your project folder (right click on your project > refresh)
  • Re-build your project
  • Clean your builds (If using Ant or Maven – clean your builds)
  • Recreate your project in Eclipse
  • ‘Switch’ Workspace – then Switch back (Eg Change to Debug, then switch back to Java)
  • Remove and re-add your JRE:
    1. Right Click on your project > properties
    2. Click on the Libraries tab
    3. Click on the JRE
    4. Click remove, then OK
    5. Repeat 1-3 again, but add the JRE again
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I couldnt believe this worked. I had done a maven clean and rebuilt the dependancies and it did not work. An eclipse clean worked :o
I'm using eclipse 2019 - 12: switching Workspace then switching back did the trick for me. Thumbs Up.
1

You just need to import them. Eclipse should help you with this: if you try to use the class names, it'll suggest where to import from.

The test classes are a separate package, so the classes will need to be declared public.

3 Comments

Thats exactly my question, I cannot import them. The import line shows class cannot be resolved
And they're part of the same project? Are the classes public?
Yes part of same project and are public classes too.
0

Absolutely, one can do it..

First You need to navigate to Build Path-->Configure Build Path--> Source tab

then in the Source tab search/check [your Project Name]/src/main/java and change

"contains test sources" from No to Yes and save it.

This will resolved issue of import packages from "src/test/java" to "src/main/java" successfully

Comments

0

In my case, it was happening because my IntelliJ Idea was configured for a different JDK version and my terminal was referring to a different JDK version. Due to this whenever I run Maven clean from the terminal, and then try to run tests via IDE, it gives this error.

Comments

-1

Project -> Clean

is enough for me to work

1 Comment

Welcome to StackOverflow! This question has already been answered and accepted, no need to add the same answer again.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.