错误:找不到或加载主类...-Eclipse之外的Maven项目 [英] Error: Could not find or load main class ... - Maven project out of Eclipse

查看:64
本文介绍了错误:找不到或加载主类...-Eclipse之外的Maven项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse创建了一个Maven项目,并且能够在Eclipse中运行 App.java .但是现在我想使用以下命令在Eclipse中运行它:

I created a Maven project with Eclipse and I am able to run the App.java in Eclipse. But now I want to run it out of Eclipse with this command:

..\workspace\subscribe\target\classes> java com.mqtt.subscribe.App 

,带有可执行文件 jar :

..\workspace\subscribe>java -jar target\subscribe-0.0.1-SNAPSHOT.jar

并收到此错误:

错误:找不到或加载主类com.mqtt.subscribe.App

Error: Could not find or load main class com.mqtt.subscribe.App

我做错了什么?我不明白.

What I am doing wrong? I don't understand it.

我还尝试使用 cmd 中的 mvn软件包 mvn clean install 重建项目.我总是得到相同的错误.

I also try to rebuild the project with mvn package and mvn clean install in cmd. I get always the same error.

我还尝试制作一个可执行文件 jar 文件,仍然是同样的错误.

I also tried to make an executable jar file, Still the same error.

谢谢您的帮助.

系统变量设置如下:

JAVA_HOME = C:\Program Files\Java\jdk1.8.0_121
Path = C:\Program Files\Java\jdk1.8.0_121\bin;C:\Program Files\Maven\apache-maven-3.3.9\bin

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mqtt</groupId>
  <artifactId>subscribe</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>subscribe</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.mqtt.subscribe.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
        <id>Eclipse Paho Repo</id>
        <url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>1.0.2</version>
    </dependency>
  </dependencies>

</project>

App.java

package com.mqtt.subscribe;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class App implements MqttCallback {

    public static void main(String[] args) {
        String topic = "TEST";
        int qos = 2;
        String broker = "tcp://broker.hivemq.com:1883";
        String clientId = "Test";
        MemoryPersistence persistence = new MemoryPersistence();

        try {

            MqttAsyncClient sampleClient = new MqttAsyncClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            sampleClient.setCallback(new App());
            System.out.println("Connecting to broker: " + broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            Thread.sleep(500);
            sampleClient.subscribe(topic, qos);
            System.out.println("Subscribed");
        } catch (Exception me) {
            if (me instanceof MqttException) {
                System.out.println("reason " + ((MqttException) me).getReasonCode());
            }
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }

    public void connectionLost(Throwable arg0) {
        System.err.println("connection lost");

    }

    public void deliveryComplete(IMqttDeliveryToken arg0) {
        System.err.println("delivery complete");
    }

    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println("topic: " + topic);
        System.out.println("message: " + new String(message.getPayload()));
    }  
}

CMD :

C:\Users\test\workspace\subscribe>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building subscribe 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ subscribe ---
[INFO] Deleting C:\Users\test\workspace\subscribe\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ subscribe
---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\test\workspace\subscribe
\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ subscribe ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\test\workspace\subscribe\target\c
lasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ su
bscribe ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\test\workspace\subscribe
\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ subscri
be ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ subscribe ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.0:jar (default-jar) @ subscribe ---
[INFO] Building jar: C:\Users\test\workspace\subscribe\target\subscribe-0.0.
1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ subscribe ---
[INFO] Installing C:\Users\test\workspace\subscribe\target\subscribe-0.0.1-S
NAPSHOT.jar to C:\Users\test\.m2\repository\com\mqtt\subscribe\0.0.1-SNAPSHO
T\subscribe-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\test\workspace\subscribe\pom.xml to C:\Users\test\.m2\repository\com\mqtt\subscribe\0.0.1-SNAPSHOT\subscribe-0.0.1-SNAPSHOT.p
om
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.049 s
[INFO] Finished at: 2017-02-26T17:50:53+01:00
[INFO] Final Memory: 15M/212M
[INFO] ------------------------------------------------------------------------

C:\Users\test\workspace\subscribe>java -jar target\subscribe-0.0.1-SNAPSHOT.
jar
Error: Could not find or load main class com.mqtt.subscribe.App

推荐答案

您的主要问题来自pom.xml的build部分,因为 assembly插件不知道您的主要类是什么,您也忘记指出应该运行程序集插件的阶段(请参阅第二个解决方案以解决此问题),第一个解决方案是摆脱程序集插件并使用下面的内容(改编主类的名称).第二种解决方案是在此代码段之后:

Your main issue comes from the build section of your pom.xml, because the assembly plugin does not know what is your main class, and you also forgot to indicate the phase in which the assembly plugin should run (see the 2nd solution to fix this) the first solution is get rid of the assembly plugin and use what is below (adapt the name of the main class). The second solution is after this code snippet:

<build>
  <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
<!--                Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.test.whatever.pomtester.App</mainClass>
                        </manifest>
                    </archive>

                </configuration>
            </plugin>
    </plugins>
  </build>

另一种解决方案是仅使用Assembly插件,如下所示(与之前修改主类的名称一样):

Another solution is to only use the assembly plugin, with something like below (as before adapt the name of the main class):

<build>
        <plugins>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.test.whatever.pomtester.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>

                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

这篇关于错误:找不到或加载主类...-Eclipse之外的Maven项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