Language - Java SBC Linux Javac: Difference between revisions

From Phidgets Support
(Redirected page to Language - Java)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Language]]{{NoTitle}}
#REDIRECT [[Language - Java]]
{|
|style="vertical-align:middle; width: 60%;"|
<font size=6>'''Language - Java'''
 
'''SBC Linux with javac'''</font>
 
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.
|{{TOC limit|2}}
|}
==Getting Started With the Phidget SBC==
Welcome to using the Phidget SBC. If you haven't already, check out the [[SBC3003 User Guide#Getting Started | user guide]] in order to set up the following:
* Networking
* Administrator password
 
 
If you are ready to go, the first step will be deciding how you will use the SBC:
*Use a more powerful external computer to develop your code, and then simply copy the files to the SBC.
*Use the SBC like any other Linux computer, simply connect a monitor and a keyboard and begin your development.
 
 
This guide will cover development using an external machine. For development using the SBC itself, check the [[Language_-_Java_Linux_Javac|Javac (Linux)]] page.
 
To begin, this video will help you get started:
<center>{{#ev:youtube|InbY-Ui1tg0}}</center>
 
 
===Developing with an External Computer===
{{SBC_Developing_with_an_External_Computer}}
 
===Installing Packages for Development===
At this point you have connected to the SBC through one or more these three options:
*SBC Web Interface
*SSH
*Directly via monitor and keyboard
 
Now that you are connected, you may want to start developing on/for the SBC. Before you do this, you need to install some packages.
 
The simplest way to install Java support on the SBC is via the install buttons on located on the SBC Web Interface (System->Packages). Check ''Include full Debian Package Repository'' before installing.
 
 
[[File:phidgetsbc_installpackages.PNG|link=|alt=|center]]
 
When developing for Java, ensure your development machine and your SBC have the same version of Java. Check your Java version by entering this command:
<syntaxhighlight lang=bash>
java -version
</syntaxhighlight>
 
If you need to update the version of Java on your SBC, use the following commands:
<syntaxhighlight lang=bash>
apt-get install default-jre-headless
su
update-alternatives --config java
</syntaxhighlight>
 
If you want to compile your Java programs on the SBC itself, you will need to download and install the JDK. You can do this by entering the following command in the terminal using [[#SSH|SSH]]:
<syntaxhighlight lang='bash'>
apt-get install default-jdk-headless
</syntaxhighlight>
 
You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.
 
==Use Our Examples==
'''''For simplicity, if you have not used Phidgets before, we recommend trying them out directly on an external development machine like a desktop computer. For Java development, check the [[Language_-_Java_Windows_Javac|Javac (Windows)]], [[Language_-_Java_macOS_Javac|Javac (MacOS)]], or [[Language_-_Java_Linux_Javac|Javac (Linux)]] page, depending on your operating system.'''''
 
One of the best ways to start programming with Phidgets is to use our example code as a guide.
 
Select an example that will work with your Phidget:
*{{SampleCode|Java|Java Examples}}
 
When developing on an external computer, you will write, compile, and test your programs on that machine. When you are ready, you will then upload your programs to the SBC to run them.
 
Follow these steps to get our examples running on an SBC:
 
1. Place phidget22.jar on your development machine in a directory that you will use to compile your Java files.
 
2. Copy all the example files from the example package to the same directory.
 
3. Compile the ''ExampleName.java'' file. If you are using a Windows machine, type the following into the command prompt:
<syntaxhighlight lang=bash>
javac -classpath .;phidget22.jar ExampleName.java
</syntaxhighlight>
If you are using a Linux or macOS machine, type the following into the terminal:
<syntaxhighlight lang=bash>
javac -classpath .:phidget22.jar ExampleName.java
</syntaxhighlight>
 
You should now have a number of ''.class'' files in your project directory
 
 
4. Using the SBC Web Interface, create a new project:
 
 
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
 
 
5. Transfer all the ''.class'' files from the development machine to the SBC, either using the SBC Web Interface or a tool like [https://winscp.net/eng/download.php WinSCP]. The project directory will be <code>/usr/userapps/ProjectName</code>
 
6. Use [[#SSH|SSH]] to access the SBC terminal
 
7. Navigate to the project folder using the command:
 
<syntaxhighlight lang=bash>
cd /usr/userapps/ProjectName
</syntaxhighlight>
 
8. You can now run the program with the command:
 
<syntaxhighlight lang=bash>
java ExampleName
</syntaxhighlight>
 
 
Success! The example is running on your SBC. If you are already familiar with how to use Phidgets in your programs, you can jump ahead to [[#Running a program automatically|Running a Program Automatically]].
 
{{Edit_the_Examples}}
 
==Editing the Examples==
{{Language_-_Java_Editing_the_Examples}}
 
==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.
 
When developing on an external computer, you will write, compile, and test your programs on that machine. When you are ready, you will then upload your programs to the SBC to compile and run them.
 
To use the Phidget Java library, add the following import to your code:
<syntaxhighlight lang="java">
import com.phidget22.*;
</syntaxhighlight>
 
Once your code is written, follow these steps to get your program running on the SBC:
 
1. Place phidget22.jar on your development machine in a directory that you will use to compile your Java files.
 
2. Compile your ''ProgramName.java'' file. If you are using a Windows machine, type the following into the command prompt:
<syntaxhighlight lang=bash>
javac -classpath .;phidget22.jar ProgramName.java
</syntaxhighlight>
If you are using a Linux or macOS machine, type the following into the terminal:
<syntaxhighlight lang=bash>
javac -classpath .:phidget22.jar ProgramName.java
</syntaxhighlight>
 
You should now have a number of ''.class'' files in your project directory
 
 
4. Using the SBC Web Interface, create a new project:
 
 
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
 
 
5. Transfer all the ''.class'' files from the development machine to the SBC, either using the SBC Web Interface or a tool like [https://winscp.net/eng/download.php WinSCP]. The project directory will be <code>/usr/userapps/ProjectName</code>
 
6. Use [[#SSH|SSH]] to access the SBC terminal
 
7. Navigate to the project folder using the command:
 
<syntaxhighlight lang=bash>
cd /usr/userapps/ProjectName
</syntaxhighlight>
 
8. You can now run the program with the command:
 
<syntaxhighlight lang=bash>
java ExampleName
</syntaxhighlight>
 
Success! The program is running on your SBC. Next, you may want to learn about [[#Running a program automatically|running a program automatically]].
 
==Running a Program Automatically==
After testing your program, you will likely want it to run on boot, or on a schedule, without your input.
 
{{SBC_Running_A_Program_Automatically}}
 
{{Language Page What's Next}}

Latest revision as of 16:38, 15 March 2021

Redirect to: