How to Import MQTT Paho into Java 'playground'? - java

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.
Reproduce the Error Message in the Playground
After clicking the link to the code playground, click the 'run' button to reproduce the error.
Error Messages
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
QUESTIONS
Why is it that the resource can not be found?
How can the obstacle be overcome? (substitutes?)

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.

Related

What is the correct package name for importing MigLayout?

FYI, I'm new to Java development and Netbeans!
I have downloaded the MigLayout jar files, both core and for swing, and added their path in the NetBeans 8.2 IDE under 'Tools->Libraries->Library_Classpath'.
My problem is that I'm trying to use the API MigLayout() but every package name that I've tried to import it results in the error "package 'Package_Name' does not exist"
The following package names fail at compile time:
import MigLayout;
import net.miginfocom.MigLayout;
import net.miginfocom.swing.MigLayout;
The following package names only fail at run time:
import net.miginfocom.layout.Grid;
import net.miginfocom.swing;
import net.miginfocom.miglayout;
Links to the MigLayout forum are broken, and none of the tutorials I've seen have the import statements. All I am asking for is a link to or list of the packages needed to use MigLayout.
If MigLayout is no longer supported, it would be nice to know that as well!
Of all packages you mentioned, the following are actually working:
import net.miginfocom.swing.MigLayout;
import net.miginfocom.layout.Grid;
It is not enough to set the jar files in the library configuration of NetBeans (Tools > Libraries). You also need to set it inside your project to get your classpath working.
In the projects view, locate the directory called Libraries (JDK will be located there) and right click on it, then click on Add Library and choose the library where you defined your jar files, in your case Library_Classpath.

Android JDK 7 wildcard imports

I am experimenting with developing Phonegap plugins for Android using the Phonegap CLI and JDK 7. Time and time again I have found that wildcard imports do not work and I have to explicitly import each class that I need to use. A case in point. I am currently playing with using Rhino to run JS without a WebView. The code samples I find out there suggest import org.mozilla.javascript.* which starts throwing up compiler errors as soon as I put in code that uses the Rhino Context etc objects. In order to be able compile correctly I need to do a whole series of explicit imports. e.g.
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.NativeObject;
Without such imports I end up with error messages along the lines of
cannot find symbol
NativeObject result = (NativeObject)
function.call(rhino,scope,scope,functionParams);
symbol: class NativeObject
Is this a constraint imposed by Phonegap or something that got introduced with JDK 7 (I assume that most of the sample code I am depending on predates JDK 7) or something else altogether. I can see how the Context object in Android can create a conflict here but I am equally certain that I am not accidentally using other classes that define Scriptable, ScriptableObject etc.
I'd be most grateful to anyone who might be able to clarify.

Importing UsbSerialPort, Hexdump, SerialInputOutputManager in Android Studio

I'm trying to use the 'mik3y/usb-serial-for-android' library in order to read a serial connection. When trying to use the example code, I get errors because of certain things that will not import.
Here are the import statements that I'm having issues with(I've replaced my name and project info with dummy data):
import com.mycompany.johnsmith.driver.UsbSerialPort;
import com.mycompany.johnsmith.util.HexDump;
import com.mycompany.johnsmith.util.SerialInputOutputManager;
I receive the error: Cannot resolve symbol 'driver' for the first import, and Cannot resolve symbol 'util' for the other two.
I've installed the library as a dependency and all of my other imports work fine, but I'm still not sure if the issue is because of the library.

I want to use jersey client in my code I have imported the packages still it shows error

I want to use jersey client in my code. I have imported the packages still and it shows an error.
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
I am using JDev environment.
Despite that when I run my code it throws:
Error(3,33): package com.sun.jersey.api.client does not exist
import com.sun.jersey.api.client.WebResource;
Any suggestion why is it throwing the error ?
first of all you have to download the library e.g. from here. Afterwards you can follow this tutorial to add the library to your project. Jersey depends also on other libraries (included in the bundle), so make sure to add them as well.

Java library import difficulty

I'm trying to compile a small test program I have written for a raspberry pi.
The program makes use of the Pi4J library to control the piface add-on board.
What I have done so far is based on the following tutorial: http://www.savagehomeautomation.com/piface
While I can get the above example program to compile within my IDE after setting up the class paths I get compile errors with the one I have made and as far as I can tell the imports are set up in the same way.
It says that each of the following packages does not exist:
import java.io.IOException;
import com.pi4j.component.switches.SwitchListener;
import com.pi4j.component.switches.SwitchState;
import com.pi4j.component.switches.SwitchStateChangeEvent;
import com.pi4j.device.piface.PiFace;
import com.pi4j.device.piface.PiFaceLed;
import com.pi4j.device.piface.PiFaceRelay;
import com.pi4j.device.piface.PiFaceSwitch;
import com.pi4j.device.piface.impl.PiFaceDevice;
import com.pi4j.wiringpi.Spi;
I'm assuming that my problem is relatively simple but I don't really understand how import statements work and it's quite vague topic to search about. I have included some file paths if that helps.
This is where my project resides:
/home/pi/JBerries/relay
and this is where the pi4j library is:
/opt/pi4j
I hope the following image provides some of the information requested, note that the class paths are already set up:
You need to set the CLASSPATH environment variable to /opt/pi4j or the jar file therein. WIthout this the compiler is unable to know where your libary is located and will give you the errors you describe.
If you're using a project in JBerries you need to configure the classpath for the project - the screenshot shows the classpath for single-file compilations only. To edit the project config right-click the root node in the project window and select properties.

Categories

Resources