How to Setup GlassFish 4.1 with JAVA 8 in Ubuntu 15.04

Channel: Linux
Abstract: we'll need to install Oracle JDK 8. As Oracle Java is not available in the repository of Ubuntuafter installing Oracle Java 8 and GlassFish 4.1 in our

Hi everyone, today we'll learn how to install and configure Glassfish with Oracle Java in Ubuntu 15.04 Linux Distribution. GlassFish is an open-source application server for the development and deployment of Java Platform, Enterprise Edition (Java EE platform) applications and web technologies based on Java technology. It supports different Java-based technologies like Enterprise JavaBeans, JPA, JavaServer Faces, JMS, RMI, JavaServer Pages, servlets and more. Glassfish provides a lightweight and extensible core based on OSGi Alliance standards with a web container. For the configuration and management, it has a very good easy-to-use administration console with update tool for updates and add-on components. Glassfish has good support for high availability clustering and load balancing.

Now, we'll go for installing Glassfish in Ubuntu 15.04 with pretty easy steps.

1. Adding Java PPA

First of all, we'll need to install Oracle JDK 8. As Oracle Java is not available in the repository of Ubuntu, we'll need to add a PPA for the access of the Oracle java 8 installer. So, we'll first install python-software-properties if not installed and add PPA into our Ubuntu 15.04 machine.

How to Fix 「Java Runtime Environm...

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

How to Fix 「Java Runtime Environment not found」 error
# apt-get install python-software-properties

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-apt python-minimal
python-pycurl python2.7 python2.7-minimal
Suggested packages:
python-doc python-tk python-apt-dbg python-gtk2 python-vte python-apt-doc
libcurl4-gnutls-dev python-pycurl-dbg python-pycurl-doc python2.7-doc binutils
binfmt-support
The following NEW packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-apt python-minimal
python-pycurl python-software-properties python2.7 python2.7-minimal
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,126 kB of archives.
After this operation, 17.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Now, we'll add the ppa for Java using add-apt-repository command as shown below.

# add-apt-repository ppa:webupd8team/java

Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK7 / JDK8 / JDK9). There are no actual Java files in this PPA.

More info (and Ubuntu installation instructions):
- for Oracle Java 7: http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html
- for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html

Debian installation instructions:
- Oracle Java 7: http://www.webupd8.org/2012/06/how-to-install-oracle-java-7-in-debian.html
- Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html

