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
java -jar ...command. Couple of help link - stackoverflow.com/questions/574594/… & maven.apache.org/plugins/maven-shade-plugin/examples/…