NetBeans/JDK on Ubuntu

Description

Java 8 is available through the OpenJDK package, but we'll use the Oracle Java for consistency with Windows and MAC installations, especially when intended for usage with JavaFX8. The setup described is based on Ubuntu version 16.04 and should work equally well on Linux Mint 18.

JDK Installation

We'll follow the instructions in http://tecadmin.net/install-oracle-java-8-jdk-8-ubuntu-via-ppa. Do these installations:
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
You have to accept a EULA to install this. Tab first to "OK" and Enter; then "Yes" and Enter. Afterwards you can confirm by:
$ java -version
$ javac -version

Scene Builder

This installed version, unlike those for MAC and Windows, does not have JavaFX8 Scene Builder installed. To get this, go to the site
http://gluonhq.com/open-source/scene-builder/
Download correct thing for your linux version. For 64-bit ubuntu, use the appropriate "Deb" package:
scenebuilder-LATEST_VERSION.deb
Install it as:
$ sudo dpkg -i scenebuilder-LATEST_VERSION.deb

Netbeans Installation

NetBeans is Oracle's community-based Integrated Development Environment (IDE) for a variety of software development interests including Java, Php, C/C++, etc. It requires a JDK installation. The home and download pages are these:
http://netbeans.org
http://netbeans.org/downloads/
The download site offers a bewildering array of version choices which are differentiated by a "download-type" term embedded in the file name. I generally prefer the minimalistic version since you are easily able to augment the features through Netbean's plugin facility:
netbeans-8.2-javase-linux.sh
Install by running:
$ sudo bash netbeans-8.2-javase-linux.sh
When the installation wizard asks for the JDK installation, choose the correct thing from the drop-down:
JDK for NetBeans IDE:

Personalize NetBeans

I recommend spending some time personalizing NetBeans' settings.

Author and License

NetBeans automatically adds some level of documentation to Java (and other types of) files it generattes; in particular Licensing and Author information. The licensing information is not particularly revelant to our needs, but setting the user name is important.
  1. Set the user name which will be generated automatically in every class file. Go to Tools ⇾ Templates ⇾ Settings. Edit the file, setting the "user" variable.
    user = Your Name
    
  2. Next, get rid of the Licensing information. Go to Tools ⇾ Templates ⇾ Licenses.

    Save the current "Default License" by clicking on it and selecting Duplicate, thereby creating a copy
    Default License 1
    Here is the current content of the license file:

    Default License
    <#if licenseFirst??>
    ${licenseFirst}
    </#if>
    ${licensePrefix}To change this license header, choose License Headers ...
    ${licensePrefix}To change this template file, choose Tools | Templates
    ${licensePrefix}and open the template in the editor.
    <#if licenseLast??>
    ${licenseLast}
    </#if>
    DO NOT DELETE THIS FILE! Simply select the content and delete it, or replace it by something more meaningful to you.

Source formatting

NetBeans has a very useful feature called source formatting accessible via Source ⇾ Format. By default, it will format the entire file. If a region is selected, it will format the region only. It is a good idea set these the way you like.
  1. Select Tools ⇾ Options, or on the MAC: Edit ⇾ Preferences.
  2. Choose Editor and then the Formatting tab.
  3. From the Language selection, choose All Languages (we're only using Java, but these settings are useful across the board). Leave the "Expand Tabs to Spaces" checked and set these:
    Number of Spaces per indent:   2
    Tab Size:                      4
    
  4. From the Language selection, choose Java Set this:
    Continuation Indentation Size: 4
    
  5. Choose the Alignment category. In the New Lines section, check the checkboxes for these:
    "else"
    "while"
    "catch"
    "finally"

Hello World Program

NetBeans creates directories called src which consist of one or more package of Java source files along with other types of support files. The compiled classes are kept in a separate build directory.

To create a simple "Hello World" program, start up NetBeans and follow the steps below.
  1. Select File ⇾ New Project
  2. In the New Project window, select the Java category, and choose Java Application, then Next.
  3. Choose the project name HelloWorld. The other settings have default values which you probably want to use. The project location cannot be an existing directory. NetBeans also pre-checks the box Create Main Class. Leave it checked. Click Finish.
  4. In the left-hand window there you can observe three views of the netbeans contents: Projects, Files, Services. For the most part you can work from the Projects view. In the Projects window you will see the file HelloWorld.java as part of the automatically-created helloworld package.
  5. Go to the Files view and observe the structure which NetBeans creates. The src folder is meant to hold all the source packages. The HelloWorld.java file is in a package directory HelloWorld within the src folder.

  6. Within the public static void main function, type
    System.out.println("Hello World");
    Observe the various syntactic assists which the editor offers when you pause after typing a ".".
  7. Select File ⇾ Save (or Ctrl-S) to save.
  8. There are several ways to compile and run this application. One way is to right-click on HelloWorld.java and select Run File from the popup menu. Look for the output in the Output window at the bottom.

  9. Another way to build and run the project is by selecting Run ⇾ Cean and Build Project or Shift+F11. This operation goes a step further and archives the compiled classes into the jar file HelloWorld.jar found in the newly created dist directory. Afterwards, select Run ⇾ Run Project or F6, or the button.

Shell Execution

NetBeans makes it easy to run its applications through the shell, assuming that your java executable is accessible. First of all, you have to execute "Clean and Build" to create the JAR file. The output of this operation indicates what should be done. After doing so, open a command shell and navigate to the dist folder in the HelloWorld. From this folder run:
java -jar HelloWorld.jar
The dist folder is meant to be for "distribution." NetBeans will put all relevant libraries in this folder as well so that this can act as a standalone executable which can run on any system which has JRE installed. All Windows systems will have it installed; the only hitch is making sure that the version is up to a suitable level.


© Robert M. Kline