简体   繁体   English

尝试从命令行运行时,Maven 找不到主 class

[英]Maven cannot find main class when trying to run from command line

I am trying to run a Maven package from the command line, but every time I go to build it, or run it afterwards, I get:我正在尝试从命令行运行 Maven package ,但每次我 go 构建它,或者之后运行它,我得到:

Error: Could not find or load main class utility.Main
Caused by: java.lang.ClassNotFoundException: utility.Main

I've searched through multiple SO posts, tried changing things around in pom.xml, but nothing seems to work.我搜索了多个 SO 帖子,尝试在 pom.xml 中进行更改,但似乎没有任何效果。 It does work from an IDE, but not the CLI.它在 IDE 上工作,但不是 CLI。

Here is the build part of my pom.xml:这是我的 pom.xml 的构建部分:

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>        
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>utility.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>     
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

What exactly am I doing wrong?我到底做错了什么?

Update with requested info:更新请求的信息:

package utility;

import data.UMLEnvironment;

public class Main {

    public static void main(String[] args) {
        run();
    }

  public static void run() {
        UMLEnvironment env = new UMLEnvironment();
        Console console = new Console(env);
        console.run();
    }

}

And I do see "Main.class" being saved in target/classes/utility.而且我确实看到“Main.class”保存在目标/类/实用程序中。 Layout is:布局是:

src
-main
--java
---utility
----Main.java
----Otherstuff.Java
---data
----MoreStuff.java
---config
----AndALittleMoreStuff.java

Edit: I figured out the fix.编辑:我想出了解决办法。 I was trying to run it via java -cp target/mmouse-uml-0.0.2-SNAPSHOT.jar:target/mmouse-uml-0.0.2-SNAPSHOT-jar-with-dependencies.jar utility.Main when I really just needed to run it like this: java -cp target/mmouse-uml-0.0.1-SNAPSHOT-jar-with-dependencies.jar utility.Main . I was trying to run it via java -cp target/mmouse-uml-0.0.2-SNAPSHOT.jar:target/mmouse-uml-0.0.2-SNAPSHOT-jar-with-dependencies.jar utility.Main when I really just需要像这样运行它: java -cp target/mmouse-uml-0.0.1-SNAPSHOT-jar-with-dependencies.jar utility.Main

I figured out the fix.我想出了解决办法。

I was trying to run it via java -cp target/mmouse-uml-0.0.2-SNAPSHOT.jar:target/mmouse-uml-0.0.2-SNAPSHOT-jar-with-dependencies.jar utility.Main when I really just needed to run it like this: java -cp target/mmouse-uml-0.0.1-SNAPSHOT-jar-with-dependencies.jar utility.Main . I was trying to run it via java -cp target/mmouse-uml-0.0.2-SNAPSHOT.jar:target/mmouse-uml-0.0.2-SNAPSHOT-jar-with-dependencies.jar utility.Main when I really just需要像这样运行它: java -cp target/mmouse-uml-0.0.1-SNAPSHOT-jar-with-dependencies.jar utility.Main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在命令行上运行时,Java“无法找到或加载主类” - Java “cannot find or load main class” when trying to run on command line 尝试从命令行运行应用程序时出错:找不到 class - error when trying to run application from command line: cannot find class 从命令promt运行时,maven未运行主类 - maven is not run main class when run from command promt 从命令行运行Java-无法找到主类 - Run Java from the command line — unable to find main class 当我从命令行运行 Main 时,Java 在同一个 package 中找不到 class - Java can't find class in same package when I run Main from command line 尝试从main方法运行TestNG时,无法在类路径中找到类 - Cannot find class in classpath when trying to run TestNG from the main method 在Windows上尝试使用classpath(用于其他lib)从命令行运行jar文件会引发“找不到或加载主类” - Trying to run jar file from command line using classpath(for other libs) raises “Could not find or load main class”, on Windows Java从命令行运行jar:找不到或加载主类com.test.Main时出错 - Java run jar from command line: Error could not find or load main class com.test.Main 错误:从Linux命令行运行时找不到或加载主类Main- - Error: Could not find or load main class Main - when running from Linux command line 无法从命令行运行Java类 - Cannot run java class from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM