Before we jump into programming our first Java Play service, we need a development environment.
This step is for Windows users only. If you are using Mac OS X or Linux, you may continue on to the next entry.
Before we start, an explanation
For several years now, when I have needed to develop web services using Windows, I always use Cygwin. Why? Because I cannot stand the Windows command prompt interface. Mainly because it is not Unix friendly. I am looking forward to this changing in a future Windows update that is bringing Linux command line applications to the Command Prompt. However, that is most likely far off, and until then, I will continue to use Cygwin.
Along with Cygwin, I use a tabular terminal tool called Console Z. Together, this gives me a command line environment very much like the Unix terminal you get with Mac OS X and the various Linux flavors.
Environment consistency is critical to human error prevention and efficient web service development and maintenance.
I usually switch between many OS’s on a daily basis. Consistency makes this easier. And Linux (specifically bash) is the tool that bridges each platform. Whether I am ssh’d (remotely connected) to one of my cloud servers, or running commands locally, the commands look the same. Trust me, this setup will bring you much joy.
Let’s get started
First, download Console: https://github.com/cbucher/console/wiki/Downloads
Select the correct version for you, most likely amd64/x64.
What you are downloading is a zip file. Extract this, and then move the extracted directory into the root of one of your drives. If you only use C: for your main programs, that works.
When you extract the zip file, you will be left with a directory with the version number and everything. Let’s rename this simply to ConsoleZ. Therefore, once you have moved it to C: (or another drive), you’ll have a path that looks like this:
To start Console, double click Console.exe. Create a shortcut or pin to Start if you like. You’ll be using this frequently.
Make a note of this folder, as we’ll come back to it soon. For now, please close out of Console if you have it open.
Download Cygwin setup here: https://cygwin.com/install.html
Make sure you download the correct version, example setup-x86_64.exe for Windows 64-bit.
After you download the setup application, run it and follow these steps:
- Click Next
- Install from Internet
- Root Directory: leave as default or move to another drive if you prefer
- Package Directory: default is fine
- Internet Connection: Direct Connection, unless you are on some funky network
- Randomly select a download site, for whatever reason I usually select cygwin.mirrors.hoobly.com.
- Select Packages–the most important step. Use Search at the top to find each of these, using the keyword I place within the slashes. There are sub-categories for each, which I designate before the colon.
- Web – lynx: A text-based Web Browser (needed for apt-cyg)
- /ssh/ – Devel: libssh2: SSH2 protocal library
- /ssh/ – Net: libssh-common
- /ssh/ – Net: libssh-devel
- /ssh/ – Net: libssh2-devel
- /ssh/ – Net: openssh
- /wget/ – Web: wget
- /gcc/ – Libs: libgcc1
- /gcc/ – Devel:
- /cmake/ – Devel: cmake: Cross-platform makefile generation system
- /ssl/ – Devel: *0penSSL (download both libraries 64 and 32-bit toolchains)
- /ssl/ – Libs: libxmlsec1-openssl-devel
- /ssl/ – Libs: libxmlsec1-openssl1
- /ssl/ – Net: openssl-devel
- /subversion/ – Devel: subversion: A version control system
- Phew! Finally, click Next
- Make sure the checkbox for Select required packages is checked, then click Next
- Now go grab a cup of coffee and come back in 15 minutes, since it’ll take some time for all your packages to download
- When the downloading is complete, simply click Finish. You do not need any shortcuts to Cygwin
I know, I know. That is quite a list of Cygwin packages. But, this should be a good start to getting you everything you need to have a Unix-like experience.
And we’re not done yet. But the rest of the packages will be installed in a different way.
Cygwin phase 2
What is Cygwin you ask? It is an alternative to the Windows command prompt. Many of the commands you can run on the Command Prompt can also be run in Cygwin. Additionally, you can run all the commands we just installed as well as as bash commands (<3 bash).
So, Cygwin is a command line interface (cli). When you run Cygwin, you are presented with a black window and a place to start typing. The place where you start typing is called your command line.
We won’t run Cygwin directly. Instead, we will run it through Console Z. Console Z merely adds the ability to have tabbed Cygwin terminals open. And very often you will run many tabs (just like you probably have many browser tabs open at the same time).
So, let’s finish setting up Console Z now.
Remember where you copied the ConsoleZ directory to (C:\ConsoleZ possibly)? Navigate there. Then download this file and copy it into that directory:
Make sure the file is called console.xml.
Next, edit console.xml, changing each occurrence of “A:\cygwin” with the correct location for you. Make sure to leave the rest of the path as it is, or you’ll run into some errors.
There are 3 places in the file where you’ll find “A:\cygwin.” For example, if your location is “C:\cygwin64”, then the first occurrence will change to: C:\cygwin64\bin\bash.exe.
Save and close the file.
Now, test your console configuration by running ConsoleZ.
The first time you open this up, if everything is setup properly, you’ll see messages at the top telling you that Cygwin has setup your home directory. You will only see these messages the first time you run Cygwin’s bash shell.
Cygwin phase 3
Almost done setting up Cygwin. For the final steps, we are going to install a package manager (similar to debian’s apt-get).
TIP: the keyboard shortcuts for copying and pasting do not work within Cygwin. This is because CTRL+C means something in bash that is not copy. So instead, to copy and paste into the Cygwin command line, do this:
- Copy: Ctrl + Insert
- Paste: Shift + Insert
- To drag and highlight text in Cygwin, you must hold down the Shift key when you click and drag. Then you can copy the highlighted text.
In your console’s command line, run the following two commands. Note: The “$” character denotes a command to run in your new, fancy bash command line interface.
- $ lynx -source rawgit.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg
- $ install apt-cyg /bin
There are no messages output when apt-cyg is installed. But you can test by running:
- $ apt-cyg –version
If you don’t see a message like “command not found”, you’re good to continue.
Now, let’s use apt-cyg to install two more command line applications.
- $ apt-cyg install vim
- $ apt-cyg install git
Next, let’s configure vim to work properly on Windows. But first, let me teach you how to use vim. This will be a brief tutorial on vim and editing text files within bash. Note: vim is my personal preference for file editing within bash. There are other applications you can use that are easier to learn. But I do strongly recommend learning the basics of vim. It is a very powerful text editor.
Quick vim tutorial
Why even learn vim? Well, you won’t always have local copies of the file you need to edit. Whether you are ssh’d into a local vagrant machine, local or remote docker container, or any remote virtual machine, vim will come in handy.
If you are already familiar with vim and the basics of the bash shell, you can skip this part and continue to the final section below.
From your command line, make sure you are inside your home directory.
- $ cd ~
cd means change directory. “~” means your home directory. To see the full path of your home directory, you can type:
- $ pwd
pwd means (maybe means) present working directory. ~ is a shortcut on Linux machines to your home directory. When you type pwd, it tells you what ~ actually means in your current environment. For me, I get:
Now, let’s create a test file using vim.
- $ vim test.txt
This command starts up vim and creates an empty file, called test.txt.
To start adding text to the new file, you first need to press the “i” key. This means you are ready to insert text.
When you are done typing, press the Esc key. That exits insert mode.
To save the file, you type a colon character, “:” followed by a “w”, followed by a “q”:
Then press Enter.
This saves and closes the file, returning you to the command line.
What does this mean? “:” tells vim you are about to issue a command. “q” tells vim you are wanting to quit. “w” tells vim you want to save the changes to the file.
Let’s say you make changes to a file, but then decide not to save it. In this scenario, you would type:
The “!” tells vim to discard the changes.
If you open a file, and then want to close it without having made any changes to its contents, you can simply type:
Take a minute and play with these various vim commands, creating new files and editing existing files like test.txt. Hopefully you grow a bit of a comfort level with using vim. I’ll teach you more advanced vim commands later.
Fixing vim on Windows
For whatever reason, vim is a bit buggy when it comes to the backspace key on Windows. Luckily, there is a very easy fix to this problem.
cd into your home directory.
- $ cd ~
Use vim to create a file called “.vimrc” (yes, the filename starts with a period):
- $ vim .vimrc
Press “i” to begin insert mode. Then type one line of content:
Press Esc key to exit insert mode. Then save and close the file by typing the characters :qw then press Enter.
That’s it. From now on, vim should behave properly on Windows.
You now have a Unix-like terminal on your Windows machine. Reading the rest of the articles in this development series will be much simpler, since all the commands will work the same regardless of what operating system you are using.
In this article, you installed Cygwin, downloaded several command line packages, installed Console Z, and learned a little bit about vim. From now on, if I instruct you to open a terminal window, or ask you to type the following on the command line, I am asking you to open Console and start typing.
Console is the graphical user interface that wraps Cygwin, the command line interface.