1

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
Gerold Broser
  • 11,355
  • 4
  • 36
  • 85
Chris
  • 21
  • 1
  • 5
  • 1
    1. Read (and post) the stack trace: it contains the reason why your program won't run. 2. Your class depends on the org.eclipse.paho... library, but you're not specifying it in the classpath when running your class, so the classes it contains, and that your app needs, can't possibly be loaded. Read your introductory Java book to understand what the classpath is, what needs to be in the classpath, and how to specify it when starting a program. – JB Nizet Feb 26 '17 at 15:53
  • I would suggest you create executable jar and run with `java -jar ...` command. Couple of help link - http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven & https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html – Subhrajyoti Majumder Feb 26 '17 at 15:55
  • Thanks for your answers, I edited my answer with the stack trace and the executable jar. Still the same problem. – Chris Feb 26 '17 at 16:57
  • No. You haven't posted any stack trace. Read http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – JB Nizet Feb 26 '17 at 16:59
  • 1
    Possible duplicate of [How can I create an executable JAR with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Adonis Feb 26 '17 at 20:58

1 Answers1

3

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>
Adonis
  • 3,984
  • 3
  • 31
  • 47
  • Very nice. I tried to get rid of the assembly plugin and took instead the dependency plugin (first) solution and it worked!! Thank you for your help. – Chris Feb 27 '17 at 06:41
  • Guys I'm facing the same issue, but I don't understand very well how can to get rid of the dependency plugin. I would like to post my POM.XML to get help if it's possible. thanks – Andres Urrego Angel Nov 03 '17 at 12:26
  • @Adonis: Could you provide the run commands for the command lines for both options? That would be very helpful for beginners. – Janothan Jul 04 '18 at 19:29