在Intellij中如何将一个类添加到类路径中?

huangapple go评论58阅读模式
英文:

How to add a class on classpath in Intellij?

问题

我在Intellij中有以下的项目结构:

当我运行名为GenerateGraph的类时,出现了以下错误:

错误: 找不到或无法加载主类graph.GenerateGraph
原因: java.lang.ClassNotFoundException: graph.GenerateGraph

我不明白为什么会发生这种情况,这是我第一次遇到这种情况。当我进入“项目结构”时,看到以下内容:

运行配置:

似乎它甚至没有看到graph包中有一个类。有人可以帮助我理解发生了什么吗?是因为我导入项目后重命名了包吗?非常感谢您提前的帮助。

英文:

I have the following project structure in Intellij:

在Intellij中如何将一个类添加到类路径中?

I am actually working with a maven project and all I wanna do is just run the project. Unfortunatelly I dont understand why but when I run the class called GenerateGraph it shows me the following error:

Error: Could not find or load main class graph.GenerateGraph
Caused by: java.lang.ClassNotFoundException: graph.GenerateGraph

I dont understand why this happens- it is the first time I encounter something like that. When I go on project structure I see the following:

在Intellij中如何将一个类添加到类路径中?

Run configuration:
在Intellij中如何将一个类添加到类路径中?

seems like it doesnt even see that the graph package has a class inside. Could someone maybe help me understand what has happened? Is it because I renamed the package after importing the project? Thank you very much in advance

答案1

得分: 2

您的项目设置不正确。

您将reverseGraph标记为源根。在这种情况下,src是源文件夹。

典型的Maven项目结构是src/main/java用于非测试源文件和src/test/java用于测试。

我建议的做法是修改您的文件夹结构:

  1. 删除reverseGraph
  2. src移动到项目的根目录。
  3. 添加一个src/main/java文件夹,并将您的graph文件夹移入其中。
  4. 在IntelliJ中重新导入Maven项目,它应该会自动标记src/main中的java文件夹为源根。
英文:

Your project is not setup correctly.

You have reverseGraph marked as the sources root. In this case, src is the source folder.

The typical Maven project structure is src/main/java for non-test source files and src/test/java for tests.

What I'd recommend is modifying your folder structure:

  1. Remove reverseGraph
  2. Move src to the root of the project.
  3. Add a src/main/java folder, and move your graph folder into that.
  4. Reimport the Maven project in IntelliJ, and it should automatically flag the java folder in src/main as the sources root.

答案2

得分: 0

最有可能是依赖项没有正确设置 - 因为您没有在IDE中将此项目打开为Maven项目。请将此项目导入为Maven项目,参见IntelliJ IDEA文档中的“打开现有的Maven项目”部分。

英文:

Most likely the dependencies are not correctly set up - because you do not have this project opened as Maven project in IDE. Please import this project as Maven project, see steps from Open an existing Maven project IntelliJ IDEA documentation section.

huangapple
  • 本文由 发表于 2020年8月4日 01:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63234072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定