This is the second part of the “Installing Glassfish 4. on Ubuntu” series. In the first part we have installed the “original” Oracle Java Runtime to be able to run Java programms on Ubuntu. In this post we will see how to install the Glassfish 4 application server on Ubuntu (server). It is required that you have Java up and running.

Downloading Glassfish

The first step is to download the Glassfish server onto our machine. You can download the newest version from the official Glassfish site and just by pasting the following code in a terminal:

wget http://download.java.net/glassfish/4.0/release/glassfish-4.0.zip

Extracting Glassfish

The next step is to unzip this archive by the use of the “unzip” command. Not everyone has the tool already installed on his system, so if you need to install it, just run following code in your terminal:

sudo apt-get install unzip

The best location for installing Glassfish is the “/opt” directory, so we will unzip the contents of the archive there by the use of this command:

unzip glassfish-4.0.zip -d /opt

As a result you will find there a new folder of the name glassfish4.

Starting the newly installed server

Now its time for the first run. The newly installed server has the absolute path /opt/glassfish4/ and the administration binary can be found under /opt/glassfish4/bin. To start the server you just need to execute this command:

sh /opt/glassfish4/bin/asadmin start-domain

The startup process can take a few seconds and after that the server will be responding on the address http://example.com:8080/ for normal HTTP requests and the administration interface will be available at http://example.com:4848

Creating a startup script

Most likely you will want to have an automatic startup script, because you don’t want to manually start the server each time you make some kernel updates on Ubuntu. Therefore I will give you a startup script, which I have found on https://blogs.oracle.com/foo/entry/how_to_run_glassfish_v3, but we still needed to make some changes to run it with Glassfish 4. Here is the script:

#!/bin/sh
#
# glassfish init script for Linux
# Simplest possible case -- no password file, one default domain
# it would be simple to add such options

GLASSFISH_HOME=${GLASSFISH_HOME:-"/opt/glassfish4"}

case "$1" in
start)
$GLASSFISH_HOME/bin/asadmin start-domain >/dev/null
;;
stop)
$GLASSFISH_HOME/bin/asadmin stop-domain >/dev/null
;;
restart)
$GLASSFISH_HOME/bin/asadmin restart-domain >/dev/null
;;
\*)
echo "usage: $0 (start|stop|restart|help)"
esac

Copy and save the script as glassfish in the /etc/init.d directory. You can use the nano editor to create the file by typing:

nano glassfish

Give the script the correct rights and add it to the startup process:

chmod +x glassfish
update-rc.d glassfish defaults

That is all! Now you should have a running Glassfish 4 on Ubuntu.