Ahmad
Ahmad

Reputation: 9668

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.

Upvotes: 53

Views: 123046

Answers (13)

Lenymm
Lenymm

Reputation: 901

In my case there was an old .iml file in the module causing these problems. So if nothing else worked for you, try looking for one.

Upvotes: 0

David Pement
David Pement

Reputation: 111

If you've made it this far because rebuilding or invalidating the cache didn't work work you, I found that deleting the class and adding a new one with the same code worked.

Upvotes: 1

Meena Chaudhary
Meena Chaudhary

Reputation: 10695

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)

Upvotes: 133

Mohamed Safeuq J
Mohamed Safeuq J

Reputation: 51

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.

Upvotes: 2

Mevlüt Beder
Mevlüt Beder

Reputation: 107

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

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

Upvotes: 0

radzimir
radzimir

Reputation: 1368

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.

Upvotes: 0

Aleksei Vekovshinin
Aleksei Vekovshinin

Reputation: 1

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

Upvotes: 0

granadaCoder
granadaCoder

Reputation: 27904

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

Upvotes: 0

Yumi H
Yumi H

Reputation: 159

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

Upvotes: 7

Alexander S
Alexander S

Reputation: 41

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.

Upvotes: 4

Patrycja Cz
Patrycja Cz

Reputation: 231

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

Upvotes: 23

Arda &#199;.
Arda &#199;.

Reputation: 534

Rebuilding project worked for me.

Upvotes: 9

Ahmad
Ahmad

Reputation: 9668

The project contained several modules. While the library was added to the project libraries, some modules lacked it in their dependency part. So I solved the problem using the following steps in IntelliJ

Creating a module library and adding it to the module dependencies:

  1. Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S).
  2. In the left-hand pane of the dialog, select Modules.
  3. In the pane to the right, select the module of interest.
  4. In the right-hand part of the dialog, on the Module page, select the Dependencies tab.
  5. On the Dependencies tab, click + (on the top right) and select Jars or directories.
  6. In the dialog that opens, select the necessary files and folders. These may be individual .class and .java files, directories and archives
    (.jar and .zip) containing such files as well as directories with
    Java native libraries (.dll, .so or .jnilib).
  7. Click OK. If necessary, select the Export option and change the dependency scope.
  8. Click OK in the Project Structure dialog.

Upvotes: 25

Related Questions