> Programming Considerations > Licensing an Application > Examples

Here are some code samples that illustrate the use of those runtime license routines and methods. The first example illustrates the routine CPXputenv when opening the CPLEX environment.

Notes
This example assumes a Microsoft Windows file directory structure that requires an additional backslash when specifying the path of the file containing the key. It also assumes that the application uses an environment variable called MYAPP_HOME to identify the directory in which it was installed.The string argument to CPXputenv must remain active throughout the time ILOG CPLEX is active; the best way to do this is to malloc the string.

CPXputenv Routine

char *inststr = NULL;
char *envstr  = NULL;

/* Initialize the CPLEX environment */

envstr = (char *) malloc (256);
if ( envstr == NULL ) {
   fprintf (stderr, "Memory allocation for CPXputenv failed.\n");
   status = FAIL;
   goto TERMINATE;
}
else {
   inststr = (char *)  getenv("MYAPP_HOME");
   if ( inststr == NULL ) {
      fprintf (stderr, "Unable to find installation directory.\n");
      status = FAIL;
      goto TERMINATE;
   }
   strcpy (envstr, "ILOG_LICENSE_FILE=");
   strcat (envstr, inststr);
   strcat (envstr, "\\license\\access.ilm");
   CPXputenv (envstr);
}

env = CPXopenCPLEX (&status);

The putenv Method for Java Users

Here is an example using Concert Technology for Java users:

IloCplex.putenv("ILOG_LICENSE_FILE=\\license\\access.ilm");
try {
   cplex = new IloCplex();
}
catch (IloException e) {
   System.err.println("Exception caught for runtime license:" + e);
}

CPXRegisterLicense Routine

The following is an example showing how to use the routine CPXRegisterLicense.

static char *ilm_license=\
 "LICENSE ILOG Incline\n\
  RUNTIME CPLEX      9.000 21-Apr-2004 R81GM34ECZTS N IRIX , options: m ";
static int ilm_license_signature=2756133;

   CPXENVptr     env = NULL;
   int           status;

   /* Initialize the CPLEX environment */

    status = CPXRegisterLicense (ilm_license, ilm_license_signature);
    if ( status != 0) {
       fprintf (stderr, "Could not register CPLEX license, status %d.\n",
                status);
       goto TERMINATE;
    }
    env = CPXopenCPLEX (&status);
    if ( env == NULL ) {
       char  errmsg[1024];
       fprintf (stderr, "Could not open CPLEX environment.\n");
       CPXgeterrorstring (env, status, errmsg);
       fprintf (stderr, "%s", errmsg);
       goto TERMINATE;
    }


The registerLicense Method for Java Users

Here is an example for Java users applying IloCplex.registerLicense:

static String ilm_CPLEX_license=
"LICENSE ILOG Test\n RUNTIME CPLEX      9.000 021-Apr-2004 R81GM34ECZTS N IRIX 
,
options: m ";
static int ilm_CPLEX_license_signature=2756133;

public static void main(String[] args) {

   try {
      IloCplex.registerLicense(ilm_CPLEX_license, ilm_CPLEX_license_signature);
      IloCplex cplex = new IloCplex();
   }
   catch (IloException e) {
      System.err.println("Exception caught for runtime license:" + e);
   }
}