Kaltura Client Library - Java

Get the Java client library from the Kaltura TestMe Console


This short guide features a usage example of the Kaltura JAVA client.
In this guide we will create a small class that uses the JAVA client to issu API calls for adding new entries and getting a list of entries currently on the Kaltura server.

Get The Example Code

Attached below, you can find an archive including :

  • The full example code (src/KalturaExample.java).
  • A compiled JAR file of the java client (lib\kaltura-java-client-3.2.jar).
  • Additional libraries required for the java client to work (also under the lib directory).

Note that the files are organized as an Eclipse project, that you can import into your IDE and run as is.

The Code Explained

First we need to import the client packages and other java packages that we are going to use in our example :

// import the kaltura java client packages
import com.kaltura.client.*;
import com.kaltura.client.enums.*;
import com.kaltura.client.services.*;
import com.kaltura.client.types.*;
import com.kaltura.client.utils.*;

// import other packages that are used in this example
import java.io.File;

Next thing will be to declare some constants that will hold our partner information. These will allow us to relate our API calls to our partner account on the Kaltura server. Make sure to replace the needed information in the code you have downloaded before trying to execute it.

// define partner data
private static final  String USER_SECRET  ="PUT_USER_SECRET_HERE";   // replace with user secret string
private static final  String ADMIN_SECRET ="PUT_ADMIN_SECRET_HERE";  // replace with admin secret string
private static final  int    PARTNER_ID   = 12345;                   // replace with real partner ID

The first thing we have to do before we can send API calls to the Kaltura server is to create a 'KalturaClient' object that will be used to talk with the server. This involves the following steps :

  • Create a 'KalturaConfiguration' object that is setup with our partner ID and the Kaltura server as an end point.
  • Create a new 'KalturaClient' object that uses the 'KalturaConfiguration' object.
  • Ask the Kaltura server for a Kaltura Session string (KS) that will be used to identify our session with the server.

Examine the code for the 'getKalturaClient' function that returns a 'KalturaClient' object configured with the required KS :

 * Create a new KalturaClient object with an initiated session.
 * @param partnerId Partner's ID number
 * @param secret    Secret string (either user's or admin's, depending on 'isAdmin')
 * @param isAdmin   Should client be an admin or a normal user ?
 * @return          A configured client that you can use to call API services
 * @throws KalturaApiException

private KalturaClient getKalturaClient(int partnerId, String secret, boolean isAdmin) throws KalturaApiException
        // set a new configuration object
        KalturaConfiguration config = new KalturaConfiguration();

        // get a new client object using our configuration
        KalturaClient client = new KalturaClient(config);

        // start a new session (admin/user) and recieve the ks (kaltura session) string
        String userId = "user's name";
        KalturaSessionType sessionType = (isAdmin ? KalturaSessionType.ADMIN : KalturaSessionType.USER);
        String ks = client.getSessionService().start(secret, userId, sessionType);

        // set the kaltura client to use the recieved ks as default for all future operations

        // return the configured client
        return client;

The 'addEntry' function shows an example of using the JAVA client to upload a local file to the Kaltura server, create a new entry for it and print the new entry ID string.
The function uses the 'baseEntry' service and executes 2 of its actions: upload, addFromUploadedFile.

 * Upload a media clip to the kaltura server's and create an entry for it.
 * @param String fileName   File to upload.
 * @param String entryName  Name for the new entry.
 * @throws KalturaApiException

private void addEntry(String fileName, String entryName) throws KalturaApiException
        // create a new USER-session client
        KalturaClient client = getKalturaClient(PARTNER_ID, USER_SECRET, false);                       
        // upload the new file and recieve the token that identifies it on the kaltura server
        File up = new File(fileName);
        String token = client.getBaseEntryService().upload(up);
        // create a new entry object with the required meta-data
        KalturaBaseEntry entry = new KalturaBaseEntry();
        entry.name = entryName;
        entry.type = KalturaEntryType.MEDIA_CLIP;
        // add the entry you created to the kaltura server, by attaching it with the uploaded file
        KalturaBaseEntry newEntry = client.getBaseEntryService().addFromUploadedFile(entry, token);
        // newEntry now contains the information of the new entry that was just created on the server
        System.out.println("New entry created successfuly with ID " + newEntry.id);

The 'listEntries' function shows an example of using the JAVA client to print a list of entries currently on the Kaltura server.
The function uses the 'media' service and execute its 'list' action.

 * Get a list of entries on the kaltura server
 * @param mediaType  type of entries
 * @param howMany    maximum numer of entries
 * @throws KalturaApiException

public void listEntries(KalturaMediaType mediaType, int howMany) throws KalturaApiException
        // create a new ADMIN-session client
        KalturaClient client = getKalturaClient(PARTNER_ID, ADMIN_SECRET, true);

        // create a new mediaService object for our client
        KalturaMediaService mediaService = client.getMediaService();

        // create a new filter to filter entries - not mandatory
        KalturaMediaEntryFilter filter = new KalturaMediaEntryFilter();
        filter.mediaTypeEqual = mediaType;

        // create a new pager to choose how many and which entries should be recieved
        // out of the filtered entries - not mandatory
        KalturaFilterPager pager = new KalturaFilterPager();
        pager.pageSize  = howMany;

        // execute the list action of the mediaService object to recieve the list of entries
        KalturaMediaListResponse listResponse = mediaService.list(filter, pager);

        // loop through all entries in the reponse list and print their id.
        System.out.println("Entries list :");
        for (KalturaMediaEntry entry : listResponse.objects) {
                System.out.println("id:" + entry.id);

The main function for our example code uses the functions we wrote to upload 2 new files to the kaltura server and than print a list of the entries we already have on the server.
To execute it, make sure you change all the constants that hold our partner data with your real partner data, and that you put 2 video files "example_file_1.flv" & "example_file_2.flv" in the example's root directory.

public static void main (String[] args)
        try {
                KalturaExample example = new KalturaExample();

                // add 2 new entries
                example.addEntry("example_file_1.flv", "example1");
                example.addEntry("example_file_2.flv", "example2");

                // list entries currently on the server (the new entries will probably not show up
                // because they are still being converted
                example.listEntries(KalturaMediaType.VIDEO, 10);

        catch (KalturaApiException e) {

java_client_example.zip1.63 MB