Decision Optimization

 View Only

 Use Cplex Java API without installing the whole Cplex suite

Jump to  Best Answer
Lorenzo Moreschini's profile image
Lorenzo Moreschini posted 01/15/25 09:56 AM

I would like to run CPLEX on my mac book pro (running Sequoia 15.2) without installing the whole CPLEX suite.

To do so, I unpacked the installer, located the files "cplex-22.1.1.jar" and "libcplex2211.dylib", and copied them inside a 'lib' folder in my project.

When I need to run Cplex using java, I add the 'lib' directory to the classpath and I set the property `-Djava.library.path=lib`. This avoids the usual 'UnsatisfiedLinkageError' at runtime, but neverthess I get:

java.lang.NullPointerException: Cannot invoke "ilog.cplex.CplexI.setOut(java.io.OutputStream)" because "this._cplexi" is null
        at ilog.cplex.IloCplex.setOut(IloCplex.java:13154)

(setOut() is the very first method I call on the cplex instance)

What files/resources I need to add in my 'lib' forlder to use Cplex without installing the whole suite?

 

Lorenzo Moreschini's profile image
Lorenzo Moreschini  Best Answer

The error had nothing to do with the dynamic library and the jar archive. I was erroneously closing the model before calling .setOut(), apologies.

Saif Ali Sabri's profile image
Saif Ali Sabri

AI was used to create this answer

To utilize the CPLEX Java API without installing the entire CPLEX suite, it's essential to ensure that all necessary components are correctly configured. Here's how you can set up your environment:

  1. Required Files:

    • JAR File: Ensure that cplex-22.1.1.jar is included in your project's lib directory.
    • Native Library: Place libcplex2211.dylib (for macOS) in the same lib directory.
  2. Classpath Configuration:

    • Add the JAR file to your project's classpath. In your Java command, this can be specified with the -cp or -classpath option:
      bash
      java -cp lib/cplex-22.1.1.jar:. YourMainClass
    • Alternatively, if you're using an Integrated Development Environment (IDE), configure the build path to include the JAR file.
  3. Native Library Path:

    • Set the java.library.path to point to the directory containing the native library:
      bash
      java -Djava.library.path=lib -cp lib/cplex-22.1.1.jar:. YourMainClass
    • This informs the Java Virtual Machine (JVM) where to find the native libraries required by the CPLEX API.
  4. Environment Variables:

    • Ensure that any environment variables required by CPLEX are properly set. This may include variables like CPLEX_HOME or others specified in the CPLEX documentation.
  5. Licensing:

    • Verify that your CPLEX license is accessible and correctly configured. Without a valid license, the API will not function properly.
  6. Error Handling:

    • The NullPointerException you're encountering suggests that the CPLEX instance (_cplexi) is not being initialized correctly. This could be due to missing dependencies or incorrect configuration. Double-check that all required files are present and paths are set correctly.
  7. Consult Documentation:

    • Refer to the official CPLEX Java API documentation for detailed information on setup and configuration. This resource will provide guidance tailored to your specific version and operating system.

By meticulously following these steps, you should be able to integrate the CPLEX Java API into your project without the need to install the full CPLEX suite. Ensure that all paths and environment variables are correctly set to facilitate seamless interaction between your Java application and the CPLEX libraries.