Language - Java Windows Javac

From Phidgets Support
Jump to: navigation, search

Java Development Environments
OS - Windows Windows Javac (Command Line)NetBeansEclipse
OS - macOS macOS Javac (Terminal)NetBeansEclipse
OS - Linux Linux Javac (Terminal)NetBeansEclipse
OS - Linux Phidget SBC Linux Javac (SBC Linux)

Language - Java

Windows with javac

Welcome to using Phidgets with Java! By using Java, you will have access to the complete Phidget22 API, including events.

Javac is a command line-based compiler for java programs that compiles java code into bytecode class files.

Install Phidget Drivers for Windows

Before getting started with the guides below, ensure you have the following components installed on your machine:

  1. You will need the Phidgets Windows Drivers
  2. You will need the Java Development Kit from Oracle

Use Our Examples

One of the best ways to start programming with Phidgets is to use our example code as a guide. Before we get started, make sure you have installed the Phidget drivers.

Next, select an example that will work with your Phidget:

Copy phidget22.jar from the following location:

  • C:\Program Files\Phidgets\Phidget22\phidget22.jar

Place the example files and the phidget22.jar file in the same location. Your folder should now look something like this:

Java javac folder.png

Open the command prompt at the folder location. Next, enter the following command in the command prompt:

javac -classpath .;phidget22.jar

Finally, enter the following command to run the program:

java -classpath .;phidget22.jar example

Java javac.PNG

You should now have the example up and running for your device. Your next step is to look at the Editing the Examples section below for information about the example and important concepts for programming Phidgets. This would be a good time to play around with the device and experiment with some of its functionality.

Editing the Examples

To get our example code to run in a custom application, simply remove the calls to AskForDeviceParameters and PrintEventDescriptions, as well as the ChannelInfo object, then hard-code the addressing parameters for your application.

If you are unsure what values to use for the addressing parameters, check the Finding The Addressing Information page.

For instance:

//You may remove these lines and hard-code the addressing parameters to fit your application
ChannelInfo channelInfo = new ChannelInfo();  //Information from AskForDeviceParameters(). May be removed when hard-coding parameters.
PhidgetHelperFunctions.AskForDeviceParameters(channelInfo, ch);


if(channelInfo.netInfo.isRemote) {
    if(channelInfo.netInfo.serverDiscovery) {
        try {
        catch (PhidgetException e) {
            throw new Exception("Program Terminated: EnableServerDiscovery Failed", e);
    else {
        Net.addServer("Server", channelInfo.netInfo.hostname,
            channelInfo.netInfo.port, channelInfo.netInfo.password, 0);

//This call may be harmlessly removed

Might become:


Notice that you can leave out any parameter not relevant to your application for simplicity.

You can then manipulate the rest of the code as your application requires. A more in-depth description of programming with Phidgets follows in the Write Code section.

For future Phidgets-based projects, you can leave out the and classes entirely.

Write Code

By following the instructions for your operating system and compiler above, you now have working examples and a project that is configured. This teaching section will help you understand how the examples were written so you can start writing your own code.

Remember: your main reference for writing Java code will be the Phidget22 API Manual and the example code.

Step One: Create and Address

You will need to create your Phidget object in your code. For example, we can create a digital input object like this:

DigitalInput ch = new DigitalInput();

Next, we can address which Phidget we want to connect to by setting parameters such as DeviceSerialNumber.


Although we are not including it on this page, you should handle the return codes of all Phidget functions. Here is an example of the previous code with error handling:

try {
    DigitalInput ch = new DigitalInput();
} catch (PhidgetException ex) {

Step Two: Open and Wait for Attachment

After we have specified which Phidget to connect to, we can open the Phidget object like this:;

To use a Phidget, it must be plugged in (attached). We can handle this by calling open(timeout), which will block indefinitely until a connection is made, or until the timeout value is exceeded. Simply calling open() does not guarantee you can use the Phidget immediately.

Alternately, you could verify the device is attached by using event driven programming and tracking the attach events.

To use events to handle attachments, we have to modify our code slightly:

ch.addAttachListener(new AttachListener() {
    public void onAttach(AttachEvent ae) {
        printf("Phidget attached!\n");

We recommend using this attach listener to set any initialization parameters for the channel such as DataInterval and ChangeTrigger from within the AttachListener, so the parameters are set as soon as the device becomes available.

Step Three: Do Things with the Phidget

We recommend the use of event driven programming when working with Phidgets. In a similar way to handling an attach event as described above, we can also add an event listener for a state change event:

DigitalInput ch = new DigitalInput();

ch.addAttachListener(new AttachListener() {
    public void onAttach(AttachEvent ae) {
        printf("Phidget attached!\n");

ch.addStateChangeListener(new DigitalInputStateChangeListener() {
    public void onStateChange(DigitalInputStateChangeEvent e) {
        System.out.println("State changed: " + e.getState());

This code will connect a function to an event. In this case, the onStateChange function will be called when there has been a change to the channel's input.

If you are using multiple Phidgets in your program, check out our page on Using Multiple Phidgets for information on how to properly address them and use them in events.

If events do not suit your needs, you can also poll the device directly for data using code like this:

boolean state = ch.getState();
System.out.println("State: " + state);

Important Note: There will be a period of time between the attachment of a Phidget sensor and the availability of the first data from the device. Any attempts to get this data before it is ready will result in an exception. See more information on this on our page for Unknown Values.


Some Phidget devices have functions that deal with specific predefined values called enumerations. Enumerations commonly provide readable names to a set of numbered options.

Enumerations with Phidgets in Java will take the form of com.phidget22.EnumerationType.ENUMERATION_NAME.

For example, specifying a SensorType to use the 1142 for a voltage input would look like:


and specifying a K-Type thermocouple for a temperature sensor would be:


The Phidget error code for timing out could be specified as:


You can find the Enumeration Type under the Enumerations section of the Phidget22 API for your device, and the Enumeration Name in the drop-down list within.

Step Four: Close and Delete

At the end of your program, be sure to close your device:


Setting up a New Project

When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget Java library.

First, copy phidget22.jar from the following location:

  • C:\Program Files\Phidgets\Phidget22\phidget22.jar

Create your new project folder and java file, and place phidget22.jar in the same folder as your new project file. Your folder should look somehting like this:

Java javac folder new project.png

To use the Phidget Java library, add the following import to your code:

import com.phidget22.*;

Once you are ready to run your program, open the command prompt at the folder location. Next, enter the following command in the command prompt:

javac -classpath .;phidget22.jar

Finally, enter the following command to run the program:

java -classpath .;phidget22.jar example
Java javac new project.PNG

The project now has access to Phidgets.

Further Reading

Phidget Programming Basics - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.

Data Interval/Change Trigger - Learn about these two properties that control how much data comes in from your sensors.

Using Multiple Phidgets - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.

Polling vs. Events - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.

Logging, Exceptions, and Errors - Learn about all the tools you can use to debug your program.

Phidget Network Server - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.