Language - Java: Difference between revisions
No edit summary |
|||
| (254 intermediate revisions by 10 users not shown) | |||
| Line 1: | Line 1: | ||
[[ | {{#seo:|description=Learn how to use Phidget USB devices with Java.}} | ||
[[Category:Language]] | |||
==Get Started== | |||
With the Phidget22 library, it's easy to create Java applications that work with Phidget devices. | |||
== | ==Java Libraries== | ||
You must install the [https://www.phidgets.com/docs/Operating_System_Support Phidget drivers] for your operating system to be able to compile and run Java programs. | |||
Your Java project will also require our Java library file: | |||
* [https://www.phidgets.com/downloads/phidget22/libraries/any/Phidget22Java.zip phidget22.jar]. | |||
====macOS Considerations==== | |||
Legacy Phidgets running a HID USB stack require a driver extension (macOS 10.15 and newer) or a kernel extension (macOS 10.14 and earlier). During development, we recommend downloading the appropriate [https://www.phidgets.com/docs/OS_-_macOS#Quick_Downloads package] which will install the extension. When deploying applications, review the README in the [https://cdn.phidgets.com/downloads/phidget22/libraries/macos/Phidget22_macosdevel.zip macOS Development Files] for information about bundling the extension with your application. | |||
=====Determining USB Stack===== | |||
To determine which USB stack your device is running, navigate to the product page and then to the specification tab, and look for the ''USB Stack'' specification. If you are using a VINT device, navigate to the product page for the VINT Hub you are using. | |||
[[Image:Javascript_networkserver_webusb_spec.png|center|600px|link=https://cdn.phidgets.com/docs/images/8/80/Javascript_networkserver_webusb_spec.png]] | |||
====Linux Considerations==== | |||
Linux restricts access to USB devices to the root user. To run your Python application as a regular user, you'll need to [{{SERVER}}/docs/OS_-_Linux#Setting_Udev_Rules set up udev rules] on your system. | |||
==Development Environment Configuration== | |||
Before choosing a development environment, make sure you have the [http://www.oracle.com/technetwork/java/index.html Java Development Kit] installed. | |||
===Visual Studio Code=== | |||
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed" | |||
|+ '''Instructions''' | |||
| To start developing with Java in VSCode, open it up and install the '''Extension Pack for Java'''.||[[Image:Language_java_vscode_extension2.png|center|350px|link=https://cdn.phidgets.com/docs/images/b/b4/Language_java_vscode_extension2.png]] | |||
|- | |||
| Next, press '''Ctrl+Shift+P''' to open the command palette, type "java" and select '''Java: Create Java Project'''. || [[Image:Language_java_vscode_createproj.png|center|360px|link=https://cdn.phidgets.com/docs/images/7/78/Language_java_vscode_createproj.png]] | |||
|- | |||
| Select "No Build Tools" and select a directory for the new project. You'll be asked for a project name, and VSCode will create the directory structure. || [[Image:Language_java_vscode_emptyproj.png|center|350px|link=https://cdn.phidgets.com/docs/images/c/c1/Language_java_vscode_emptyproj.png]] | |||
|- | |||
| colspan="2"|You'll need to get a copy of '''phidget22.jar''' to put inside the "lib" folder. [https://www.phidgets.com/downloads/phidget22/libraries/any/Phidget22Java.zip You can download phidget22.jar here]. | |||
|- | |||
| colspan="2"| You can now begin writing code in the newly created "App.java" file, or you can download a [[#Example_Code|sample program]] for your device and place it in the "src" folder. | |||
|- | |||
| Compile and run by pressing '''F5''' or by clicking the play button in the top right. || [[Image:Language_java_vscode_run.png|center|350px|link=https://cdn.phidgets.com/docs/images/e/ea/Language_java_vscode_run.png]] | |||
|} | |||
Instructions | ===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> | |||
|| | |||
|- | |||
|} | |||
== | ===Netbeans=== | ||
{| 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].|| | |||
Java | |- | ||
| 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]] | |||
|- | |||
| 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]] | |||
|- | |||
| 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. || | |||
|} | |||
[[ | |||
= | |||
The project now has access to | |||
===Eclipse=== | ===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. Be sure it's being added to the classpath and not the module path. || [[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. Eclipse may require that you add a package name. Since this isn't a modular project, you must delete the {{code|module-info.java}} file that it creates. || [[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! || | |||
|- | |||
|} | |||
=== PhidgetSBC Web Interface === | |||
{| style="margin:auto;" class="table-no-border mw-collapsible mw-collapsed" | |||
|+'''Instructions''' | |||
|<center>{{#ev:youtube|gZmWvWXICIA|rel=0}}</center> | |||
|} | |||
= | |||
< | |||
= | |||
== | ==Example Code== | ||
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. | |||
[[Image:Language_java_codesample.png|center|600px|link=https://cdn.phidgets.com/docs/images/a/af/Language_java_codesample.png]] | |||
===Phidget Programming Basics=== | |||
{{PhidgetProgrammingBasicsLink}} | |||
{{ | ==API== | ||
[{{SERVER}}/?view=api&lang=Python Phidget22 API] | |||
== | == Deploying Applications == | ||
If you're planning to deploy your application, it should be noted that you don't strictly need the Phidget Drivers installed. | |||
At a minimum, these three files are required: | |||
* phidget22.jar | |||
* phidget22.dll | |||
* phidget22java.dll | |||
All three files can be gathered from the installation directory on a computer that has our drivers installed (e.g. C:/Program Files/Phidgets/Phidget22). Java will look for these files in several locations, including the classpath and the working folder of the project. | |||
Latest revision as of 22:28, 23 March 2026
Get Started
With the Phidget22 library, it's easy to create Java applications that work with Phidget devices.
Java Libraries
You must install the Phidget drivers for your operating system to be able to compile and run Java programs.
Your Java project will also require our Java library file:
macOS Considerations
Legacy Phidgets running a HID USB stack require a driver extension (macOS 10.15 and newer) or a kernel extension (macOS 10.14 and earlier). During development, we recommend downloading the appropriate package which will install the extension. When deploying applications, review the README in the macOS Development Files for information about bundling the extension with your application.
Determining USB Stack
To determine which USB stack your device is running, navigate to the product page and then to the specification tab, and look for the USB Stack specification. If you are using a VINT device, navigate to the product page for the VINT Hub you are using.
Linux Considerations
Linux restricts access to USB devices to the root user. To run your Python application as a regular user, you'll need to set up udev rules on your system.
Development Environment Configuration
Before choosing a development environment, make sure you have the Java Development Kit installed.
Visual Studio Code
| To start developing with Java in VSCode, open it up and install the Extension Pack for Java. | |
| Next, press Ctrl+Shift+P to open the command palette, type "java" and select Java: Create Java Project. | |
| Select "No Build Tools" and select a directory for the new project. You'll be asked for a project name, and VSCode will create the directory structure. | |
| You'll need to get a copy of phidget22.jar to put inside the "lib" folder. You can download phidget22.jar here. | |
| You can now begin writing code in the newly created "App.java" file, or you can download a sample program for your device and place it in the "src" folder. | |
| Compile and run by pressing F5 or by clicking the play button in the top right. | |
Javac
| 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. | |
| 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.javaFinally, enter the following command to run the program: java -classpath .;phidget22.jar example |
Netbeans
| 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. | |
| 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. | |
| Next, add a reference to phidget22.jar by right-clicking on the libraries folder. | |
| Navigate to the folder where you downloaded phidget22.jar, and select it. | |
| The project now has access to Phidgets. |
Eclipse
| 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. | |
| You can download phidget22.jar here. | |
| To start, open Eclipse and create a new Java project. | |
| On the Libraries tab, click Add External JARs and add phidget22.jar to your project as an external jar. Be sure it's being added to the classpath and not the module path. | |
| Create a new Class in your project. | |
Name the class, and be sure to check the public static void main box. Eclipse may require that you add a package name. Since this isn't a modular project, you must delete the module-info.java file that it creates. |
|
| Your project now has access to Phidgets! |
PhidgetSBC Web Interface
Example Code
Navigate to our Code Sample Generator to view and download code samples that are tailored to your specific device.
Phidget Programming Basics
To learn more about the structure of the example code, visit our Phidget Programming Basics guide.
API
Deploying Applications
If you're planning to deploy your application, it should be noted that you don't strictly need the Phidget Drivers installed.
At a minimum, these three files are required:
- phidget22.jar
- phidget22.dll
- phidget22java.dll
All three files can be gathered from the installation directory on a computer that has our drivers installed (e.g. C:/Program Files/Phidgets/Phidget22). Java will look for these files in several locations, including the classpath and the working folder of the project.
