Lesson: Hello World on the EV3

Overview: 
Create and download your first Java program for the EV3.
Objectives: 

Learn the basics of creating a Java program in Eclipse then downloading it to the EV3 and running it.

Content: 

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 and select New Project.

A list of project wizards 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 Projects. 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 completed before proceeding.

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.

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 project tree in the left pain 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.

 

Lesson navigation: