Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Learn how to code in Java on a Mac

Lucy Hattersley | March 28, 2016
Java is the hottest programming language to learn right now. Here's how to set up Java and start learning it on your Mac

Installing Java in Mac OS X

Learn how to code in Java on a Mac: Installing Java on a Mac

The first thing you need to do is download Java SE (Standard Edition) from Oracle. You may have Java installed in some form on your Mac already (probably the consumer version), but Java SE for Developers (known as the Java SDK) is the one to get.

You'll also need a IDE (Integrated Developer Environment), and Oracle has a great one called NetBeans. We'll install both the Java SKD and NetBeans at the same time.

  1. Open Safari and head to Oracle's Downloads page. Click on Java SE.
  2. Click on the Download icon under NetBeans with JDK 8. You'll want a programming environment, and Netbeans is a good one. The JDK 8 will come alongside it and it handles the install for you.
  3. Click on Accept License Agreement and click the Download link next to Mac OS X. The DMG file will be saved into your Downloads folder.
  4. Open the download file (ours is "NetBeans 8.1 wtih JDK 9 Update 73") and follow the installation instructions.
  5. Open System Preferences and click on Java then About. Check that Version 8 Update 74 (or later) is installed. Alternatively open Terminal and enter "java -version."

You should now have Java and NetBeans installed in OS X. NetBeans is where you'll be creating most of your programs, so let's take a look at it.

Create HelloWorld in Java with NetBeans

Learn how to code in Java on a Mac: Create HelloWorld in Java on a Mac

The next step is to open NetBeans in OS X and create your first program. Tradition has it that the first program you create in any language should print "Hello World!" on the screen. Here's how to create HelloWorld in NetBeans:

  1. Open the Applications folder and launch NetBeans 8.1.
  2. Choose File > New Project.
  3. Ensure Java is selected in Categories and Java Application in Projects. Click Next.
  4. Enter "HelloWorld" in the Project Name field. Ensure "Create Main Class" is highlighted and click Finish.
  5. You should see the contents of HelloWorld.java in the main window. If not, use the Projects window on the left and locate HelloWorld.java (under Source Packages).
  6. Locate the code marked "// TODO code application logic here". Underneath this add a line that says "System.out.println("Hellod!");". Ensure this line of code is lined up with the "// TODO..." line above. See the image for exact positioning.
  7. Choose File > Save.
  8. Click the green Run Project icon in the Toolbar.

In the Output window at the bottom you should see the following:

Run:

Hello World!

BUILD SUCCESSFUL (total time: 0 seconds)

 

1  2  Next Page 

Sign up for MIS Asia eNewsletters.