Creating a Facebook App with Java Part I
We have all heard of “mobile apps” that are sweeping the internet stories of developers creating hit apps in their parents basements, or while having coffees at Starbucks. They strike it rich, quit their jobs, and retire wealthy before the age of 25; but first, lets set some reasonable expectations. Those stories do happen, and they make the news because they are fun to hear about, but you might want to consider playing the lottery for some better odds if you really think you’re next.
I strongly believe that mobile apps (and/or HTML5 webpage apps) are the future of software development. Beyond that, social media is going to keep playing a greater and greater role in these applications. This article will take you through setting up a new Facebook app, from beginning to end. It is assumed that you are a developer, and as such are working on some UNIX based platform (all examples in this article will be from Ubuntu 11.10).
Those working on Windows (like I may have been just last year) will have to find equivalent commands for certain parts. It is worth noting here that I am not a JBoss employee, nor am I affiliated with them in any way. However I am friends with several of their lead developers, which probably helped my initial gravitation in that direction. You will see me reference JBoss projects many times throughout this tutorial, and while you are always free to use your own alternatives, you may need to deviate from my instructions to do so.
Overview
You may have listen terms like gamification, social networking, which all point to social applications giving developers more power to reach and captivate a wider audience than ever before; these social applications have the capability of growing exponentially faster than on any other medium. So the next logical question, “why use Facebook?” Google+ is up and coming, there’s Twitter, LinkedIn, and the list goes on. “What makes Facebook so special?” The answer is quite simply the fact that… EVERYONE… is on Facebook – that’s where people have the most “friends” and connections. Until this changes, it’s where you want to focus integrating your social connection points on your app. Other social media APIs may also be useful to integrate with, in addition, and if all goes well with these first few tutorial posts, I’ll provide more in the future showing you how to use Twitter as a great way to add to your social media influence, even send direct SMS messages for free to your users. For right now, we will focus on what we need to build our first Facebook application.
- Set up our Development Environment, including installation of Java, Eclipse, and a Jboss AS7 server for local development.
- Sign up for Facebook app development, and setup our first app.
- Use JBoss Forge to quickly create projects in Eclipse as well as install plugins for other frameworks like cloud hosting and REST web services (some of which we will explore in this article).
- Sign up for free app hosting on Red Hat’s OpenShift cloud.
- Create a REST Web Service to process our application logic.
- Create a complete Facebook application (Game).
- Install and set up integration tests using Arquillian.
- Setup a MySQL DB on the OpenShift platform to help store your data more permanently.
- Use the Twitter API to allow you to send direct SMS messages to users for free.
The end result of this tutorial will be a game where the names of 5 of your Facebook friends will appear at the top of the page, and 3 of their profile images will appear with text boxes below. Users will be able to copy and paste names from the top into the text boxes and submit the answers. Users will start with 100 points and get 10 points for each correct Name-to-ProfilePic match, and lose 10 points for each incorrect guess. How well do you know your Facebook friend’s profile pics? The answer may surprise you.
Install Java (JDK 6+)
We need to make sure we have a Java 6 (or higher) Java Development Kit (JDK) installed. If you don’t have a JDK, and you’re on Windows, you can download it and follow the instructions straight from Sun/Oracle.
Alternatively, if you are on Ubuntu you may use an open-source bash script to perform a streamlined installation; the script even allows you to select the version of Java you wish to install. Installing Java is relatively simple, and should take no more than a few minutes; Ubuntu/Apt linux users can also simply type the following into a terminal, like this.
[highlight highlightColor=”highlight-lightblue” ]sudo apt-get install openjdk-6-jdk[/highlight]
Install Eclipse and JBoss Tools
Go to the Eclipse for EE developers download page, choose the version appropriate for your operating system, and extract the distribution file into a folder on your hard disk. Then, go to the folder where you extracted the distribution and open Eclipse by double clicking on the executable file (usually just called eclipse) – you do not need to perform an installation. Once Eclipse is opened, we need to download JBoss Tools. Click and drag this install button into your Eclipse window – or open a web browser to this URL and follow the instructions.
Install JBoss AS 7
While you are free to use whatever server you’d like to locally host your application, JBoss AS7 is currently leaps and bounds ahead of any competitor. Start-up and publish times clock in at UNDER 10 seconds!! Sometimes as fast as 2 seconds. For those that choose to work with Glassfish or (god help you) Websphere, I simply ask, “why submit yourself to all that extra time you’re going to bang your head, screaming at your computer to deploy your app when you could have been coding?” For those of you forced to use any of AS7 competitors by your employers, you have my condolences, and from 9-5 everyday my empathy.
AS7 allows you to setup the server in a “standalone” or “domain” mode. For more info on the differences of these two modes, check out the JBoss AS 7 Getting Started Guide; we will use the “standalone” mode to start our server for now.
Go to the path that you downloaded the tar.gz (or zip) file and extract it to an appropriate location (I’ve used ~/projectWork/).
Open a Terminal and cd into the bin/ directory and execute standalone.sh. Now we can verify that AS7 was fully installed by making sure we get to the welcome screen when going to: http://localhost:8080. If you do not see the AS7 welcome screen, check the server output to make sure there were no problems while starting up.
Install JBoss Forge
If you installed JBoss Tools that comes with Forge already, you can skip this section and access Forge by opening Eclipse and pressing “Ctrl + 4”. This will open a Forge console where you can execute commands directly in Eclipse itself. To ensure you have the most recent version of Forge installed, and/or to execute the lines from a system terminal (as seen later in this tutorial), you can follow the rest of this section, below.
Install Forge by downloading the latest distribution zip-file from here (You’ll probably want the latest release or snapshot).
Extract it to a directory of your choosing (here I use /home/fife/projectWork/forge, I will refer to this as my ${FORGE_HOME} directory from now on.
Set FORGE_HOME (and if not already there, JAVA_HOME). For Linux users, update the ~/.profile file.
Add ${FORGE_HOME}/bin and JAVA_HOME to our PATH environment variable.
In a terminal, edit your environment (usually ~/.profile), then add the following to the end of the file:
export FORGE_HOME=~/path_to_forge_folder/
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export PATH=$FORGE_HOME/bin:$JAVA_HOME/bin:$PATH
Now refresh your environment by re-executing your profile or opening a new terminal (in Linux), and in Windows, you may need to log out and start a new Windows session. Run forge by typing $ forge for the first time, and we are ready to begin developing our Facebook application!
Create your new Facebook app
If you are unfamiliar with how Facebook applications work, this Facebook developer article provides a good overview of how Facebook apps work and connect to both Facebook, and your systems. Once you’ve familiarized yourself with the basics, lets go ahead and sign up for a Facebook developer account.
- Start by going to the app signup page, but before you click the Create New App button, make sure that you are a “Verified Facebook User” (This means you have already set up a mobile phone, or credit card with Facebook). If you have not, you will get an error when trying to create a new app.
- If this is the first app you are creating with Facebook, you will need to grant permissions for your applications. Make sure that you click allow.
- Now give your new app a name, agree to the terms, fill out the Captcha, and click submit.