Reputation: 1537
GOAL: Java to publish an MQTT message through a code playground console. The playground is used to prove out functionality before transplanting instructions to Android Studio.
After clicking the link to the code playground, click the 'run' button to reproduce the error.
Why does adding the import statements in the code playground?:
import org.eclipse.paho.client.mqttv3.MqttClient;
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;
cause a failure:
./Playground/Playground.java:4: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttClient;
^
./Playground/Playground.java:5: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
^
./Playground/Playground.java:6: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttException;
^
./Playground/Playground.java:7: error: package org.eclipse.paho.client.mqttv3 does not exist
import org.eclipse.paho.client.mqttv3.MqttMessage;
^
./Playground/Playground.java:8: error: package org.eclipse.paho.client.mqttv3.persist does not exist
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
^
5 errors
Upvotes: 0
Views: 906
Reputation: 59731
tl;dr - this probably will never work
The Paho Java library is not included with the default classes in the Java SDK. It is what is known as a third-party library.
It ships as a jar file that you need to add to the classpath of any Java runtime you want to use it with.
There are hundreds of thousands such libraries, each offering different extra functionality that extends the default set of standard classes and also available in different release versions.
There is no way the administrators of the code playground could know in advance which of those libraries a user might want to try and just including an import statement at the top of the class doesn't fully identify which version of the library you mean.
While systems like Maven provide a way to look up and download some these libraries in a standard way it's still not suitable for this type of environment and doesn't cover every library you might want.
Also even if the playground did have a way to specify third-party libraries this would open up a huge security problem, because they would have no control over the code that would now run on their machines. I expect the snippets are already run under a security manager that prevents access to the internet and local file system. This would prevent you from being able to connect to the broker.
Upvotes: 2