Click on images to see them in the natural size

Java and Cygwin under Windows 7

Jan Labanowski, Computational Chemistry List, Ltd.

I examine here the relation between Java and Cygwin. The main reason for it is that Cygwin is a Linux like environment that runs under Windows, and makes you feel like you are on the Linux computer, while Java does not know anything about Cygwin and it only knows that it runs under Windows. Since Cygwin uses different convention for file paths (forward slashes / and represents Windows drives as directories under the tree like file system) than Windows (back slashes \ and drives on the file system that is not a topological tree), there are some problems. Moreover, Windoz has this preoccupation with the directory names that have spaces in them, while Java and Cygwin often die on those (they come from the Unix world where nobody of sound mind would use spaces in file names, since they require quotes and create overhead).

For the newbies, let me reiterate a few things. The Java comes in two main varieties:

As with other popular software, the Java is constantly updated and you can have several different versions of JDKs and JREs on your computer. So it is important to keep track of what version you are actually using. The situation is aggrevated (or helped, as some would say) with the frequent Java updates that are installed on your Windoz computer by automatic updates from the Oracle site. They can mess up your defaults and even lead to the situation when the stuff does not work anymore.

You can keep many version of Java and use them selectively, but you need to manage it. So, it is prudent to do some clean up before you decide to install a new Java.

If I start from scratch, I go to my [Control Panel] and Uninstall all JREs and JDKs that are there. It is trivial to reinstall them. Go to [Control Panel] from your [Start] menu, click on Programs/Uninstall a program, find your Javas and right-click on them to uninstall them. Note that you have to have Admin privileges to do so.

Uninstall JRE

Uninstall JDK

Now Javas are gone...

Before you download and install the Java, it is prudent to do some clean-up. First, see your User environment variables. They hide the damned thing really well. Click on [Start] button and type environment into a search box at the very bottom: Search programs and files. This should come up with the links:

ChangeYourUserEnvironment

Click on the link Edit environment variables for your account and you should see the Environment Variables box:

Note that the bottom box for System variables has the [New]/[Edit]/[Delete] buttons greyed out and you cannot change those as a regular user. You can only change these variables as Admin. But the User variables can be edited. Make sure that you do not have there any Java stuff. The TEMP and TMP things are OK, and the %USERPROFILE% on Windoz resolves C:\Users\YOUR_USER_NAME in my case C:\Users\jkl. This is kinda coresponding to the Unix HOME directory for a user. If you have other stuff there, make sure that you know what you are doing and what it does. The User environment variables OVERRIDE the SYSTEM variables and it is sometimes hard to detect what the heck is going on.

The next piece is reviewing (and getting rid of) the Java related stuff in SYSTEM and ADMIN environment variables. Note, in my case the ADMIN is a User with id root. Makes me easier to remember. Due to Windoz proverbial security, I try to be root only when I absolutely have to. This helps a bit, though it is annoying since there is no sudo on Windoz. It is quite likely that uninstalling Java did not take care of all the lint. The automatic updates of JRE from Oracle (disable the thing if you can) often improve the Windows %PATH% and %JAVA_HOME% The updates often put something like C:\ProgramData\Oracle\Java\javapath; in your windows %PATH% environment. The javapath folder contains the links to the most important Java programs:

If you find that the links in the javapath directory point to nonexisting files, just remove the directory. You actually should always remove this directory when you do not have Java installed on your machine (i.e., at the moment, where we are in this document). To modify the SYSTEM and ADMIN environment variables you will need an Admin Password. Remove all the variables when there is no Java on your machine. Sometimes, this will not be enough, since some Java stuff may exists in your Windoz Registry and you would need to use regedit to get rid of this stuff, but let us not go there here, since you would need to make sure to understand the risk (you can easily mess up your Windows installation with regedit if you are not careful and knowledgable). I give you below some examples of how to modify (and delete in this case) the Java related environment variables before you do a clean install. Go to your Windows Environment variables as: [Start] -> [ControlPanel] -> [SystemAndSecurity] -> [System] -> [AdvancedSystemSettings] and you should see:

Click on [EnvironmentVariables] Add the JAVA_HOME if you know what it will be, or just delete if for now, and add it after you install the JDK.

Edit the %PATH% variable (Do not delete it!):

and remove the Java related stuff. You can also later on add the Java bin directory, once you know what it is.

Remember to click on all [OK]'s for this thing to be saved. Then, as usual, you need to reboot your computer. Note that people who design Windoz have a habit to put the most important pieces of software under directory paths that contain spaces in the name. This is a nuissance, since such paths have to be quoted on the command line and many scripts assume that nobody of sound mind would put a space into a directory name. For this reason, when I cannot install the stuff into directory paths without spaces, I use a trick. I routinely create links (junctions) to these directories and make them accessible through the paths that do not contain spaces. Then I use these links rather then original dirctory names in the environment variables. For example, the notorius Program Files is linked in my root directory on my C drive as:

You create them with the Windoz mklink /j command. For details check my Cygwin write-up.

Time to install JDK

Now, Java is gone... It is time to download new Java I usually go to Google and search for Java SE JDK download. and click on the top link, whatever pops off (they change it a lot). Then I take the latest stable Standard edition JDK (not JRE). Nowadays, the title is usually something like: Java SE Development Kit 8uXX.  I accept the license agreement and click on a link under Download to get the version corresponding to my OS. You need to have an account on Oracle OTN site, but it is free, and they do not really send you spam. For this writing it is Windows 7 Professional x64. and the JDK version was 8u31.

