CS636 Java/Ant Development Setup: on Linux at cs.umb.edu and on your home machine, Fall 2017

Linux at cs.umb.edu: Java/Ant setup.
For Linux systems such as users.cs.umb.edu and our system, topcat.cs.umb.edu, you should not need to do any setup to use java and ant. To make sure the java and ant are recent-enough versions, simply use the commands under Linux java and setup tests below.  To check that java and javac are in /usr/bin as expected, use the "which"; command as follows:

    $ which javac
    /usr/bin/javac

To define the JAVA_HOME environment variable, put this line in your .profile:

JAVA_HOME=/usr; export JAVA_HOME

Linux at cs.umb.edu:  editors Hopefully you know emacs or vi or another Linux editor.  We will do most of our software development on PCs, and then test the systems on Linux at cs.umb.edu for final delivery. This works well because of the robust portability of Java. Project deliveries are to the Linux filesystem at cs.umb.edu. See AccessToCSHosts.html for more info on file transfers.

Linux at cs.umb.edu: java setup test:

java-version   should output
java version "1.7.0_xx", or more recent version, plus two more lines of text, where xx stands for 2 digits
The JAVA_HOME environment variable is needed by some ant tasks and other tools.  To check it:
env|grep JAVA    should output 
JAVA_HOME=/usr  (on topcat.cs.umb.edu, Linux
env|grep CLASSPATH    should output nothing.  If it does give output, try to hunt down the source, usually in .profile or .bashrc, or files they invokes.

Linux at cs.umb.edu:  ant setup test:

ant-version     should output
Apache Ant version 1.7.0 ...  or more recent version

Home System Software Setup

For logging in to cs.umb.edu systems from home PCs and transferring files, see AccessToCSHosts

Browser

We will use Chrome as the default browser. Please install it if you don't have it already.

Text Editor

Windows: download Notepad++, unless you already have (and know how to use) emacs or vi or another pure text editor.
Mac: download TextWrangler unless you already have and (and know how to use) emacs or vi or another pure text editor. Note that Mac's TextEdit does not produce pure text files. As on any UNIX system, vi is available at the shell prompt (in Terminal).
Linux: As on any UNIX system, vi is available at the shell prompt. Other possibilities are gedit (simpler) and emacs (harder).

Java setup on home systems

Java for Linux or Mac OSX:

First see what java is already there by using "java -version"; and "javac -version";. If these versions agree and show 1.7 or 1.8, you do not need to download the JDK.  For Linux, you may be able to install a newer Java JDK by using the package system of your Linux distribution. For the Ubuntu Linux, see http://tecadmin.net/install-oracle-java-8-jdk-8-ubuntu-via-ppa/#

Java for Windows

Assume you need to download Java's JDK, unless you have previously set up Java for development.

Downloading Java from Oracle - We will be using Java 8 (or Java 7) from Oracle.  Everything we need from Oracle is in the Java SE (Java Standard Edition) download, that is, the  JDK (Java Development Kit) for Java 8.  Note that the JRE (Java runtime environment) is not enough. Download and install the latest JDK (Java SE Development Kit) from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. It takes about 160MB of disk space. Note that the Java SE installation will install a system JRE, i.e., possibly disrupt other Java development on the system if you need an older Java for some reason, but fine if this system is used only for current Java development. Java 8 can be used as a maintenance release of Java 7 or Java 6 for normal Java programming.

      If you already have the Java 7 JDK downloaded from Oracle, that's good enough. Also, it's OK if you have the larger (360 MB) distribution of JavaEE (Java Enterprise Edition), which contains the full Java SE JDK in its jdk subdirectory.  Be aware that at least some JavaEE installations include the unneeded applications server called Glassfish, which can interfere with tomcat, the applications server we will be using, by tying up port 8080.

JAVA_HOME and PATH Environment Variables  

The environment variable JAVA_HOME tells where the JDK is located on the system (Windows, Linux, or MacOSX). Specifically, it is the directory with a bin subdirectory containing java and javac.  JAVA_HOME is used by programs (other than java and javac) that need access to resources in the JDK.

The environment variable PATH holds the search path used by the system (Windows, Linux, or MacOSX) to locate executables. When we add a new executable located in a directory not listed in PATH, we need to add its directory to PATH, so we can run the program from anywhere on the system. How to do this depends on the system as follows.

JAVA_HOME and PATH for a Windows machine: Use System Control Panel>Advanced>Environment to define the JAVA_HOME user environment variable as C:\Program Files\Java\jdk1.8.0_xxx or c:\Java\jdk1.7.0_xx or wherever you put the JDK download. There is a shortcut method to get to this control panel, as follows. Click the Start button, fill in "env" in the text box, and click on "Edit Environmant Variables for your account", add JAVA_HOME as C:\Program Files\Java\jdk1.8.0_xxx or c:\Java\jdk1.7.0_xx or wherever you put the JDK download. See pic. Then edit your user Path environment variable to include your JDK's bin directory, using %JAVA_HOME%\bin as its value, or the explicit path such as C:\Program Files\Java\jdk1.8.0_xxx\bin. Use a semicolon between entries in the value of Path. For example, my Path value has c:\Program Files\emacs\bin; %JAVA_HOME%\bin.    Test the environment variables by creating a new command window and using the "set" command.

JAVA_HOME and PATH for a Linux machine: It should work to add the environment variables to .profile (or .bashrc or .bash_profile possibly, non-Ubuntu Linux) in your home directory. For example, if javac is in /usr/bin, add this line to .profile (creating the file .profile if not there already):

  export JAVA_HOME=/usr

If you have downloaded Java to /usr/local/jdk1.8.0_60 (say), then put "export JAVA_HOME=/usr/local/jdk1.8.0_60"; in .profile. If you did the download through a package system like apt-get for Ubuntu, you may not need to do anything extra to get the new java and javac on your Path, so check the resulting versions by "java -version" and "javac -version". If these fail or show an old version, you need further work. Ubuntu/Debian Linux has "update-alternatives"; for handling multiple versions of Java on the system, but it is somewhat difficult to use. The straightforward way is to edit your PATH environment variable setup in .profile.  Suppose the old PATH (reported by "echo $PATH";) is :/usr/local/bin:/bin:/usr/bin  Then add this line to .profile:

   export PATH=$PATH:/usr/local/jdk1.8.0_60/bin

Note the colons between directories on the PATH, vs. semicolons for the Windows PATH.

This should be executed when you open a new shell window, but if that doesn't work, you can execute it explicitly by "source .profile".

Test the environment variables by creating a new shell window and using the "env" command, or for example "echo $JAVA_HOME";.

JAVA_HOME and PATH for a MacOSX machine: Don't even try to Google this topic (environment variables on Mac OSX), because you will only find a confusing contradictory morass.

From the Terminal application, i.e., the UNIX shell, MacOSX works very much like Linux, except that .profile is called .bash_profile. Put the following line in .bash_profile in your home directory, creating a new file if necessary. After download from Oracle, look in /Library/Java/JavaVirtualMachines for the exact directory name in the following JAVA_HOME value. The following shows the specification valid in August, 2016.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/     (replace 101 as needed to match)
export PATH=$PATH:$JAVA_HOME/bin

Then logout and in again and check by "echo $JAVA_HOME"; in Terminal. Try the Java setup test below. This will establish the needed environment variables for command line work, i.e., commands in Terminal. However, we also need to use environment variables inside eclipse, a GUI program normally executed from an icon in the dock. There is no single way to set up global environment variables for all recent versions of Mac OSX. Instead, what we can do is either run eclipse from Terminal (cd to the directory containing file "eclipse" and use eclipse, or possibly ./eclipse, as a command), or wrap it in a shell script and put the shell script in the dock. See "eclipse on Mac" below.

Java setup test

Note: use a brand new command window or shell if you just changed your environment variables.

1. Windows    set|find "JAVA"      should output
  JAVA_HOME=c:\Program Files\Java\jdk1.8.0_xxx          or whatever you specified as the JDK directory

Linux/Mac:      env|grep JAVA    should output
  JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_xxx.jdk/Contents/Home/   or Linux directory

2. On Windows, the path command outputs the operational Path value, which is the system Path value appended with the user Path value you set up in the control panel, so near the end of the displayed path, you should see ;c:\Program Files\Java\jdk1.8.0_xxx\bin. On Linux or Mac OSX, echo $PATH  outputs the current PATH variable. You should see /Library/Java/JavaVirtualMachines/jdk1.8.0_xxx.jdk/Contents/Home/bin/  or similar in the colon-delimited list.

3. Use Windows cd %JAVA_HOME%      or Mac/Linux      cd $JAVA_HOME  to get to the Java distribution. Then use Windows  dir or  Mac/Linux ls. It should list directories bin, lib, as well as some files and other directories.   

4. java -version should output:

    java version "1.8.0_xxx"        where xx is two or three digits
    Java(TM) SE Runtime Environment (build 1.8.0_xxx)

Windows Failure case:  All set up as above, but java -version shows java 1.6.x (or some other wrong version). Look for a java.exe in Window's system32 directory.  Rename it to java_hold.exe (run CMD as administrator for this by right-clicking its icon and selection "run as administrator", or log in as Administrator and use Windows Explorer.)
Mac/Linux Failure case:  All set up as above, but java -version shows java 1.6.x (or some other wrong version). Look for java in /usr/bin. Just rename it to java_hold (cd /usr/bin; sudo mv java java_hold) . If it's not in /usr/bin, find it using "which java". This will print out where it's found on the system search path.

5. javac -version                   should output the same version, say
  javac 1.8.0_xxx
 

6. On Windows, set|find "CLASSPATH"    should output nothing. Similarly, env|grep CLASSPATH should output nothing on Mac/Linux  If it does give output, look for an old environment variable setting and delete it. 

Eclipse (Oxygen, release of summer, 2017): Eclipse IDE for Java EE Developers

It's best to have Java working first, then install eclipse, so that eclipse can find the new JDK on its own.  But if you install eclipse before Java, you can configure the new JDK in eclipse as shown in step 2. below.

  1. Install the "Eclipse IDE for JEE Developers" from www.eclipse.org. This also free.  If you have an older version of eclipse, upgrade now, because each new version has important new features and fixes. Try out eclipse as a Java IDE if you don't already know it.
  2. Make sure eclipse sees your Java 8 JDK by visiting Window>Preferences>Java>Installed JREs (Mac: Eclipse>Preferences>Java>Installed JREs), and looking for the Java JDK. If it's not there, click the Add button, and fill in:
       JRE name: jdk8 (or similar)
       JRE home directory: c:\Program Files\Java\jdk1.8.0_xxx  (or on Mac,  /Library/Java/JavaVirtualMachines/jdk1.8.0_xxx.jdk/Contents/Home/) and clicking OK.  Then make sure it is checked as the default JRE in the "Installed JREs" window you should return to.

Eclipse on Mac OSX

The Mac can get confused about what version of Java it has. If you can't get eclipse running even though you have downloaded JDK v. 8 as above, you can edit eclipse.ini to set its JDK. See https://wiki.eclipse.org/Eclipse.ini#-vm_value:_Mac_OS_X_Example. Be sure to use TextWrangler or vi to do the edit.

As discussed under environment variables above, it is challenging to get environment variables to work inside Mac applications launched from the dock. You can launch eclipse from Terminal, or put the app on your Desktop and use the resulting icon, or create a small shell file to launch it. For example, if eclipse is in your home directory's eclipse_dir subdirectory, create a file run_eclipse like this:

       #!/bin/bash
        source ~/.bash_profile
        ~/eclipse_dir/eclipse
      

Then drag and drop run_eclipse into your dock. It won't have a proper icon, but should work fine.

Eclipse tricks worth knowing:

Ant

On Linux, you can install ant using your distribution's package system ("sudo apt-get install ant"; on Ubuntu for example), or follow the following downloading instructions.

Download ant from http://ant.apache.org/bindownload.cgi, say as a zip archive, and unzip it to c:\apache-ant-1.9.6 (or current version number)

On Windows,

On Linux, define ANT_HOME and modify PATH as described above for the JDK in /usr/local/jdk8.0 (unnecessary if you used your package install)

Ant setup test:

ant -version     should output
Apache Ant(TM) version 1.9.9 ...  (or 1.9.x)

Windows set|find "ANT"  or Linux env|grep ANT  should output
ANT_HOME=c:\apache-ant-1.9.9 \bin  or … for Linux
path  or echo $PATH  should output ... ;C:\apache-ant-1.9.6\bin; ... or similar for Linux