Error: Could not find or load main class … - Maven project out of Eclipse

随声附和 提交于 2021-01-28 06:11:30

问题


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 

EDIT: with executable jar:

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

and get this error:

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

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

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

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

Thank you for your help.

System variables set like:

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

回答1:


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>

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>


来源:https://stackoverflow.com/questions/42470641/error-could-not-find-or-load-main-class-maven-project-out-of-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!