From the Oracle's site I just download a file, (in this case jdk-8u31-windows-x64.exe) and ran it, since it is an .exe file.

It is a Windows Installer. Unfortunately, gone are the times that you could easily install the JDK on Windows without the Admin Password (when they were distributing JDK as a zip file). It is still possible to install Java without Admin privs, but you have to go through a lot of hoops, and you do it only when you really have to do it (e.g., you do not have the admin password). You can do it with stuff like cabextract but it is easy to mess up. in this case, I install the JDK at home on my own Windoz box that has the same version of Windoz. I may have it already installed under C:\Program File\Java\jdk1.X.Y_X. I just zip the jdk1.X.Y_X folder (make sure the top dir in the archive is jdk1.X.Y_X) and put it on my flash drive. Then I unpack it on the other computer in the directory where I have the write privileges. You are not usually asked for Admin passwords if your security guys allow unzipping files. Make sure that your path does not have a space in it (say C:\Users|JoeSchmoe, but look below for some ideas). You will need to set the %JAVA_HOME% environment to C:\Users\JoeSchmoe\jdk1.X.Y_X and prepend or append (whetever works, depends on the patch level of Windoz) the %JAVA_HOME%\bin; to your Windoz %PATH%. If you do not know how, read on, since I talk about it later. But for now, I assume you have Admin access. Right-click on the Java installer that you downloaded and click on [Open]. You will be asked for an Admin Password at some point.

They will Welcome You... Click on [Next] and stop and think and watch..

The proposed installation directory is a nightmare. It has spaces in it. It will make your life miserable. So click on [Change] and choose something that does not have spaces.

I clicked on [Change] and selected the C:\Jdk1.8_31:

Clicked on [OK] and checked if things go as planned:

After clicking on [Next] the installation started

After the JDK is installed, the installer will want to install the JRE and you need to choose the installation dirctory again. As before, click on [Change] to be given a chance.

Note that I did not have the needed folder created, so I had to choose the name.

And then

And finally, so now I can click [OK].

The stuff will go to the directory that I want, so [Next]

The installer got working again:

At this point, I choose Close, or they will show me Web pages about Java, and I can look at them later.

Now, after all this, you should see the JDK and JRE installed in the Control Panel:

We should also verify what the DOS Command Prompt is showing up:

Not unexpectedly we got a bummer... javac not found. The reason being that the entry for Java in the Windoz %PATH% is C:\ProgramData\Oracle\Java\javapath; and there is no link to javac in this directory:

The JDK installation may (or may not) add the entry for JAVA_HOME but it surely does not add the directory with javac to the %PATH%. There are many ways to fix this, but I usually do it by appending the JDK's bin directory location to the %PATH% variable for the regular user (here jkl). I try to limit the number of programs that can be fired by the Admin to the minimum. I have enough problems with Windoz security and do not ask for more. The Admin does not really need javac, since I do most of my development work as a regular user. If the Admin needed the access to JDK I would just replace the C:\ProgramData\Oracle\Java\javapath; with the C:\Jdk1.8_31\bin. If you come from the Unix world it may seem strange that I want to append the %JAVA_HOME%\bin to the Windoz SYSTEM %PATH%, since you would probably want to prepend it. The caveat is that on Windoz, adding a private User's %Path% environment variable like %JAVA_HOME%\bin;%Path% will not survive a reboot. The command line SETX utility will likely not work either on newver versions of Windoz. This is a security the Windoz way. The only way is to append to a SYSTEM %PATH%. Period. In other words, when you execute java, it will come from the C:\ProgramData\Oracle\Java\javapath directory via a link to %JRE_HOME%\bin (i.e., C:\jre1.8_31\bin in our case). If you execute javac it will come from %JAVA_HOME%\bin (i.e., C:\Jdk1.8_31\bin in our case). You cannot jump higher than your butt, so I am appending to the %PATH%. So here we go again: clicked on [Start], entered environment in the search field, clicked on Edit environment variables for your account and familiar dialog appeared.

Then I appended the JDK's bin to the %Path%

Then I clicked [OK] as many times as was needed to close all these dialogs.

Now, the SYSTEM %Path% will be what it was originally (the javapath directory at the front) plus the appended bin directory of JDK. When I log in as jkl I should be able to find files in the JDK's bin directory and have my %JAVA_HOME% set. But of course, this will all happen after I perform the Windoz Reboot Ritual. This is what I saw in the DOS window:

And this is what was showing up in the startup bash terminal of Cygwin.

And this was showing in the Cygwin's xterm

Of course, we need to check if this thing actually works. I grabbed some short test from Google and saved it in the file: SwingHello.java

import javax.swing.JFrame;
import javax.swing.JLabel;

// Stolen from http://www.javabeginner.com/java-swing/java-swing-tutorial
// It is short enough and will test if Java works under  Cygwin.

public class SwingHello extends JFrame {

    public static void main(String args[]) {
        new SwingHello();

        System.out.println("There should be Hello CCL Window showing up");

        }
    SwingHello() {
        JLabel jlbHelloWorld = new JLabel("Hello CCL");
        add(jlbHelloWorld);
        this.setSize(200, 200);
        setVisible(true);
        }
}

Then I did javac HelloSwing.java to compile. Then, in DOS window (on the left) and Cygwin's xterm on the right I type proverbial java HelloSwing. And you can see what happened.

And this concludes the write-up on Java and Cygwin and Windoz. While everything here is elementary My Dear Watson, the devil is always in the details.

Jan Labanowski
jkl at ccl dot net

P.S. If you saw something totally idiotic, let me know. If you liked it, share it with others.