Setting up GlassFish on Ubuntu

This document details how to setup GlassFish on Ubuntu as a non-root user - given Ubuntu does not have a dedicated root user account.  The goal of this text is to install GlassFish on ports 8081/8181 (http/https) with the administrator port on 4848.  In addition, instructions will be given how to configure the /etc/init.d directories so the default (domain1) server automatically starts and stops with the boot process.

As with the other documents in this series, I will assume little GlassFish and only moderate Ubuntu knowledge.


  • start synaptic package manager and download "ant - A Java based build tool like make".  Note: a copy of ant can be found in the GlassFish download, but it doesn't hurt to have the universe version installed.
  • The version of glassfish detailed here requires Java 5.0 (aka JDK 1.5).  Download and install JDK 1.5 then set JAVA_HOME to point to the base directory of 1.5.  Note: if you have installed the Sun Java WebServer then you already have a copy of JDK 1.5 installed.  Details on how to install the Sun Java Web Server on Ubuntu can be found here.
  • (optional) Start synaptic package manager and download "ksh - The real, AT&T version of the Korn Shell" from the Shell Universe.

To set your JAVA_HOME (in this case to the 1.5 JDK within the Sun Java WebServer) via the following commands:
  • cd ~
  • open .profile in your favorite text editor.  Yes, you can use a real one like vi.
  • add the following line at or near the end of the file:
    export JAVA_HOME=/home/kkranz/apps/webserver7/jdk
  • Source the profile: (. .profile) or simply open and use a new terminal window

Installing GlassFish Itself:

  • download your desired glassfish release.  (In my case it was glassfish-installer-v2-b50g.jar)
  • Copy glassfish-installer-v2-b50g.jar to the parent directory where you want to install the server.  (In my case it was ~/apps/glassfish)
  • Go to the parent directory and execute
    java -Xmx256m -jar glassfish-installer-v2-b50g.jar
  • (optional) rename the resulting "glassfish" directory to match your version of glassfish.  Example: glassfish_v2_b50.  For example the fully qualified path to my glassfish install is ~/apps/glassfish/glassfish_v2_b50.  I will refer to this as the [GlassFishHome].  You only need to do this if you too have a swiss cheese brain like me and will forget over time and/or possibly have more than one version of glassfish installed at a time.
  • (optional) change your http, https, and/or admin port numbers if they are already in use by other applications.  For example: oracle is already occupying port 8080 on my machine.  To change the default http port for glassfish, edit the setup.xml file and search for "instance.port".  Change the 8080 value to another port (eg: 8081).  Note: in the section where instance.port is defined, you will see other settings for admin port, admin password, default domain name, etc.  Modify them as necessary.
  • In your [GlassFishHome] execute the following command:
        ant -f setup.xml
  • (optional) Remove the glassfish-installer-v2-b50g.jar file
You may now refer to the standard glassfish documentation.  As a shortcut, to start (and stop) your server execute the following commands:
    cd [GlassFishHome]/bin
    ./asasmin start-domain domain1

If you point your browser at http://localhost:8081/ you should see the ubiquitous "your server is up and running" page (assuming you changed your http - otherwise it will be 8080).  http://localhost:4848 will bring up the administration page.

Setting up the /etc/init.d/GlassFish Files:

If you would like your glassfish server to start at boot time then you need to create the necessary /etc/init.d files - much like you did with the Sun Java Web Server.  The core of the init.d file is shown below.  

case "$1" in
    ${GLASSFISHHOME}/bin/asadmin start-domain domain1
    ${GLASSFISHHOME}/bin/asadmin stop-domain domain1
    ${GLASSFISHHOME}/bin/asadmin stop-domain domain1
    ${GLASSFISHHOME}/bin/asadmin start-domain domain1
    echo $"usage: $0 {start|stop|restart}"
    exit 1

Create the above file using:
    sudo gedit /etc/init.d/GlassFish
Then add execute permissions:
    sudo chmod a+x /etc/init.d/GlassFish
Finally create the necessary run level links to start the server in states 2-5 and to terminate it in states 0,1, and 6.  


If, by now, you are tired of creating the links in the various rcX.d directories and remembering to get the file permissions correct. The following script will do it for you.  In fact, the only thing it wont do is write the start/stop script for you. 



# Author: Ken Kranz - Sun Microsystems, Inc
# NOTE: this script makes use of Korn Shell commands.  You must download "The real,
# AT&T version of the Korn Shell" from the Shell universe.
# creates the proper links from the various rcX.d directories to the TARGET file
# located in the /etc/init.d directory.  It assumes a standard startup config,
# namely the TARGET is started in run states 2-5 and shutdown in states 0,1 and 6.
# For this script to work, the /etc/init.d/TARGET file must already exist.  Once found
# it creats the /etc/rcX.d links back to the TARGET and, for good measure, does a
#    chomd +x
# on the target.
# You can run this script multiple times.  I might complain that the links exist, but it
# continue on.  aka: if you aborted a previous run, you can rerun to complete the link process
# NOTE: if you decide to rerun this script with a different TARGET to NUMBER value, it will
# NOT remove the existing links.  So, be careful, you don't want to be creating duplicate
# start or stop scripts.

USAGE="$(basename $0) TARGET NUMBER"

if [[ $# != 2 ]]; then
    echo $USAGE
    exit 0

if [[ x$TARGET = x ]]; then
    echo missing TARGET
    echo $USAGE
    exit 1
if [[ x$NUMBER = x ]]; then
    echo missing NUMBER
    echo $USAGE
    exit 1

if [[ ! -e /etc/init.d/$TARGET ]]; then
    echo cannot find $TARGET
    exit 1

sudo chmod a+x /etc/init.d/$TARGET

sudo ln -v -s /etc/init.d/$TARGET /etc/rc0.d/K${NUMBER}$TARGET
sudo ln -v -s /etc/init.d/$TARGET /etc/rc1.d/K${NUMBER}$TARGET
sudo ln -v -s /etc/init.d/$TARGET /etc/rc2.d/S${NUMBER}$TARGET
sudo ln -v -s /etc/init.d/$TARGET /etc/rc3.d/S${NUMBER}$TARGET
sudo ln -v -s /etc/init.d/$TARGET /etc/rc4.d/S${NUMBER}$TARGET
sudo ln -v -s /etc/init.d/$TARGET /etc/rc5.d/S${NUMBER}$TARGET
sudo ln -v -s /etc/init.d/$TARGET /etc/rc6.d/K${NUMBER}$TARGET

No I hadn't - wish I had. At a minimum, I would have included the reference up front in my post.

Posted by Ken Kranz on August 14, 2007 at 02:37 AM EDT #

Why not use the bundled .deb ?

Posted by Robert Clark on November 25, 2007 at 09:58 PM EST #

For the ksh script above, does the NUMBER value matter? What importance does it have? I understand that re-running the script with different TARGET/NUMBER pairs will create duplicate start/stop entries. Just curious.

Posted by Gerald Lopez on May 26, 2008 at 07:23 AM EDT #

Update-rc.d command under ubuntu adds the links needed or remove og wanted

Posted by Egil on August 07, 2009 at 09:03 AM EDT #

maybe this tutorial is the best i have ever seen:

Posted by isa on November 04, 2010 at 10:23 AM EDT #

here i found a new tutorial:

it is exm´plaining how to install glassfish as a non-root user as well. and also iptables etc seems to be explained. i havent seen a more complete tutorial yet...

Posted by anonym on November 05, 2010 at 10:44 PM EDT #

Post a Comment:
  • HTML Syntax: NOT allowed



« April 2014