Java V3.1 Documentation

You are viewing an old version of the documentation. For the latest, please go here: Java V3.2

Step by Step Guide for Netbeans

These instructions assume you have Netbeans, JDK and JRE 1.6 or later installed and configured for your system.

  1. Download the latest Java package from Sourceforge
  2. Unzip to your preferred project directory
  3. Add reference to the 51Degrees core JAR file to your project by right clicking the 'Libraries' and selecting 'Add JAR/Folder' option.
Netbeans add external JARs

You should see a file selection window. Navigate to the directory 'dist' directory and sselect the 'core' JAR file:

Netbeans choosing external libraries

You should see the 'core' package in the list of libraries.

Netbeans importing 51Degrees libraries
  1. 4.The code below demonstrates how to initialize the API and supply it with the device data file as well as how to perform the match and retrieve the results
import fiftyone.mobile.detection.Match;
import fiftyone.mobile.detection.Provider;
import fiftyone.mobile.detection.factories.StreamFactory;
import java.io.IOException;

public class GettingStartedNetbeans {

    public static void main(String[] args) throws IOException {
        // FiftyOne Provider object - used to access data and perform match.
        Provider provider;
        // Path to FiftyOne device data file.
        String datFile = "path\\to\\data_file.dat";
        // User agent string of the device in question.
        String userAgent = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) "
                + "AppleWebKit/534.11+ (KHTML, like Gecko) Version/ "
                + "Mobile Safari/534.11+";
        // Initialise provider with the data file to use.
        // 'false' flag indicates the data file is not temporary.
        provider = new Provider(StreamFactory.create(datFile, false));
        // Match object contains results of detection.
        Match match = provider.match(userAgent);
        // Fetch specific property, in this case 'IsMobile'.
        boolean isMobile = match.getValues("IsMobile").toBool();
        // Implement some logic based on detection results.
            System.out.println("This device is mobile.");
            System.out.println("This device is not mobile.");

Once the program executes you should see that the user agent in this example is a mobile device using the Blackberry browser.

Netbeans console window