Language - Java: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
(270 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[File:icon-Java.png|64x64px]] Preamble about the language's origin and its main characteristics.
[[Category:Language]]
__NOTOC__


==Support==
We provide support for the Java language in all major operating systems. We also provide instructions on how to get your project started in a number of common development environments. Select your operating system and preferred development environment below, and follow the instructions to get your project running with Phidgets.
Java has a complete API and code samples for all Phidgets devices.


For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
If you do not know which development environment you want to use, or your development environment of choice is not listed, we recommend starting with javac as the simplest path to getting your code running.


* Our honest opinion on how well this language is suited to controlling Phidgets. If it is a poor choice, suggest and link similar (better) languages.
Once you have set up your development environment to run with Phidgets, we recommend you follow our guide on [[Phidget Programming Basics]]. The guide will showcase the fundamentals of programming with Phidgets, with examples in Java.
* In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.


==Development Environments and Compilers==
==Choose Your Development Environment:==


Describe each major compiler and notable differences or important information. (eg. framework versions) If there are known issues/workarounds mention them and link to the corresponding issue at the bottom of the page.
{{Language_-_Java_Dev_Environment_Table}}


==Quick Downloads==
== Quick Downloads ==


''For using Java in Android, please refer to the [[Language - Android Java]] page.''
If you already know what you're doing and just need the files, you can find them all below.


Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.
=== Documentation ===
Visit the drivers section at www.phidgets.com and get the latest:
* [http://www.phidgets.com/drivers.php Phidget Framework]
* [http://www.phidgets.com/downloads/libraries/phidget21jar_2.1.8.20110615.zip Phidget21.jar]
You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:
* [http://www.phidgets.com/documentation/JavaDoc.zip API Manual]
* [http://www.phidgets.com/documentation/web/javadoc/index.html API Reference]
* [http://www.phidgets.com/downloads/examples/JavaJNI_2.1.8.20110615.zip Java Sample Code]
* You can find a high level discussion about programming with Phidgets in general on the [[General API]] page.
* The [[Device Functionality]] page explains the general operational information for your device.


You may want to have these pages open while working through these instructions.
*{{Phidget22API}}  (select Java from the drop-down menu)


==Getting Started (Windows, Linux, and Mac OS)==
=== Example Code ===


The Phidget full examples were written using NetBeans and this tutorial assumes its use.
*{{SampleCode|Java|Java Examples}}
Other environments such as Eclipse work as well and would be set up in a similar manner.
For Java command line compilers, include the phidget21.jar during compilation.
For example, you can use “javac -classpath phidget21.jar MyPhidgetProgram.java” from the Java SDK.
In NetBeans, the Phidget .jar library can be added to the project from the project explorer window.
Simply right click the “Libraries” item in the project explorer and then select “Add JAR/Folder”.
Navigate to the location where the phidget21.jar was extracted and then add it to the project.
You are now ready to begin coding with Phidgets.


===Coding For Your Phidget===
===Libraries===


Before you can use the Phidget, you must include a reference in the code to the library. In Java:
{{AllQuickDownloads}}
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  import com.phidgets.*;
  import com.phidgets.event.*;
 
</source>
</font>
</div>
 
Now in the main body of code, the Phidget object will need to be declared.
For example, we can declare a PhidgetInterfaceKit with:
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  static InterfaceKitPhidget ik;
 
</source>
</font>
</div>
 
The object name for any type of Phidget is listed in the API manual.
Every type of Phidget also inherits functionality from the Phidget base class.
All Phidgets can throw PhidgetExceptions if something unexpected happens during operation.
Make sure to catch or declare the exception, even in a generic way.
This is what the main function looks like where we initialize the Phidget object without any constructors:
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  public static final void main(String args[]) throws Exception
  {
    ik = new InterfaceKitPhidget();
  }
 
</source>
</font>
</div>
 
===Connecting to the Phidget===
 
Next, the program needs to try and connect to the Phidget through an open call.
The open will tell the program to continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected.
This means that simply calling open does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or by calling waitForAttachment.
WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  ik.openAny();
  ik.waitForAttachment();
 
</source>
</font>
</div>
 
The parameters can be used to open the first Phidget of a type it can find, open based on its serial number, or even open across the network.
The API manual lists all of the available modes that open provides.
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed.
This prevents any other instances from retrieving data from the Phidget, including other programs.
The one connection per device limit does not apply when exclusively using the Phidget Webservice.
At the end of your program, don’t forget to call close to free any locks on the Phidget.
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  ik.close();
  ik = null;
 
</source>
</font>
</div>
 
===Event Driven Programming===
 
We recommend the use of event driven programming when working with Phidgets. In Java, we hook
an event handler with the following code: 
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  ik.addSensorChangeListener(new SensorChangeListener()
  {
      public void sensorChanged(SensorChangeEvent se)
      {
        //Insert your code here
        System.out.println(se.getValue());
      }
  });
 
</source>
</font>
</div>
 
With this method, the code inside sensorChanged will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.
The items from the event, such as the index or reported value, can be accessed from the SensorChangeEvent object properties.
Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets.
Please refer to the API manual for a full list of events and their usage.
 
===Working Directly With the Phidget===
 
Some values can be directly read and set on the Phidget. Simply use the instance’s properties or call member functions such as  getSensorValue(int index) or setOutputState(int index, boolean newVal) for PhidgetInterfaceKits.
These methods can be used inside a polling loop as an alternative to event driven programming.
 
===Working With Multiple Phidgets===
 
Multiple Phidgets of the same type can easily be run inside the same program.
In our case, it  requires another PhidgetInterfaceKit instance to be defined and initialized.
The new instance can then be set up, opened and used in the same process as the previous one.
If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.
 
===Other Phidgets===
 
The design given in this document can also be followed for almost all Phidgets.
For example, if you were using a PhidgetRFID instead of an PhidgetInterfaceKit, you would declare an RFID object instead of an InterfaceKit object.
The methods and events available would change but they can be accessed in a similar manner.
 
===Compiling a .jar File===
 
Finally, when the project is completed it is recommend to compile the project as a .jar.
This will reduce the number of extra flies created into a single package that is easier to manage.
In NetBeans a .jar file is automatically created during compilation.
Under the command line, you can use the jar utility from the Java SDK to package the .class files.
For example: “jar -cf MyProgram.jar *.class”. You will also need to provide a Manifest file for the jar program to indicate the program entry point via the Main-Class and the Class-Path which will point to the phidget21.jar.
Simply create a file, for example MyProgram.mf, and enter the following lines:
 
<font size="3">
 
Manifest-Version: 1.0
Class-Path: phidget21.jar
Main-Class: MyProgram
 
 
</font>
 
Please note that the manifest file must end with a new line or a carriage return.
Now run the jar utility to package the files. For example: “jar –cfm MyProgram.jar MyProgram.mf *.class”.
 
==Building your Project==
Describe the different ways a project could be built using this language.
 
==Manual Install==
<b>Windows</b>
<br/>
Files needed: phidget21.dll, phidget21.jar
<br/>
Place phidget21.dll into C:\WINDOWS\system32.
phidget21.jar can be placed anywhere as long as the path to them are specified with your compiler
 
 
==Common Problems and Solutions/Workarounds==
Here you can put various frequent problems and our recommended solutions.

Revision as of 22:14, 28 February 2019


We provide support for the Java language in all major operating systems. We also provide instructions on how to get your project started in a number of common development environments. Select your operating system and preferred development environment below, and follow the instructions to get your project running with Phidgets.

If you do not know which development environment you want to use, or your development environment of choice is not listed, we recommend starting with javac as the simplest path to getting your code running.

Once you have set up your development environment to run with Phidgets, we recommend you follow our guide on Phidget Programming Basics. The guide will showcase the fundamentals of programming with Phidgets, with examples in Java.

Choose Your Development Environment:

Java Development Environments
OS - Windows Windows

JAVA JC WIN.png JAVA JC WIN on.png

JAVA NETBEANS.png JAVA NETBEANS on.png

JAVA ECLIPSE.png JAVA ECLIPSE on.png

OS - macOS macOS

JAVA JC TRM.png JAVA JC TRM on.png

JAVA NETBEANS.png JAVA NETBEANS on.png

JAVA ECLIPSE.png JAVA ECLIPSE on.png

OS - Linux Linux

JAVA JC TRM.png JAVA JC TRM on.png

JAVA NETBEANS.png JAVA NETBEANS on.png

JAVA ECLIPSE.png JAVA ECLIPSE on.png

OS - Linux Phidget SBC Linux

JAVA JC SBC.png JAVA JC SBC on.png

OS - Android Android

JAVA AS ANDROID.png JAVA AS ANDROID on.png

Quick Downloads

If you already know what you're doing and just need the files, you can find them all below.

Documentation

Example Code

Libraries