Saturday, March 7, 2020

Java EE Micro Services Using Thorntail


The Software Development world is growing every minute and as it evolves the systems are becoming more and more normalised and refined into Micro Services, this has also inspired the likes of Spring Boot and many other frameworks.

I have realised that when it comes to enterprise software most Java developers are either using Spring or  Java EE / Jakarta EE. For the who are using Spring we have Spring Boot to help us with fast project kick off and development. More to that some use it for the Micro Service pattern or methodology. What happens when you you as a developer want to use JEE? Spring Boot may not be what you are looking for and perhaps you are looking for something along the lines of WildFly.

We will have a look at a baby project along these lines, a new sidecar project supporting WildFly to enable deconstructing the application server as is and pasting just enough of it back together with your application to create a self-contained executable jar. This project is formerly known as "Wildly Swarm" and now known, as Thorntail. I mentioned Spring Boot and now you can imagine where this is going. 

1. Assumptions

Now remember that we spoke about Micro Services so the best use case for this article would be something along the lines of some sort of API. In this case we will create a RESTFul API. We assume that you have an understanding of the following tech / tools.

  • WildFly AS
  • Java 8 
  • Maven
  • JAX-RS

2. Project Setup Using Maven

Let's set up the project using maven, as our automated build tool and also  grab some wildly fractions (dependencies) we need. Worry not we will cover a little bit about fractions as we go. We will start off with some basic properties settings. These can be anything you prefer so feel free to change these properties up. Just the important one is that since this will be a web application we want to package it as a ".war" file, so keep that one as is in the example.

<name>Thorntail REST API Showcase</name>

We have more of our properties, mainly for our versions in this case. We are also now telling maven that our Java version will be 8. At the moment [07-03-2020] the latest version of Thorntail is "2.6.0.Final". So we will be using the latest version. As of Jboss 7.x we no longer really need the ".web.xml" so the tag  <failOnMissingWebXml>  also is there to make sure that we don't get failures as we will not be using the ".web.xml" file in this example. 


Maven being a very good and strong dependency management tool, it also has a very cool feature using BOM (Bill Of Materials) which pretty much allows us to access a set collection of various dependencies and hand pick the ones we need for our app. So in this case we will import the Thorntail specific BOM and then choose the dependencies we need for our JAX-RS API.


There's a Thorntail Maven Plugin maven plugin available that can help us work on packing our application much easier. Let's set that up next.


Earlier in the article we mentioned "fractions". This is a concept based on Thorntail, a "fraction" can include all or none of the WildFly components. Ultimately a fraction contributes configuration or capabilities to a runtime system. This is simply because we are loading in smaller pieces that we need, thus, Fractions. Looking at our current topic, (Thorntail) these fractions are in the form of dependencies. Let's go shopping for some ingredients that make up our REST Service API.


That's it for the project set up with maven. As you can see, the fractions we are pulling in are very basic and specific, not need for the entire heavy WildFly. We have hand picked only Swaggertogether with its own web application piece, for the Web UI and finally the core piece we need for our REST API, JAX-RSAt this point we are now ready for some code implementation. 

3. Java REST Resource Implementation Using JAX-RS API

Let's now create a Rest resource implementation using Java and JAX-RS API. 

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api( value = "/text", tags = "Text API")
@Produces( MediaType.APPLICATION_JSON)
public class SampleResource {

