英文:
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:
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:
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
用于测试。
我建议的做法是修改您的文件夹结构:
- 删除
reverseGraph
。 - 将
src
移动到项目的根目录。 - 添加一个
src/main/java
文件夹,并将您的graph
文件夹移入其中。 - 在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:
- Remove
reverseGraph
- Move
src
to the root of the project. - Add a
src/main/java
folder, and move yourgraph
folder into that. - Reimport the Maven project in IntelliJ, and it should automatically flag the
java
folder insrc/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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论