[英]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.