Language - Java Linux Javac
Language - Java Linux 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 Linux
Before getting started with the guides below, ensure you have the following components installed on your machine:
- You will need the Phidgets Linux Drivers
- You will need the Java Development Kit from Oracle
- You will need a copy of phidget22.jar
Use Our Examples
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install the JDK. You can do this by entering the following command in the terminal (where VERSION is replaced with your preferred version number):
apt-get install openjdk-VERSION-jdk
Before continuing, ensure your JDK version matches your JRE version:
javac -version
java -version
Now that you have the JDK installed and phidget22.jar on hand, select an example that will work with your Phidget:
Your project folder should now look something like this:
Next, open the terminal at the folder location and enter the following command:
javac -classpath .:phidget22.jar example.java
Finally, enter the following command to run the program:
java -classpath .:phidget22.jar example
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);
ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber);
ch.setHubPort(channelInfo.hubPort);
ch.setIsHubPortDevice(channelInfo.isHubPortDevice);
ch.setChannel(channelInfo.channel);
if(channelInfo.netInfo.isRemote) {
ch.setIsRemote(channelInfo.netInfo.isRemote);
if(channelInfo.netInfo.serverDiscovery) {
try {
Net.enableServerDiscovery(ServerType.DEVICE_REMOTE);
}
catch (PhidgetException e) {
PhidgetHelperFunctions.PrintEnableServerDiscoveryErrorMessage(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
PrintEventDescriptions();
Might become:
ch.setDeviceSerialNumber(370114);
ch.setHubPort(2);
ch.setIsHubPortDevice(true);
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 can be found in our guide on Phidget Programming Basics.
For future Phidgets-based projects, you can leave out the PhidgetHelperFunctions.java and ChannelInfo.java classes entirely.
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.
In order to use Java, you will need to download and install the JDK. You can do this by entering the following command in the terminal (where VERSION is replaced with your preferred version number):
apt-get install openjdk-VERSION-jdk
Before continuing, ensure your JDK version matches your JRE version:
javac -version
java -version
You will also need a copy of 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:
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 example.java
Finally, enter the following command to run the program:
java -classpath .:phidget22.jar example
The project now has access to Phidgets.
What's Next?
Now that you have set up Phidgets to work with your programming environment, we recommend you read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.