            value = "Gets a text message",
            notes = "Returns the message as HTTP Resposne",
            response = Response.class)
    @Produces( MediaType.APPLICATION_JSON)
    public Response get() {
        return Response.status(Response.Status.OK)
                .entity( "Hey, there, you requested for text?")

According to the JAX-RS standards, the next step is to include a class extending to define the rest @ApplicationPath and also register our rest resources. 

public class ResourceConfig extends Application {

We will be adding the second last piece to our implementation before we can test it out. We will include a class implementing the ContainerResponseFilter interface, which works like a filter for the ContainerResponse extension point on the server side, in order to filter the response message after the invocation has executed:

public class CORSFilter implements ContainerResponseFilter {
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
        responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        responseContext.getHeaders().add("Access-Control-Max-Age", "-1");
        responseContext.getHeaders().add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

Let's configure our Wildfly port and context-root just because port 8080 is so over rated, plus we will be looking at an alternative to setting up, similar to the spring boot way with the or .yml file. In your source code resources folder, add a new file project-defaults.yml

Add the following piece of yml in your file. 

    port: 8881
    path: that-service

We are now all set, it's time to build our app and then try to run it and see. Let's talk to maven to see if it can help us bootstrap the rest service.

4. Run The Project Using Maven

Build the app. Make sure you are in the root project folder where you can run your pom file.

$ mvn clean install

Run the application, remember there's no standalone application server. So we are actually going to just execute our ".jar" file as mentioned in the beginning of this article. Let's get on with it then. 

$ java -jar target/thorntail-api-thorntail.jar

NB: At this point it will boot up very quick and you should see something along these lines. Just click the image below to enlarge it.

5. Swagger Checkout

So let's check the swagger out for some basic testing and checks. Now head over to the link http://localhost:8881/swagger-uiand you should see a swagger screen with the default API loaded. 
NB: A quick look at the default swagger home page once you launch it. Just click the image below to enlarge it.

You probably want to check your API, so while you are on that swagger screen just insert the following URL into the top text box. http://localhost:8881/that-service/swagger.json and then press enter. This will load your API in the web app and you should be able to see your API as shown in the image below.
NB: Web view of what it looks like once you have pointed it to your API. Just click the image below to enlarge it.


So there you go. A standalone service using Wildfly and some Java EE, similar way you would have it using Spring Boot. We have learned how to build one and the nice part was that fractions section which allowed us to hand pick which bits of wildfly we would want. Sometimes working with a full fledged application server is necessarily bloated and heavy. Incase you wondered how you can work with Java EE in Micro Service then this is another way you can do it.  
Note: Postman example of our API test, incase you were wondering. 

You may also find the GitHub Code Base incase you want to double check our exercise. I hope this has been helpful, leave comments in the section below. 

Wednesday, January 22, 2020

Setting Up Custom PowerShell Scripts On Terminal Load



So I am used to having my own shell functions load up automatically when I open my terminal window to perform various operations on a daily basis. In other words I prefer having my own short cuts as functions or aliases for commands that I need and use on a daily basis. For those who want to know more about the reason for this you may refer to this older article

One day a landed in an organization where the team preferred using Windows OS over others. If you have read the article above you will understand that this was a different ball game for me, and obviously I wanted to have the same effect on Windows OS. Yes with the latest Windows OS 10 one can set up bash, but then how would I ever learn something new, in this case PowerShell.

Oh well ... 

Let's Get Started

We will be looking at a step by step process of setting up  your own custom PowerShell functions which will load automatically every time you open your PowerShell terminal. This will simplify working with those long complex commands which you probably need to use every day.


1. Locate Or Create The Default Folder

  • Open up Windows Explorer and head over to the "Documents" folder.
  • Look for a folder with the name "WindowsPowerShell". If the folder is not there then create it. This folder is the home / default directory for your PowerShell profile. Make sure you use that specific name.
  • Open the folder. Just note that if you did not have to create this folder and it was already there then it will probably contain a have a sub folder named "Scripts", which you can ignore right now.

2. Creating Your Custom PowerShell Script / Function

  • While you're still in that "WindowsPowerShell" folder, create a new PowerShell script file i.e I created mine with the name and extension Message-Printer.ps1 and please note that you can name it whatever you want, just keep the extension ".ps1" and that's it.
  • Open up the file in your favorite text editor or IDE, I prefer VS Code for most of my scripting. I also think that in this case it works out because it's a Microsoft IDE which means there's good support for PowerShell out of the box. So go ahead and open that file to start editing the as follows: 

function showThisMessage() {
    # Print out the message
    Write-Host "Welcome to PowerShell automation!"

  • Ultimately the following image shows how things should be on you machine at this point. Have a look to confirm and then we can carry on.

3. Configuring Auto Load Of Your Script  

For each time you open your powershell terminal, there's a session that gets created and that session has some sort of profile that it's running with it. When a user does not have any custom profiles setup then they can work with the default one. For example we are going to work with a default one which will be activated when we open up the PowerShell terminal. 

NB: You may click the following link for more info about PowerShell Profiles. Let's get to the work...

  • Create a new file inside your "WindowsPowerShell" folder with the following name, Microsoft.PowerShell_profile.ps1, keep the name of file exactly like that since that's the default profile naming.
  • Open up the file in your favourite text editor or IDE.

 # This code may show as two lines but, please write it in one line.
 Get-ChildItem -Path $PSScriptRoot\Message-Printer.ps1 | Foreach-Object { . $_.FullName } 

  • For more validaton you may refer to the image below: 

  • So what's happening in that line is basically a search for our new custom script, "Message-Printer.ps1". This search will then import this script into your PowerSehll session by default when a new PowerShell terminal tab is opened. This is the PowerShell sytax and way of loading other scripts. It's achieved through the power of the Get-ChildItem commandlet which is used to get the specified file locations on the file system. 
  • The actual path is picked up dynamically with the $PSScriptRoot part of the script and then later we can suffix with our file and its extension.
  • And finally the Foreach-Object commandlet used to perform the same operation on a collection of files.

4. Running The Custom Script

Great! So now make sure you have saved all your files from your editor or IDE and then give your script a test.
  • Open up the PowerShell terminal.
  • Immediately start typing the function you created earlier. So just type showThisMessage in your terminal tab followed by the "return" or "enter" key ... and BOOM! Your text show be displaying as follows: 




So there you go! You now know to automatically load up your custom PowerShell scripts. Hoping it was helpful and many of you will try and use this method. 

Wednesday, June 22, 2016

My Transition To UNIX From DOS: Working With Aliases


It's been fun playing on a Windows OS machine as a Software Engineer. I always find it interesting and fun to explore other things. Funny enough though, I have always been scared of using commands because I always felt that I would mess up something on my machine and have to do a factory reset of some sort. It's always a drag when one has to do that on their work machine. Finally I landed at a place where the recommended OSes were the unix based ones, preferably, Linux Ubuntu or Macintosh OS. Ultimately I went for the Macintosh OS, just because... 

From my first day there I had to learn some basic and common unix commands from the likes of: tail -f /file/path/here up to, ssh At some point I got a bit fed up with typing a long and same old command everyday and to a point, every hour even. Even though it was great practice, something had to be done and that's when I learned about Unix aliases. Aliases are pretty much abbreviations of FUCs "Frequently-Used Commands" 

For example one can shorten a command like: defaults write AppleShowAllFiles NO, which toggles hidden files & folders off, in Mac OS's finder, to something like show-hidden-files which will essentially be the name "Alias" of that command. 

Now to the "Nuts & Bolts", we will look at the following:
  1. Creating / Adding a new alias
  2. Referencing an external file for aliases.
We will also need some "shell scripting skills", nothing major, very few lines of code so that's all fun and well

1. Creating / Adding a new alias

Perhaps this may be slightly different on other unix platforms, so this is more pertinent to Mac OS. Let's go...!
  • Open finder and head to your home directory. (Unless you are in your home directory by default) 
  • Look for a file named, ".bash_profile". Notice the dot before the file name. If you cannot see hidden files then use the command (in your terminal)  from the example above and just change that "NO" to "YES". Then hold down the "alt" button and then "alt-click (right-click)"  on the finder icon and then "left click"  the option "Relaunch".
  • Go back to looking for our file and then open it in any of your favourite text editor... (Sublime Text, Atom, even VI on your terminal etc ...)
  • Let's add a new line, this will be a new alias for any command you want to add, for example:  

  alias show_files_no="defaults write AppleShowAllFiles NO"

  • To delineate things a bit more, "alias" is a reserved key word for shell, so you are letting shell know that you want to add a new alias, the next word "show_files_no" can be anything you want, this is the actual name or alias of your command. Then last part "defaults write AppleShowAllFiles NO" is the actual shell command that you would normally type and execute on the terminal. We are almost done. 
  • If you were editing the file using the terminal with VI, VIM, Nano etc... The next thing is to reload the ".bash_profile"  by either closing and opening the terminal again or typing the command "bash -l". I believe there may be more commands out there that one can use. 
  • Finally open terminal if you closed it. If you reloaded the ".bach_profile"  using the command then just type the alias you added recently, in our case just type "show_files_no". This will execute the command associated with that alias. To see if this made an effect just follow the steps about relaunching finder above. That's it!

2. Referencing an external file for aliases.

I have recently been playing around with externalising some bash profile stuff because I have a lot of aliases and my bash profile artifact was just getting too congested. So main things to note here is that
... you don't have to create your new external file inside the same directory as your bash profile and the name of your file does not have to start with the word "bash"...
I just named it that way for the sake of naming it that way! So let's get to it:
  • Create a new file as follows, "~/bashes/.whatever_file_name", of which in my case, is inside a new folder that I created, "bashes", in my home directory and named it ".bash_aliases". Keep in mind that your folder can be named anything you want.

  • Now go back to your main file, ".bash_profile" and then replace your alias with the following shell scrip code.

  #Referencing path to file containing the aliases
  if [ -f $aliasesPath ]; then 
     source $aliasesPath

  • Something along those lines should help you out. It's basically a shell if statement that checks if that file path exists and if so we then reference it from the main ".bash_profile" so next thing you should try is to now reload like we did earlier if you did all this using terminal or just close and then open terminal.  
So now you have externalised the aliases and you can try with other stuff like your environment variable profiles can also be externalised and so forth. Like I said this is the first main thing I learned when moving to UNIX, I hope it helps someone out there. I would really love to learn from you on how I can improve this post and some feedback, Cheers! 

Tuesday, February 11, 2014

Starting with Acitiviti (BPM Engine)


I am busy working on a project which has a very small workflow or business process component to it. One of the requirements was that we had to use Activiti to manage the business processes. My first port of call was the "Activiti in Action" book. When I felt that had enough ammunition to take on Activiti, I gave it a go.

There were a few bumps along the road especially because I was trying to incorporate Acitiviti into an existing Spring Maven application. In this blog I am going to show you how to get your Activiti code running in a Maven Spring application.

Activiti Architecture

The Basic Activiti architecture is pretty simple. There is a database which is used to maintain and persist state. You can manipulate the state of objects within the database by using the Activiti engine or API. This can be the Activiti libraries or dependencies in your application or you can also interrogate the database using the Rest services.

The Activiti Explorer is a web application which uses the API to view and change objects housed in the database. The Activiti Explorer also has a tool (Activiti Modeler) which can be used to model BPMN processes. You can model your process using this tool and then export the configuration into an xml file. You can then use this xml file in your application without having to model the process manually. 


You will need to download Tomcat and the latest Activiti Release. The only 2 artefacts of concern to you for this tutorial can be located in the wars directory:
  • activiti-rest.war
  • activiti-explorer.war 
You can deploy these 2 war files on your Tomcat server. To be really specific, you only need the activiti-explorer. You might need to add the H2 database Driver to your Tomcat server.

Do I need a database?

Activiti does use a database but you do not need one explicitly. Activiti is packaged with an embedded H2 database. This means that when you run the application, it will access this embedded database automatically. So even though you are not specifying a database, there is one being used.

Add these dependencies to your Maven POM file

     <!-- Activiti Stuff-->  
 <!-- Spring Stuff-->  
 <!-- H2 Driver-->  

Add this repository to your Artefact repository or your Maven POM file

       <id>Alfresco Maven Repository</id>  

Design a Simple Activiti Workflow process using Activiti Modeler

This is the sample process that I designed. It is simple and self explanatory.
Here is the exported xml file contents (SampleActivitiProcess.bpmn20.xml):
 <?xml version="1.0" encoding="UTF-8"?>  
 <definitions xmlns="" xmlns:xsi="" xmlns:activiti="" xmlns:bpmndi="" xmlns:omgdc="" xmlns:omgdi="" typeLanguage="" expressionLanguage="" targetNamespace="">  
  <process id="SampleActivitiProcess" name="SampleActiviti" isExecutable="true">  
   <startEvent id="start" name="start"></startEvent>  
   <userTask id="SomeUserTask" name="SomeUserTask"></userTask>  
   <endEvent id="end" name="end"></endEvent>  
   <sequenceFlow id="flowFromStart" name="flowFromStart" sourceRef="start" targetRef="SomeUserTask"></sequenceFlow>  
   <sequenceFlow id="flowToEnd" name="flowToEnd" sourceRef="SomeUserTask" targetRef="end"></sequenceFlow>  
  <bpmndi:BPMNDiagram id="BPMNDiagram_SampleActivitiProcess">  
   <bpmndi:BPMNPlane bpmnElement="SampleActivitiProcess" id="BPMNPlane_SampleActivitiProcess">  
    <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">  
     <omgdc:Bounds height="30.0" width="30.0" x="165.0" y="210.0"></omgdc:Bounds>  
    <bpmndi:BPMNShape bpmnElement="SomeUserTask" id="BPMNShape_SomeUserTask">  
     <omgdc:Bounds height="80.0" width="100.0" x="301.0" y="185.0"></omgdc:Bounds>  
    <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">  
     <omgdc:Bounds height="28.0" width="28.0" x="495.0" y="211.0"></omgdc:Bounds>  
    <bpmndi:BPMNEdge bpmnElement="flowFromStart" id="BPMNEdge_flowFromStart">  
     <omgdi:waypoint x="195.0" y="225.0"></omgdi:waypoint>  
     <omgdi:waypoint x="301.0" y="225.0"></omgdi:waypoint>  
    <bpmndi:BPMNEdge bpmnElement="flowToEnd" id="BPMNEdge_flowToEnd">  
     <omgdi:waypoint x="401.0" y="225.0"></omgdi:waypoint>  
     <omgdi:waypoint x="495.0" y="225.0"></omgdi:waypoint>  
I would advise you to always change the ID's of all of the elements and also provide them with descriptive names. This helps you out tremendously when developing against your BPMN process.

Let us start Coding

The general process of running an Activiti process is as follows:

Design BPMN Process

You can use Activiti Modeler from Activiti Explorer, the Eclipse Activiti Plugin or manually build the xml representing your business process flow.

Deploy Process

You can use Activiti Explorer to deploy or programatically deploy your business process flow. Doing it programatically, you have 2 options:

  • The Spring way
  • The Standard java way

Run Process Instance

You reference a process which is deployed and start a new process instance.

Create Activiti process engine

The are 3 ways to achieve this:

In Memory

 ProcessEngine processEngine = ProcessEngineConfiguration  

This is the easiest and fastest way to get up and running. It references the embedded database and uses the default configuration.

File based

 ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();  
The database connection settings are referenced from the activiti.cfg.xml

Here is a sample file refencing the embedded default H2 database:
 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns=""  
   <!--<bean id="processEngineConfiguration"  
   <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">  
     <property name="jdbcUrl"  
          value="jdbc:h2:/tmp/activiti;AUTO_SERVER=TRUE" />  
     <property name="jdbcDriver" value="org.h2.Driver" />  
     <property name="jdbcUsername" value="sa" />  
     <property name="jdbcPassword" value="" />  
     <!-- Database configurations -->  
     <property name="databaseSchemaUpdate" value="true" />  
     <!-- job executor configurations -->  
     <property name="jobExecutorActivate" value="false" />  
     <!-- mail server configurations -->  
     <property name="mailServerPort" value="5025" />  
     <property name="history" value="full" />  

Basic Spring Style

     ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Service-core-application-context.xml");  
     ProcessEngine processEngine = (ProcessEngine) ctx.getBean("processEngine");  

Here is the Spring configuration required:
   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
     <property name="driverClass" value="org.h2.Driver" />  
     <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
     <property name="username" value="sa" />  
     <property name="password" value="" />  
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
     <property name="dataSource" ref="dataSource" />  
   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
     <property name="databaseType" value="mysql" />  
     <property name="dataSource" ref="dataSource" />  
     <property name="transactionManager" ref="transactionManager" />  
     <property name="databaseSchemaUpdate" value="true" />  
     <property name="deploymentResources"  
          value="classpath*:SampleActivitiProcess.bpmn20.xml" />  
     <property name="jobExecutorActivate" value="false" />  
   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
You can see in this way, Spring handles deploying the BPMN Process for you.

It is important to know that BPMN Process xml files can only be of 2 types and have to end in the following extensions for the API to process / deploy the files:

  1. .bpmn20.xml (Activiti Modeler)
  2. .bpmn (Eclipse Activit Plugin)
The 2 services that we require is the RuntimeService and the RepositoryService:
 RepositoryService repositoryService = processEngine.getRepositoryService();  
 RuntimeService runtimeService = processEngine.getRuntimeService();  

Remember that if you are using the Spring way, the respective beans already exist.
We use the RepositoryService to deploy the process, if it has not already been done (the spring way or via the Activiti Explorer console):

We then start a process using the RuntimeService:
 String procId = runtimeService.startProcessInstanceByKey("SampleActivitiProcess").getId();  

A process is considered to be active if it is not in an end state or not suspended. You can use the Task Service to manipulate active tasks:
 TaskService taskService = processEngine.getTaskService();  
 List<Task> tasks = taskService.createTaskQuery().active().list();  
Remember that the TaskService bean already exists if you are using the Spring way.

You can use the TaskService to query, claim and complete tasks:
 taskService.claim(task.getId(), "sampleUser");  

Once the Process in a Final state, you can't use the TaskService for these processes but need to use the HistoryService:
 HistoryService historyService = processEngine.getHistoryService();  
     HistoricProcessInstance historicProcessInstance =  
     System.out.println("Process instance end time: " + historicProcessInstance.getEndTime());  
Remember that the HistoryService bean already exists if you are using the Spring way.


This should be enough information to get you up and running with Activiti. Remember to start off small and build on incrementally. This way you can understand the process in much greater detail.
Activiti is a great tool and what is even more amazing is that this piece of software is open source and free to use.

As always, I would love to hear your comments or questions. You can also follow me on Twitter or Google plus. I have added shortcuts to my profile in the About me link.

Tuesday, January 21, 2014

Back to Java Basics - Installing Java

I can't remember learning how to install java at varsity. Maybe, the instructions were on the first tutorial and I never had to bother with it again or it might have just been installed for us. The point being; when I started at my first job and had to install java on both my Windows machine and my allocated workspace on a Unix, I just could not get it right.

Was I crazy?

This might have been the case but then again whenever I feel an inference in the direction that I am crazy, I often find solace in believing that everyone else in this world is crazy and I being the only sane one. Moving on ... I did google the usual "How to install java on windows" and followed the instructions meticulously but still it did not work. You can imagine how I felt when I had to ask one of the seniors to help me install java. It was not straight forward for him as well. He spent about 30 minutes to an hour. Messing around with environment variables through command line and all kinds of weird nonsense. Eventually he got it working and I was amazed. My first thoughts were that if getting java installed was so difficult, how would I cope as a java developer? It turned out that the windows machine I inherited belonged to a techie who installed every possible software he could get his hands on. You might be asking: but this has nothing to do with installing java???

Oh, you are so wrong. I eventually got to understand the sorcery behind installing java. Up to this day I still find senior java engineers struggling with this concept but in a different problem space. As a practising consultant we often have to work with systems that are running in production with multiple versions of java installed but running some undesired version. I have seen even the most senior engineers spending hours trying to sort this mess out.

By contrast to the title, I am not going to show you how install java. I am going to clear up all the mystery that surrounds:
  • Installing java step by step and it just not working for you
  • Having installed a specific version of java but when running "java -version" it says that it is running some other obscure version

Understand Environment Variables

It is of utmost importance that you understand how environment variables work. I have written an article and would suggest that you read this article very carefully and become comfortable with the concepts before proceeding:

Setting Environment Variables on Unix and Windows

If you reached this point, you are really comfortable with environment variable ;-)
Cool, let us continue...

Installing Java

For me there are just 3 important parts to installing java:

  1. The jdk package (jdk folder)
  2. Setting the JAVA_HOME environment variable
  3. Updating the path variable to reference the jdk binary or executables

Point number 2 is not even required, you will understand later.

Once you have the jdk package (folder containing bin, include, jre, lib & Other files) you are 90% there. If you type java through a command line at this point, unfortunately java will not work. The only thing required here is to get the operating system to reference the java binary or executables. If you have read my other post, you should have realised that I am talking about setting the path variable. All we need to do is add the path / location of the bin directory to the path environment variable.

Now, if you try and run java from a command line it should work. If you have followed my instructions correctly and it is still not working then; open up a new command line terminal (fully explained in my other post) or this is were things get really interesting.

Why is the JAVA_HOME environment variable not important? 

The JAVA_HOME environment variable would contain a reference to the base directory of jdk. When setting the path, it is advised that you only add JAVA_HOME/bin so that the operating system can find the binaries executable files. There is no functional difference by specifying the full directory path to the bin directory. It is merely a standard and preferred convention.

Why the convention?

The convention is there so that if you reference an alternate version of java, you would only need to update the JAVA_HOME environment variable. The path variable is quite long containing multiple references to different executable files. It can become "difficult" working with this long string and a typo can affect some other program.
So in a nutshell, the JAVA_HOME environment variable is a convenience variable for the purposes of this article. For your broader knowledge, the JAVA_HOME environment variable might be referenced by programs that require java. Certain web servers or application servers might need this variable be set in order for it to function. It uses this variable to reference libraries or files for it to function correctly.

Installing java today(The Interesting part I spoke about earlier)

I have noticed that when you download a java jdk today, it is packaged with an installation procedure. This procedure completes the process mentioned above on your behalf but even if you use the installer, you might still not get java to work or an undesired version might be running.

Other programs mess with the path variable 

There are so many programs, applications, servers and processes that require java. When installing them, they either reference the java installation already installed on your machine (good program) or contain it's own minimised / customised jdk or jre. If they are installed and append to your path variable, you are done! What happens is that every time you run a java comand, the system evaluates the path variable from left to right and looks in each directory for an executable called java. It will find the first occurrence, which in this scenario is a minimised / customised program specific one execute and terminate. I have personally been bitten by this when installing Oracle server and even some profiling tools.
How do we fix this?
I specifically highlighted, bolded, increased font size and underlined the word "add" earlier in this post. When most people hear add they think that it is append(put at the back). You can even prepend(put in the front) the path variable. This is a very cool trick. Now you have a way of guaranteeing that your desired java executable is executed first. Prepending the path variable saves you from figuring out which reference is problematic. You can figure this out by removing the references one at a time up until you get it working.


The path variable can be a source of major headaches but if you understand both environment variables really well and what is required to install java you should be fine. You would also be comfortable hosting multiple versions of java and be able to reference a specific version if and when required by merely changing your JAVA_HOME environment variable.

As Always, I would love to hear your comments or suggestions on how I can improve this post. I am also more than willing to lend a hand if you are having issues in your environment!

Wednesday, January 8, 2014

Setting Environment Variables on Unix and Windows

In this post I will try and explain the different ways on how to set environment variables on both Windows and Unix operating systems. I am also going to address the scope when setting an environemt variable.

What environment variables are set on my machine?

You can easily check this by typing "set" into a command line terminal on both windows or unix.
Unix and windows:
How do you retrieve the value of a particular environment variable?
echo $environment_variable
echo %environment_variable%

What are environment variables?

Environment variables are operating system level variables that can affect the running of a process. This meaning is slightly obscure but let me try to explain it the way I understand it. The way I see it, environment variables are just aliases for file or directory paths. All they are are just short names or references to a directory on your system. The reason for their existence as you may have realised by now is that they are dynamic and are uniformly used in programs (ie. no hard coding paths).

There is however one special environment variable. This is the "path" variable. The path variable contains multiple directory locations separated by either a ";" on windows or ":" in unix. Each directory location references a program's binary files or executable files. As an example, if you were to type in "java" on the command line interface of both windows or unix, the OS would search each of the directories in the path variable and identify the executable or binary with the name java. When it identifies the executable matching the name, it will execute the process and terminate. This is a very interesting and important statement.

  1. The path variable is evaluated from left to right scanning each directory for an executable matching the name.
  2. It executes the first occurrence of the executable and terminates.
  3. If no matches are made, a command not found message is returned.

How to set environment variables? 

Environment variables can be set either programmatically or manually with both being equal but they can be set for different levels of scope.

Global Scope

This is the highest level and is available to all users of the system. 
On unix this can be achieved by adding the environment variable to /etc/profile or /etc/profile.d file. On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a System Variable.
You will only be able to set a variable on this level if you are an administrator on the system.  

Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent.

User Scope

Environment variables set at this level is only available to the user that is logged in.
On unix this can be achieved by adding the environment variable to /Users/<username>/.bash_profile file. Once an environment variable is set, you would need to log in again for the change to take effect or you can execute the .bash_profile script which will set the environment variable for the open session.

On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a User variable.
Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent for the user.

Session Scope

Environment variables set at this level only last for the duration that the command line terminal is open. It is lost as soon as the terminal is closed.
On unix this can be achieved by typing the following in a command line terminal: 
export VARIABLE=value  # for Bourne, Bash and related shells
setenv VARIABLE value  # for csh and related shells
On windows this can be achieved by typing the following in a command line terminal:

Script Scope

Environment variables set at this level only last for the duration that the script runs. It is lost as soon as the script terminates, unless the script is executed from command line terminal. In this case the session scope rules apply.
Same as in the session scope but is embedded in a shell or batch script.

Setting the "path" environment variable

All of the above applies to normal environment variables. Remember we said that the "path" variable is special and contains multiple directories. You can edit the path environment variable in the same way as mentioned for normal variables as above for the most part. 

Here are other variations which might be useful when setting the "path" environment variable.
When setting on windows use the ";" symbol as a separator:
SET path=C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the ":" symbol as a separator:
export path=/interesting/informing/techies:/important/informing/techies
If you wanted to append or prepend a new directory location to the existing "path" environment variable:      
When setting on windows use the %VAR% notation:
SET path=%path%:C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the $VAR notation:
SET path=$path:/interesting/informing/techies;/important/informing/techies

Remember prepending or appending to the path variable makes a huge difference!!!

Who uses and maintains environment variables?

Applications and programs use environment variables. They also maintain the environment variables by doing the necessary creations, deletions and updates during installation, updating and uninstalling processes. 

Therefore, messing around with environment variables without a clear and proper understanding can cause your system not to work as desired. My suggestion is that if you do want to test something out, try it out on the session scope. In this way your system is not permanently affected. 

With this said, you can also use environment variables in your applications. They are great at abstracting paths and are also a solution to hard coding. 


I hope that this gives you a clear understanding of environment variables and their scope. As always, I would appreciate any feedback and comments are always welcome.

If you have issues in your environment I am more than happy to lend a hand!

Wednesday, December 4, 2013

Installing mysql database on a unix/linux box

There are a few tricks involved with installing mysql on a unix machine. There is the installation part and then the part where you need to connect to the mysql database server.

PS. This article assumes that the unix machine has internet access.

Step 1

Download and install mysql database server.
This command will download mysql server from the internet and install it.
If you have root access:
 yum install mysql-server  
If you do not have root access:
 sudo yum install mysql-server  

Step 2

Start the mysql database server as a unix service.
If you have root access:
 service mysqld start  
If you do not have root access:
 sudo service mysqld start  

Step 3

Check the status of the mysql database server.
If you have root access:
 service mysqld status  
If you do not have root access:
 sudo service mysqld status  

Step 4

Check that you can connect to the mysql database from the same machine.
 shell>mysql -u root  
Then run a simple query:

Step 5

Secure your mysql database server.
Run this shell command on the server:
This is an important script. It allows you to set the root password which we need to do and other tasks like removing anonymous access.
You can re-perform step 4 but this time remember to supply your password:
 shell>mysql -u root -p<password>  

Step 6

Check that you can connect to the mysql database from the same machine.
 shell>mysql -u root -p<password>  
Then run a this query:
 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'  
   IDENTIFIED BY PASSWORD 'your_password'  
The query allows remote access from any location where the user is root and the password. You can replace % with a hostname or ip address. You can also use it as a wildcard.
Remember to flush privileges:

Step 7

Use mysql workbench to connect to the mysql database server.

Additional Notes

Shut Down the mysql database server.
If you have root access:
 service mysqld stop  
If you do not have root access:
 sudo service mysqld stop  

Leave a comment if you feel that there is anything that I have left out.