Important!!! For now, you should continue to use Java 8 because Oracle Java 9 is available as an early access release (it should be released in 2016)! You should only use Oracle Java 9 if you explicitly need it, because it may contain bugs and it might not include the latest security patches! Also, some Java options were removed in JDK9, so you may encounter issues with various Java apps. More information and installation instructions (Ubuntu / Linux Mint / Debian): http://www.webupd8.org/2015/02/install-oracle-java-9-in-ubuntu-linux.html
More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmpahw0r1nh/secring.gpg' created
gpg: keyring `/tmp/tmpahw0r1nh/pubring.gpg' created
gpg: requesting key EEA14886 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpahw0r1nh/trustdb.gpg: trustdb created
gpg: key EEA14886: public key "Launchpad VLC" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
OK

After adding the PPA repository, we'll want to update the local package repository index. To do so, we'll need to run the following command.

# apt-get update
2. Installing Oracle JDK 8

After updating the repository index, we'll want to install Oracle JDK 8 by running the following command.

# apt-get install oracle-java8-installer
3.  Setting "JAVA_HOME" Variable

Now, after installing the Oracle JDK 8, we'll now want to set the environment variable "JAVA_HOME" as the path of the newly installed Oracle JDK 8. To set the variable, we'll need to edit /etc/environment file using our favorite text editor.

# nano /etc/environment

After opening with the text editor, we'll need to add the following line into the bottom of the file.

JAVA_HOME="/usr/lib/jvm/java-8-oracle"

Once, the line is added, we'll need to reload file.

# source /etc/environment

After installing and setting the Oracle JDK 8, we'll run the following command to check and confirm.

# java -version
java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

If we see the output as shown above, it is confirmed that we have Java 8 installed in our machine.

4. Installing GlassFish 4.1

After our Java is installed correctly, we'll now march towards installing Glassfish 4.1 which is the latest version till date. We can even download the older versions from the GlassFish official download.

# cd /tmp
# wget 'http://download.java.net/glassfish/4.1/release/glassfish-4.1.zip'

Now, we'll want to extract the downloaded zip package of the latest GlassFish 4.1 . To do that, we'll need to install unzip and then extract the package into /opt directory.

# apt-get install unzip # unzip glassfish-4.1.zip -d /opt
5. Setting GlassFish PATH

Now, we'll want to setup PATH variable for GlassFish so that the GlassFish executable files will be accessible directly from any directory. To do that, we'll edit ~/.profile file and add the PATH to the directory where GlassFish is extracted.

# nano ~/.profile

Then add the following lines into it.

export PATH=/opt/glassfish4/bin:$PATH

# source ~/.profile
6. Starting GlassFish server

Finally, after installing Oracle Java 8 and GlassFish 4.1 in our Ubuntu 15.04 machine. We'll want to start the GlassFish server. To do so, we'll run asadmin as follows.

# asadmin start-domain
Waiting for domain1 to start ............ Successfully started the domain : domain1 domain Location: /opt/glassfish4/glassfish/domains/domain1 Log File: /opt/glassfish4/glassfish/domains/domain1/logs/server.log Admin Port: 4848 Command start-domain executed successfully.

A domain is a set of one or more GlassFish Server instances managed by one administration server. The default GlassFish Server’s port is 8080 and administration server’s port is 4848 with the administration user name as admin with no password. We can visit http://ip-address:8080/ to check the homepage of GlassFish Server and http://ip-address:4848/ to get the admin login page in our web browser.

7. Enabling Secure Admin

Now, inorder to access the administration panel remotely via webpage, we'll need to enable secure admin using asadmin by running the following command.

# asadmin enable-secure-admin

Enter admin user name> admin
Enter admin password for user "admin">
You must restart all running servers for the change in secure admin to take effect.
Command enable-secure-admin executed successfully.

This will ask us the username and password we want to set.

Note: If you get this error "remote failure: At least one admin user has an empty password, which secure admin does not permit. Use the change-admin-password command or the admin console to create non-empty passwords for admin accounts." you'll need to run as admin change-admin-password and enter a new password for the admin then retry above command.

# asadmin change-admin-password

Enter admin user name [default: admin]>admin
Enter the admin password>
Enter the new admin password>
Enter the new admin password again>
Authentication failed for user: admin (Usually, this means invalid user name and/or password)
Command change-admin-password failed.

After setting, we'll need to restart the domain.

# asadmin restart-domain

Successfully restarted the domain Command restart-domain executed successfully.

After enabling the secure admin, we are able to access the administration panel by pointing our web browser to http://ip-address:4848 . Then, access the admin panel by entering the credentials entered above.

8. Deploying WAR on GlassFish

Now, after we have successfully installed GlassFish and running the server, we'll want to deploy a WAR application into the GlasFish. Here, in this tutorial we'll deploy hello.war for the test of the server. So, first we'll download hello.war from the official sample page of GlassFish using wget command.

# wget https://glassfish.java.net/downloads/quickstart/hello.war

After downloading the war file, we'll now deploy the war file using asadmin command.

# asadmin deploy hello.war

Enter admin user name> admin
Enter admin password for user "admin">
Application deployed with name hello.
Command deploy executed successfully.

This will ask us to enter the username and password for the application deployment.

As the war application has been deployed, we can check it by visiting http://ip-address:8080/hello using our web browser.

9. Undeploying and Stopping Server

Now, if we have done our task with the GlassFish Server and the deployed application, we can simply undeploy the application and stop the GlassFish server.

To undeploy a running application, we can simply run asadmin undeploy with the application name we want to undeploy.

# asadmin undeploy hello

Enter admin user name> admin
Enter admin password for user "admin">
Command undeploy executed successfully.

To stop the running GlassFish domain, we can simply run asadmin stop-domain .

# asadmin stop-domain

Waiting for the domain to stop .
Command stop-domain executed successfully.
Creating a password file

If you are tired of entering the username and password everytime you deploy or undeploy an application, you can simply create a file named pwdfile with a text editor and add the following lines into it.

# nano pwdfile

AS_ADMIN_PASSWORD=your_admin_password

Now, after that file is created, we can just add --passwordfile flag pointing the pwdfile and then deploy the war application as shown below.

# asadmin --passwordfile pwdfile deploy hello.war
Application deployed with name hello. Command deploy executed successfully.

Now, the prompt for username and password won't appear further.

Conclusion

GlassFish is an awesome open-source application server that implements Java EE. We can install GlassFish with different methods like ZIP Package, Self-Extracting Bundle and Full Platform or Web Profile Distribution. Here, in this tutorial we've used full platform with zip package. The latest GlassFish version 4.1 includes new support for Java API for JSON Processing (JSON-P) 1.0, Java API for WebSocket 1.1, Batch Applications for the Java Platform 1.0, Concurrency Utilities for Java EE 1.0, Java Message Service (JMS) 2.0, Java API for RESTful Web Services (JAX-RS) 2.0 and many updated JAVA EE Standards. GlassFish has made the deployment of war java application very fast, secure and easy. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you! Enjoy :-)

Ref From: linoxide
Channels:

Related articles