Language - Java: Difference between revisions

From Phidgets Support
No edit summary
No edit summary
 
(282 intermediate revisions by 10 users not shown)
Line 1: Line 1:
Preamble about the language and its general strengths and weaknesses.
{{#seo:|description=Learn how to use Phidget USB devices with Java.}}
[[Category:Language]]


==Assessment for use with Phidgets==
==Get Started==
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.
With the Phidget22 library, it's easy to create Java applications that work with Phidget devices.


==Support==
==Java Libraries==
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]].
===Source Files===
The Phidget22 Java library source files are available [https://cdn.phidgets.com/downloads/phidget22/libraries/windows/Phidget22-windevel.zip here].


==Restrictions==
==Development Environment Configuration==
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==
Before choosing a development environment, make sure you have the [http://www.oracle.com/technetwork/java/index.html Java Development Kit] installed.


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.  
===Javac===
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed"
|+ '''Instructions'''
|The easiest way to allow Java to access the Phidgets Java library is to place a copy of '''phidget22.jar''' in the same folder as your .java program.  ||[[Image:Language_java_javac_folder.png|center|350px|link=https://cdn.phidgets.com/docs/images/8/89/Language_java_javac_folder.png]]
|-
|[https://www.phidgets.com/downloads/phidget22/libraries/any/Phidget22Java.zip You can download phidget22.jar here].||
|-
|Once you are ready to run your program, enter the following command in the command prompt or terminal:
<syntaxhighlight>
javac -classpath .;phidget22.jar example.java
</syntaxhighlight>
Finally, enter the following command to run the program:
<syntaxhighlight>
java -classpath .;phidget22.jar example
</syntaxhighlight>
||
|-
|}


==Drivers, Libraries and Resources==
===Netbeans===
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.  
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed"
Visit the drivers section at www.phidgets.com and get the latest:
|+ '''Instructions'''
* [http://www.phidgets.com/drivers.php Phidget Framework]
|The easiest way to allow Java to access the Phidgets Java library is to place a copy of '''phidget22.jar''' in the same folder as your .java program. ||[[Image:Language_java_javac_folder.png|center|350px|link=https://cdn.phidgets.com/docs/images/8/89/Language_java_javac_folder.png]]
* [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:
|[https://www.phidgets.com/downloads/phidget22/libraries/any/Phidget22Java.zip You can download phidget22.jar here].||
* [http://www.phidgets.com/documentation/JavaDoc.zip API Manual]
|-
* [http://www.phidgets.com/documentation/web/javadoc/index.html API Reference]
| To start, open NetBeans and create a new project. Select Java Application and follow the steps as directed by Netbeans.|| [[Image:Windows_netbeans1.jpg|center|350px|link=https://cdn.phidgets.com/docs/images/c/c9/Windows_netbeans1.jpg]]
* [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.  
| Next, add a reference to phidget22.jar by right-clicking on the libraries folder. || [[Image:Windows_netbeans2.jpg|center|350px|link=https://cdn.phidgets.com/docs/images/2/2b/Windows_netbeans2.jpg]]
* The [[Device Functionality]] page explains the general operational information for your device.
|-
| Navigate to the folder where you downloaded phidget22.jar, and select it. || [[Image:Windows_netbeans3.jpg|center|350px|link=https://cdn.phidgets.com/docs/images/7/76/Windows_netbeans3.jpg]]
|-
| The project now has access to Phidgets. ||
|}


You may want to have these pages open while working through these instructions.
===Eclipse===
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed"
|+ '''Instructions'''
|The easiest way to allow Java to access the Phidgets Java library is to place a copy of '''phidget22.jar''' in the same folder as your .java program.  ||[[Image:Language_java_javac_folder.png|center|350px|link=https://cdn.phidgets.com/docs/images/8/89/Language_java_javac_folder.png]]
|-
|[https://www.phidgets.com/downloads/phidget22/libraries/any/Phidget22Java.zip You can download phidget22.jar here].||
|-
| To start, open Eclipse and create a new Java project.|| [[Image:Windows_eclipse1.jpg|center|350px|link=https://cdn.phidgets.com/docs/images/3/3a/Windows_eclipse1.jpg]]
|-
| On the Libraries tab, click Add External JARs and add phidget22.jar to your project as an external jar. || [[Image:Windows_eclipse2.jpg|center|350px|link=https://cdn.phidgets.com/docs/images/0/0c/Windows_eclipse2.jpg]]
|-
| Create a new Class in your project. || [[Image:Windows_eclipse3.jpg|center|350px|link=https://cdn.phidgets.com/docs/images/d/d9/Windows_eclipse3.jpg]]
|-
| Name the class, and be sure to check the '''public static void main''' box. || [[Image:Windows_eclipse4.jpg|center|350px|link=https://cdn.phidgets.com/docs/images/8/87/Windows_eclipse4.jpg]]
|-
| Your project now has access to Phidgets! ||
|-
|}


==Getting Started==
=== PhidgetSBC Web Interface ===
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed"
|+'''Instructions'''
|<center>{{#ev:youtube|gZmWvWXICIA|rel=0}}</center>
|}


The Phidget full examples were written using NetBeans and this tutorial assumes its use.
==Example Code==
Other environments such as Eclipse work as well and would be set up in a similar manner.  
Navigate to our [https://www.phidgets.com/?view=code_samples&lang=Java Code Sample Generator] to view and download code samples that are tailored to your specific device.
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===
[[Image:Language_java_codesample.png|center|600px|link=https://cdn.phidgets.com/docs/images/a/af/Language_java_codesample.png]]


Before you can use the Phidget, you must include a reference in the code to the library. In Java:
===Phidget Programming Basics===
{{PhidgetProgrammingBasicsLink}}


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
==API==
<font size="3">
[{{SERVER}}/?view=api&lang=Python Phidget22 API]
<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.
 
==Common Problems and Solutions/Workarounds==
Here you can put various frequent problems and our recommended solutions.

Latest revision as of 22:38, 12 March 2026


Get Started

With the Phidget22 library, it's easy to create Java applications that work with Phidget devices.

Java Libraries

Source Files

The Phidget22 Java library source files are available here.

Development Environment Configuration

Before choosing a development environment, make sure you have the Java Development Kit installed.

Javac

Instructions
The easiest way to allow Java to access the Phidgets Java library is to place a copy of phidget22.jar in the same folder as your .java program.
Language java javac folder.png
You can download phidget22.jar here.
Once you are ready to run your program, enter the following command in the command prompt or terminal:
javac -classpath .;phidget22.jar example.java

Finally, enter the following command to run the program:

java -classpath .;phidget22.jar example

Netbeans

Instructions
The easiest way to allow Java to access the Phidgets Java library is to place a copy of phidget22.jar in the same folder as your .java program.
Language java javac folder.png
You can download phidget22.jar here.
To start, open NetBeans and create a new project. Select Java Application and follow the steps as directed by Netbeans.
Windows netbeans1.jpg
Next, add a reference to phidget22.jar by right-clicking on the libraries folder.
Windows netbeans2.jpg
Navigate to the folder where you downloaded phidget22.jar, and select it.
Windows netbeans3.jpg
The project now has access to Phidgets.

Eclipse

Instructions
The easiest way to allow Java to access the Phidgets Java library is to place a copy of phidget22.jar in the same folder as your .java program.
Language java javac folder.png
You can download phidget22.jar here.
To start, open Eclipse and create a new Java project.
Windows eclipse1.jpg
On the Libraries tab, click Add External JARs and add phidget22.jar to your project as an external jar.
Windows eclipse2.jpg
Create a new Class in your project.
Windows eclipse3.jpg
Name the class, and be sure to check the public static void main box.
Windows eclipse4.jpg
Your project now has access to Phidgets!

PhidgetSBC Web Interface

Instructions

Example Code

Navigate to our Code Sample Generator to view and download code samples that are tailored to your specific device.

Language java codesample.png

Phidget Programming Basics

To learn more about the structure of the example code, visit our Phidget Programming Basics guide.

API

Phidget22 API