【问题标题】:Error: Could not find or load main class [Intellij]错误:无法找到或加载主类 [Intellij]
【发布时间】:2018-11-24 07:14:10
【问题描述】:

我正在努力通过“运行”按钮在 IntelliJ 中运行单元测试或主要方法。 (项目是通过 maven 建立的)

这是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.andrew.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
    <java.version>1.8</java.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

这是我的项目目录:

├── my-app.iml
├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── andrew
│   │               ├── app
│   │               └── Hello.java
│   └── test
│       └── java
│           └── com
│               └── andrew
│                   ├── app
│                   └── HelloTest.java
└── target
    ├── classes
    │   └── com
    │       └── andrew
    │           └── Hello.class
    ├── generated-sources
    │   └── annotations
    └── maven-status
        └── maven-compiler-plugin
            └── compile
                └── default-compile
                    ├── createdFiles.lst
                    └── inputFiles.lst

我尝试了以下方法:

  • 将项目的编译器输出设置为{project directory}/target
  • 使缓存无效并重新启动、重建
  • 在 ./target/classes 中运行 java com.andrew.Hello 返回 Hello World
  • 设置./src/main/java为源

但我仍然无法解决问题。

【问题讨论】:

    标签: java maven intellij-idea


    【解决方案1】:

    就我而言,我在 IntelliJ Run - Edit Configuration 中添加了一个 Maven 运行命令,并将命令行参数设置为 clean install spring-boot:run,它在 Mac 终端中作为运行命令 ./mvnw clean install spring-boot:run 工作。

    【讨论】:

      【解决方案2】:

      您需要在这里确定几件事....

      检查以确保您的所有测试方法都带有 {@Test} 注释 - 请参阅我添加的下图。

      一旦它们都被注释了,您应该能够右键单击该文件并选择运行测试,在您执行此操作之后 - 运行按钮会自动将最新的运行配置添加为您运行的最后一项,所以它应该显示在那里的“运行”按钮

      【讨论】:

        【解决方案3】:

        解决方案

        在项目结构 -> 编译器输出中,我已经声明了 /home/andrew/IdeaProjects/Maven:The-Definitive-Guide/my-app/classes

        我要改成:/home/andrew/IdeaProjects/Maven_The_Definitive_Guide/my-app/classes

        【讨论】:

        • 谢谢,在我的情况下输出路径错误,你给了我正确的提示。
        【解决方案4】:

        在项目视图中右键单击 Hello ->“运行 Hello.main()”应该适合你。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-07
        • 2019-07-16
        • 2015-12-22
        • 2019-04-20
        • 1970-01-01
        • 2021-12-10
        • 2018-07-21
        • 1970-01-01
        相关资源
        最近更新 更多