• About Us
  • Blog
  • Basket
  • Account
  • Sign In

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 Eclipse

This guide assumes that you have Eclipse, JDK and JRE 1.6 or later set up and configured on your system.

  1. Download the latest Java package from Sourceforge
  2. Unzip to your preferred project directory
  3. Add reference to the 'core' JAR file to your project by accessing the 'Java Build Path' section of the project's properties.
Adding external libraries in Eclipse

You should then click the 'Add external JARs' button and selec the 'Core' JAR.

Adding external libraries in Eclipse

You will then notice that 'Package Explorer' now contains a new item called 'Referenced libraries'.

Confirming external libraries being included
  1. 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 java.io.IOException;

import fiftyone.mobile.detection.Match;
import fiftyone.mobile.detection.Provider;
import fiftyone.mobile.detection.factories.StreamFactory;

public class Main {

  public static void main(String[] args) throws IOException {
    //Path to 51Degrees data file.
    String datFile = "path\\to\\51\\data\\file.dat";
    //Create a new provider to perform device matching.
    Provider provider;
    provider = new Provider(StreamFactory.create(datFile, false));
    //Now get device properties from the user agent.
    String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) "
			+ "Gecko/20100101 Firefox/40.0";
    Match match = provider.match(userAgent);
    //Implement some logic based on results.
    boolean isMobile = match.getValues("IsMobile").toBool();
    if (isMobile) {
      System.out.println("This device is mobile");
    } else {
      System.out.println("This device is not mobile");

When the code executes you will see that the user agent in this example is not mobile.

Eclipse console window result