Java无法访问类,找不到类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38549649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:24:44  来源:igfitidea点击:

Java cannot access class, class file not found

javaintellij-ideainterfacejaxb

提问by Ahmad

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

当我尝试在 IntelliJ 中创建一个项目时,我在这一行收到以下错误:

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

Sentenceis a class which implements the RootElementinterface

Sentence是一个实现接口的类RootElement

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 RootElementis an interface and not a class

所有包都存在,我可以跳转到每个接口或类的声明,但我不知道为什么 IntellJ 说它无法访问或找到它们?然而RootElement是一个接口而不是一个类

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

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

上述声明位于名为的 jar 文件中jaxb-rt-1.0-ea.jar,它存在于项目库管理器中。

采纳答案by Ahmad

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

该项目包含几个模块。虽然该库已添加到项目库中,但某些模块的依赖项部分缺少它。所以我在 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.
  1. 打开项目结构对话框(例如 Ctrl+Shift+Alt+S)。
  2. 在对话框的左侧窗格中,选择模块。
  3. 在右侧的窗格中,选择感兴趣的模块。
  4. 在对话框右侧的 Module? 页面上,选择 Dependencies? 选项卡。
  5. 在依赖项?选项卡上,单击 +(在右上角)?并选择 Jars 或目录。
  6. 在打开的对话框中,选择必要的文件和文件夹。这些可能是单独的 .class? 和 .java? 文件、目录和档案
    (.jar? 和 .zip),其中包含此类文件以及带有
    Java 本地库(.dll、.so? 或 .jnilib)的目录。
  7. 单击确定。如有必要,选择导出选项?并更改依赖范围。
  8. 单击“项目结构”对话框中的“确定”?

回答by Arda ?.

Rebuilding project worked for me.

重建项目对我有用。

回答by Meena Chaudhary

Try this

尝试这个

  1. Go to File
  2. Invalidate Caches/Restart
  3. You can choose only Invalidate and restart
  1. 转到文件
  2. 使缓存无效/重新启动
  3. 您只能选择无效并重新启动

回答by Patrycja Cz

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

File -> Invalidate Caches/ Restart 这对我长时间无效后有用

回答by Alexander S

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.

也可能是因为你在classpath中没有依赖,在依赖中用到了。
例如:您使用库 A,但是您从 A 使用的类具有库 B 的超类。但是您没有将 B 添加到类路径中。