JDK/Netbeans on Linux (Ubuntu)
(last updated: Jan 20, 2010) print

Select font size:
The software packages you'll need are these installation files (from the course website).
netbeans-6.9-ml-javase-linux.sh (NetBeans)
jdk-6u18-docs.zip (Java Documentation)
These packages and other related ones are available from the respective home sites below. The Java site provides a cobundle version with JDK and NetBeans, but I prefer separate installations. The Java Documentation is not strictly necessary, but it is needed to provide online Javadoc reference in Netbeans.

SoftwareHomeDownload
Java http://java.sun.com http://java.sun.com/javase/downloads/
NetBeans http://www.netbeans.org http://www.netbeans.org/downloads/

JDK Installation

Sun has recently begun freeing their Java implementation and thereby opening the way for free software developers to create an entirely free implementation of Java. This free form is now available as the OpenJDK. On Ubuntu, this is available as the openjdk-6-jdk package. My experience, however, is that the SUN JDK package is still a bit better, especially when used as the basis of other software tools like NetBeans.

Install the Sun JDK installation like this:
$ sudo apt-get install \
  sun-java6-jdk sun-java6-jre sun-java6-plugin sun-java6-fonts
The installation of some of these requires agreeing to a 'Distributor License' twice:
  1. First, press the TAB key to highlight OK, then Enter.
  2. Second, Press TAB key to get to "Yes", then Enter.

Make sure you have the right java

Open a terminal shell and execute these
$ java -version
$ javac -version
to make sure you're getting a "JDK6" version. If, for some reason, not. You need to run change the Java alternatives. Do so by first running:
$ update-java-alternatives -l
The first entry on each line is the key. If necessary, set
$ sudo update-java-alternatives -s java-6-sun

Netbeans Installation

NetBeans is SUN Microsystems' full-featured community-based Integrated Development Environment (IDE) for a variety of software development interests including a Java Visual GUI editor, Web developement for Java, Php, Ruby, C/C++, etc.

The download site offers a bewildering array of choices of Netbeans, they are differentiated by a "download-type" term embedded in the download file. I generally prefer to be minimalistic and augment the features as needed. Other features can easily be added through Netbean's plugin facility or by separate downloads. The download type I'm using is javase as seen in the file:
netbeans-6.9-ml-javase-linux.sh
Install into a system directory ("/usr/local" by default) by executing and following the installation wizard:
$ sudo sh netbeans-6.9-ml-javase-linux.sh
A NetBeans executable shortcut can be found in the Applications Programming menu.

Configuration

You'll want to double-check that Java 1.6 is the platform being used by Netbeans. Open Tools Java Platforms and make sure the default is JDK1.6.

Most configuration settings are done by activating Tools Options through the menu system. If this is not the case, and you've confirmed that you JDK1.6 is the "main" JDK on your system (Mac users, see above), then reinstall NetBeans — it should be able to pick up the 1.6 installation.

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 boxes Set As Main Project and Create Main Class. Leave these 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 Main.java as part of the automatically-created helloworld package.
  5. Go to the Files view and observe the structure which NetBeans creates. The Main.java file is in a package directory helloworld within the src directory, meant to hold all the source packages.

  6. Within the public static void main function (not the Main constructor), 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 Main.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 Build Build Main Project or F11. This operation goes a step further and archives the compiled classes into the jar file Hello_World.jar found in the newly created dist directory. Afterwards, select Run Run Main Project or F6.

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 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.

Install Javadoc Documentation

Choose a permanent location in your system for the documentation zip file jdk-6u18-docs.zip. For the sake of specificity, let's say it is this:
/usr/local/share/docs/java/jdk-6u18-docs.zip
Create this folder if it doesn't already exist.

In Netbeans, open Tools Java Platforms and select the Javadoc tab from the Java Platform Manager. Click the Add ZIP/Folder ... button and navigate to select the above zip archive.

To see what effect this has, right-click on the "println" portion of the statement
System.out.println("Hello World")
in the Main.java file. From the popup menu, select Show Javadoc.


© Robert M. Kline