Structure of an application

Java applications using CPLEX observe object-oriented conventions.

To use the CPLEX Java interfaces, you need to import the appropriate packages into your application with these lines:

import ilog.concert.*;
import ilog.cplex.*;

As for every Java application, a CPLEX application is implemented as a method of a class. In this discussion, the method will be the static main method. The first task is to create an IloCplex object. It is used to create all the modeling objects needed to represent the model. For example, an integer variable with bounds 0 and 10 is created by calling cplex.intVar(0, 10) , where cplex is the IloCplex object.

Since Java error handling in CPLEX uses exceptions, you should include the Concert Technology part of an application in a try /catch statement. All the exceptions thrown by any Concert Technology method are derived from IloException. Thus IloException should be caught in the catch statement.

In summary, here is the structure of a Java application that calls CPLEX:


-classpath <path_to_cplex.jar>
 
-Djava.library.path=<path_to_shared_library> 
import ilog.concert.*;
import ilog.cplex.*;
  import ilog.concert.*;
  import ilog.cplex.*;
  static public class Application {
    static public main(String[] args) {
       try {
         IloCplex cplex = new IloCplex();
         // create model and solve it
       } catch (IloException e) {
          System.err.println("Concert exception caught: " + e);
       }
     }
   }