'Java cannot access class, class file not found

When I try to make a project in IntelliJ I receive the following error on this line:

Sentence sent = new Sentence();
 sent.emptySegments();

Error:

Error:(151, 10) java: cannot access javax.xml.bind.RootElement
class file for javax.xml.bind.RootElement not found

Sentence is a class which implements the RootElement interface

import javax.xml.bind.RootElement;
...
public class Sentence extends MarshallableRootElement implements RootElement   {

All packages exist and I can jump to declaration of each interface or class but I don't know why IntellJ says it cannot access or find them? However RootElement is an interface and not a class

public interface RootElement extends Element {
    void validate() throws StructureValidationException;
}

The above declaration is in a jar file named jaxb-rt-1.0-ea.jar and it exists in the Project librarians.



Solution 1:[1]

Try this

  1. Go to File
  2. Invalidate Caches/Restart
  3. You can choose only Invalidate and restart

(See Invalidate caches on IntelliJ's manual)

Solution 2:[2]

File -> Invalidate Caches/ Restart this worked for my after long hours of effectiveless

Solution 3:[3]

Rebuilding project worked for me.

Solution 4:[4]

Deleting the .idea folder and then running Invalidate Caches/Restart worked for me.

Solution 5:[5]

It also may be because you don't have dependencies in classpath, which used in dependencies.
For example: you use library A, but class you're using from A has superclass from library B. But you didn't add B to classpath.

Solution 6:[6]

If it is a single file, you can try deleting the file and undoing it. It seems to reindex that particular file alone, which is much faster than Invalidate Caches/Restart. As a precautionary measure, you can take a backup of the file before deleting, just in case if something goes awry.

Another reason might be different versions of same library with more/less methods. This happened for me with Gradle. Sometimes it compiles fine and sometimes, it doesn't. Just find and remove the unnecessary ones.

Solution 7:[7]

My Gradle/IntelliJ "big hammer"

(Optional, but preferred). Close all instances of IntelliJ or any other Java IDE.

delete the ".idea" folder (<< intellij specific, or whatever "workspace" folder your IDE uses)

..............

./gradlew --stop       
      OR
gradle --stop

(now delete the folders)

rm -rf $HOME/.gradle/caches/

rm -rf $HOME/.gradle/build-cache-tmp/

(now resume normal gradlew commands like:)

./gradlew clean build

Solution 8:[8]

For me just worked, turn off windows defender / add exclusion project folder / idea process.

Solution 9:[9]

Similar problem can happen if a library is imported with maven scope runtime.

In such case it isn't accessible by your classes located under src/main/java.

Only classes in src/test/java can directly use runtime dependencies.

Solution 10:[10]

I removed this location "amazonaws" file and clean install later run

/Users/testuser/.m2/repository/com/amazonaws