Running Java on the OSWALD
This section describes how to use java on the OSWALD. The process is fairly simple, we will be working with a hello world program. There are a few steps, the first is to create the source, we will call it HelloWorld.java, make the file using a text editor and fill it in with the following contents. This step is done on your computer.
class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
The next step is to compile the java file into a class file, the easiest way is to follow the guide below on compiling java code using gcj on the OSWALD if you are running Radix-2.1 or later.
Otherwise you'll have to compile the code externally. One way to do that is by making a project in eclipse (cs161 students should know how to do this), a guide for that can be found here (Or you can make the class file using another method described here). Once you have made the project, run it once. Now navigate to the directory that your project is in, and find the HelloWorld.class file, copy this to a flash drive, or SD card. Once you have put the card/drive into your OSWALD, open a terminal (click the little black icon on the bottom of the screen). Type one of the following commands:
For sd cards:
cd /media/mmcblk0p1
For flash drives:
cd /media/sda1
Finally, we can run the java program using:
java HelloWorld
If successful you should see:
Hello World!
On your terminal.
Compiling java code using gcj
Java source code (.java files) can be compiled using the command 'gcj'.
For example:
gcj HelloWorld.java
This example will not work, however, as gcj must be told which main method to run when specifying source files, for example:
gcj --main=HelloWorld -o HelloWorld HelloWorld.java
The parameter "--main=<className>" tells the compiler that the executable should run the main within the HelloWorld class and the "-o" parameter specifies the output file.
If the code is successfully compiled, the executable will be a file named "HelloWorld in the current working directory. If no output file is specified, the executable will be the file named "a.out" in the current working directory.
Run the program from the current working directory with the following command:
./HelloWorld
To compile to java bytecode (.class file), use the -C option:
gcj -C HelloWorld.java
This command will produce "HelloWorld.class" which can then be run in the java virtual machine:
java HelloWorld
Notice that you only specify the class name, not the bytecode file name.
Using the Joystick
The joystick can be mostly accessed through the keyboard interface using KeyListeners. If you want to access the accelerometer please look at this page.
Using the Network
The network (802.15.4 wireless) on the OSWALD can be accessed through a standard !IP socket interface. For Java this means using the !Socket, and ServerSocket classes. Alternatively the following classes can be used as a simple (and limited) method.
- SimpleJavaServer.java (server class)
- SimpleJavaClient.java (client class)
- Server.java (usage example of the server)
- Client.java (usage example of the client)
Adding jar files to the classpath
Currently, any jar files placed in /home/root/.jars will be automatically added to the classpath environment.
Attachments
- SimpleJavaServer.java (2.9 kB) - added by goskab on 09/16/09 10:12:46.
- SimpleJavaClient.java (2.3 kB) - added by goskab on 09/16/09 10:13:02.
- Server.java (0.8 kB) - added by goskab on 09/16/09 10:13:16.
- Client.java (0.8 kB) - added by goskab on 09/16/09 10:13:29.
