Instructional Material: Hello World on the EV3

Now its time to try your first Java program on the EV3. Start Eclipse on your PC. When Eclipse is ready, select New on the File menu. Slide your cursor over to the item list and select Project (not Java Project).

A list of project types will appear. Select leJOS EV3 and below that leJOS EV3 Project and click Next. The new project dialog will appear. On this dialog enter a project name of EV3 Exercises. In the JRE box, the first item should be selected and in the drop-down box to the right select JavaSE-1.7. Click Finish.

Your new Java project has been created and should appear in the project explorer on the left side of the Eclipse screen. Click to open the projects items. One will be src, which is the directory where the project source files are stored. Right click on src and select New and then Class. This will display the New Class dialog.

On the New Class dialog, enter HelloWorld for the class name. Check the box to create a static void main method stub. Click Finish. This will create the HelloWorld.java file with the new class in it and open that class file in the editing area. You are ready to program!

Modify the code in the HelloWorld class file to look like this (you can copy and paste the code):

This program will display Hello World!! on the EV3 screen and wait for you to press any button on the EV3 to end the program. Note that we added an import statement to make all of the leJOS hardware classes known to our program. This allows us to use the leJOS Button class without fully qualifying its name. In the Button class we call a method that makes the EV3 stop execution and wait for any button to be pressed.

Now connect your EV3 to your PC with a USB cable. With the leJOS memory card inserted in the EV3, press the center button to turn on the EV3 and start leJOS. Wait until the leJOS menu is displayed on the EV3 screen. Note: On first connection between a PC and the EV3, a device driver will be automatically installed on the PC. Wait until the install completes before proceeding. See the note below about Windows 8/10 and driver install.

When the EV3 menu has appeared, return to Eclipse and click the green run arrow button in the Eclipse menu bar. The Run As dialog will appear to ask how you want to run your program. Select leJOS EV3 Program and click Ok. If you have any unsaved source files, you will be asked if you want to save them. Click Ok. Note that you can and should check the box to have Eclipse always save your files before running your program.

At this point Eclipse will pass your source file through the Java compiler. If errors are found, they will be displayed and the process stops so you can correct the errors. If no errors are found, the program will be downloaded to your EV3 and started. On the EV3 you will see a wait screen displayed (hourglass) and then your program will run. You should see Hello World!! in the top left of the EV3 display. The program will wait at this point until you press any key on the EV3.

Congratulations! You have programmed the EV3 robot controller with Java.

Note in addition to the Java and leJOS documentation available online or as local html files you can open in your browser, if you have questions about a class or method, you can put your cusor on the class instance variable or method name and Eclipse will display documentation for the selected item (if available). If you place your cursor over waitForAnyPress() for example, then the documentation for that method will be displayed. This feature and the one described below are called Intellisense.

You can type the name of a class instance followed by a dot and then wait. Eclipse will display the list of public methods and variables for that class. For instance if you typed Button. Eclipse will display a list of all methods and variables available with the Button object along with descriptions for each one. When the window opens you can use your up/down arrows to navigate the list. If you select an item and press Enter, that item will be entered into your code automatically.

A final note, if you look at the package tree in the left pane of Eclipse, you can see that the Hello World class was placed into the default package, since we did not specify a package name in the class.


Note: On Windows after version 7, the driver install may take a long time, several minutes or more. After connecting the USB cable, go to the Windows control panel and open Devices and Printers. Initially the USB connection will appear as a generic COM device. Windows may or may not display a message indicating it is updating the driver. Wait. What should occur is that the COM device will automatically transform into a "USB Ethernet/RNDIS Gadget" device after successful driver install. The driver is downloaded from the internet so you must be connected for the driver to install. If this does not happen after 10 minutes, you can return to control panel and open the Device Manager. On the devices list, find COM ports. Open the tree and locate the COM device that appeared when you connected the EV3. Right click on it and click Update Driver. This should force the driver update. When you connect the EV3, it needs to be identified as a USB Ethernet/RNDIS Gadget device to work correctly.

 

Material Type: 
Lab Activity
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
EV3
SW Platform: 
Java
Interactivity Style: 
Mixed
Audience: 
Learner