Installing Software, Running GlassFish and JavaDB
Connect to your EC2 instance using the Java SSH client, Putty (in Windows) or by running the ssh command (in MacOSX or Linux) as it is shown in the Connect Screen.
Note: every time you reboot or restart your EC2 instance its IP address and DNS name may change, so you always need to check this screen and note the new address and name.
Download and Install Java and Glassfish
To get the latest JDK, run the following:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
Say yes whenever asked. If downloading fails, try again until the process finishes successfully. Then type:
sudo apt-get install oracle-java8-set-default
Download the latest j2ee + glassfish bundle. Oracle’s site asks us to accept terms and conditions before downloading anything. But we cannot have access to a browser in our EC2 instance. To bypass this, run the following command:
wget –no-check-certificate –no-cookies – –header “Cookie: oraclelicense=accept-securebackup-cookie” http://download.oracle.com/otn-pub/java/java_ee_sdk/7u3/java_ee_sdk-7u1.zip
Download the unzip application:
sudo apt-get install unzip
Unzip the file you downloaded above:
unzip java_ee_sdk-7u1.zip
Configure Glassfish
All configuration is done using the asadmin command (for clarity, I will be writing asadmin but you should be writing /home/ubuntu/glassfish4/bin/./asadmin the full path, where the asadmin executable is stored).
A default GlassFish domain, called domain1, exists in the downloaded and unzipped GlassFish folder, under /home/ubuntu/glassfish4/glassfish/domains/domain1.
To start domain1:
asadmin start-domain domain1
To stop domain1:
asadmin stop-domain domain1
In order to be able to login remotely to the web admin console (on port 4848), we first need to change GlassFish master password. Stop your domain and run:
asadmin change-master-password domain1
Glassfish will ask your current master password, which is, by default, “changeit”.
The new password must be: “sussexwebapps”
Finally, to activate remote connection through the admin console you need to change the domain’s admin password:
./asadmin change-admin-password
You will be asked the following:
Enter admin user name [default: admin]> admin
Enter the admin password> “the default password is the empty password”
Enter the new admin password> sussexwebapps
Enter the new admin password again> sussexwebapps
Then, start the domain (console will ask for master password) and run:
./asadmin enable-secure-admin
Console will ask for the following:
Enter admin user name> admin
Enter admin password for user “admin”> sussexwebapps
Deploying Applications
To deploy a Web Application, login to GlassFish Web Console. In your browser’s address bar type:
PublicDNS:4848/
PublicDNS is the name that Amazon assigned to your server. This may change when you reboot your EC2 instance. You can find it in the AWS EC2 Console in the details regarding your EC2 instance.
On GlassFish web admin console, select Applications and Deploy.
Select Browse and find a deployable package that contains your application. This is where you can upload your .war files!
Alternatively, you can deploy an application using the asadmin tool:
asadmin deploy war-name
asadmin undeploy war-name
asadmin list-applications
Configuring and Running JavaDB
Contrary to what happens when you use Netbeans, JavaDB will not start automatically when starting a GlassFish domain. Before starting the database, first type:
sudo vi /usr/lib/jvm/java-8-oracle/jre/lib/security/java.policy
Add the following line in the grant statement:
permission java.net.SocketPermission “localhost:1527”, “listen,resolve”;
Finally, run the following command to start JavaDB:
sh /home/ubuntu/glassfish4/javadb/bin/./startNetworkServer &
This will run JavaDB in the background (denoted by the & symbol). To stop the server run:
sh /home/ubuntu/glassfish4/javadb/bin/./stopNetworkServer
Creating a new JDBC Connection Pool
To avoid creating a Database manually, we will create a new JDBC Connection Pool, which, in turn, will create the Database in JavaDB. Note that, in Netbeans, everything was happening automagically, when we were creating a new DataSource (when configuring the persistence.xml file).
On GlassFish web admin console, under Resources->JDBC->JDBC Connection Pools click New.
Pool Name: pool_NameOfYourDatabase
Resource Type: javax.sql.DataSource
Database Driver Vendor: Derby
Click Next. Enable the Ping box. This will allow us to ping the database and check that everything is fine. In the Additional Properties section change the following:
Name: username
Password: password
DatabaseName: NameOfYourDatabase
ConnectionAttributes: create=true
The last property instructs Glassfish to create the database if it’s not there. Click Finish. If you click on the Ping button the test should succeed (given that JavaDB is up and running – see above).
Creating a new JDBC Resource
Now we will create a new JDBC Resource, which will use the newly created Pool. This resource will have a JNDI name, which is the one you have in your persistence.xml file. Under Resources->JDBC->JDBC Resources click on New.
JNDI name: jdbc/NameOfYourDatabase
Pool Name: select pool created above (from the drop-down menu)
Click OK.
Querying the Database
Through the SSH Connection to your EC2 Instance, you need to run the ij tool that comes with JavaDB to query your Database. See here for more details: http://db.apache.org/derby/papers/DerbyTut/ij_intro.html
For example type, under /home/ubuntu/glassfish4/javadb/bin/ij
ij>connect ‘jdbc:derby://localhost:1527/NameOfYourDatabase’;
ij>SHOW TABLES;
ij>SHOW SCHEMAS;
ij>SHOW SCHEMAS;
ij>select * from USERNAME.A_TABLE_IN_YOUR_DATABASE;