Skip to main content

Installing GlassFish and PostgreSQL on Ubuntu Server Edition

Posted by cayhorstmann on July 18, 2006 at 8:09 AM PDT

??? style="float:left; padding-right: 1em;" />I am getting ready for the fall
semester. My software engineering students will be using href="">GlassFish for their projects. In
order to avoid the "but it worked on my computer" syndrome, I make them
deploy their apps on a server in my office. I just installed href="">Ubuntu Server Edition. href="">These
instructions got me started with the Linux basics. This blog has the
(not so) gory details for installing Java, GlassFish, and PostgreSQL. At
the end are the obligatory reflective comments.

I ran all these steps as super-user. Note that Ubuntu Server has no GUI
interface by default. I simply put the in a corner, connected with ssh,
and ran the commands in a terminal. Required skills: bash shell basics and
a stone-age text editor such as vi.


  1. Edit /etc/apt/sources.list, uncomment the lines for adding
    universe, and manually add multiverse at the end:
    deb dapper universe multiverse
    deb-src dapper universe multiverse
  2. Runapt-get install sun-java5-jdk
  3. Runjava -versionYou should get something like
    java version "1.5.0_06"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
    Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

Wow. That was phenomenally easy.

??? style="float:right; padding-left: 1em;" />GlassFish

  1. Go to and look for the version that you want to download.
  2. Download it with wget
    substitute the correct version number.
  3. Runjava -jar -mx256M glassfish-installer-v2-b09.jar(Again,
    substitute the correct version number.) The installer automatically runs
    in headless mode, without a GUI.
  4. Move the installation somewhere permanent. I want to be able to get
    new versions as they become available without having to change many
    configuration files. I'll put this version into
    /opt/glassfish-v2-b09 and make a symlink
    mv glassfish /opt/glassfish-v2-b09
    ln -s /opt/glassfish-v2-b09 /opt/glassfish
  5. Run setup.xml in the usual way:
    cd /opt/glassfish
    /opt/glassfish/lib/ant/bin/ant -f setup.xml
  6. I wanted the default domain to be in a separate directory so that I
    could install new versions. There must be a better way of controlling
    the domain directory, but for now I used a brute-force approach, again
    with symlinks.
    mv /opt/glassfish/domains/domain1 /opt
    ln -s /opt/domain1 /opt/glassfish/domains/domain1
  7. It seems prudent to change the admin password on a public
    /opt/glassfish/bin/asadmin change-admin-password --user admin
    Please enter the old admin password>adminadmin
    Please enter the new admin password>(password is echoed)
    Please enter the new admin password again>(password is echoed)
    password needs to be at least 8 characters long.
    I then had to
    remove ~/.adminpass and run /opt/glassfish/bin/asadmin
    to regenerate it, but that may have been a fluke.
  8. I want GlassFish to be a service that starts up automatically. There
    are probably better ways to do this, but here is my pedestrian approach.
    I produced a file /etc/init.d/glassfish with this content:
    #! /bin/sh
    # Provides:          glassfish
    # Required-Start:    $local_fs $remote_fs
    # Required-Stop:     $local_fs $remote_fs
    # Default-Start:     2 3 4 5
    # Default-Stop:      S 0 1 6
    # Short-Description: glassfish initscript
    # Description:       A simple initscript for the glassfish app server
    # Author:            Cay S. Horstmann (

    set -e

    DESC="Java EE5 App Server"

    # Gracefully exit if the package has been removed.
    test -x $DAEMON || exit 0

    # Read config file if it is present.
    #if [ -r /etc/default/$NAME ]
    #       . /etc/default/$NAME

    #       Function that starts the daemon/service.
    d_start() {
            $ASADMIN start-domain \
                    || echo -n " already running"

    #       Function that stops the daemon/service.
    d_stop() {
            $ASADMIN stop-domain \
                    || echo -n " not running"

    case "$1" in
            echo -n "Starting $DESC: $NAME"
            echo "."
            echo -n "Stopping $DESC: $NAME"
            echo "."
            echo -n "Restarting $DESC: $NAME"
            sleep 10
            echo "."
            echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
            exit 3

    exit 0
    No, I don't know what I am doing. I just modified
    /etc/init.d/skeleton. Then I ran update-rc.d glassfish defaults
  9. At this point, you should be able to reboot the machine and point a
    browser to http://machinename:4848. The admin interface should
    come up.
    ??? style="padding-top: 1em;" />


  1. Runapt-get install postgresql-8.1 postgresql-client-8.1
  2. Change the password:
    su postgres
    ALTER USER postgres WITH PASSWORD 'secret';
  3. Go to href="">
    and pick the most recent driver. Download it as wget
    the version number...)
  4. Copy the driver into /opt/glassfish/domains/domain1/lib and
    restart the server. Use either asadmin in the usual way or, if
    you installed GlassFish as a service, simply invoke-rc.d glassfish
  5. Set up a connection pool and a JDBC resource. You can do this
    through the admin interface or directly from the command line. I did the
    latter. Each of the two commands should be on a single line.
    /opt/glassfish/bin/asadmin create-jdbc-connection-pool 
       --datasourceclassname org.postgresql.ds.PGSimpleDataSource
       --restype javax.sql.DataSource
       --property portNumber=5432:password=secret:user=postgres:serverName=localhost:databaseName=postgres
    /opt/glassfish/bin/asadmin create-jdbc-resource
       --connectionpoolid PostgresPool
  6. ??? style="float:right; padding-left: 1em;" />Try it out. Download the href="">Elvis quiz app.
    cd quiz
    jar xvf ../
    Edit and change the password. Edit
    conf/persistence.xml and specify the data source:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="" version="1.0">
        <persistence-unit name="defaultPersistenceUnit">
                <property name="toplink.logging.level" value="FINEST"/>
    Now run/opt/glassfish/lib/ant/bin/ant run-clientYou
    should get a message such as
         [java] elvis.entity.Quiz[id=26,title=A Java Trivia Quiz,questions={IndirectList: not instantiated}]

The Obligatory Reflective Comments

??? style="float:left; padding-right: 1em;" />First off, apt-get is great.
This was far and away the easiest JDK installation I have ever done. I
didn't have to fuss with the PATH. I'll get alerted when new versions
appear and can update them easily. There is some behind-the-scenes work
that makes Sun's Java the preferred one (rather than GCJ).

I'd love to be able to run apt-get install glassfish.

I never installed much software on a headless server before and I was
pleasantly surprised how easy it was. But if that isn't your cup of tea,
you can use the admin web interface to do most steps.

The Glassfish installer needs to do something about the service script.
Maybe someone can contribute this? It is an open source project, after

Related Topics >>