2011年12月12日星期一

设置 Eclipse 以在 Java 程序中调用 CPLEX

前提当然是要成功安装CPLEX,我的系统为 Ubuntu 9.10 Karmic + CPLEXStudioAcademic123(安装在 /opt/ibm/ILOG/CPLEXStudioAcademic123/ 下)

我的做法是:


  1. /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/lib

    下找到 cplex.jar


  2. /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/bin/x86-64_sles10_4.1

    下找到 libcplex123.so

  3. 拷贝 cplex.jar 与 libcplex123.so 到 Eclipse 根目录 {EclipseHomeDir} 下的 needed/cplex 子目录中

首先在编译路径中加入CPLEX Jar 包,以便代码能够正确编译

(1). Project > Properties > Java Build Path > Libraries
(2). Add External JARs.
(3). Browse to the location of your CPLEX installation and select the file named cplex.jar.

For me,即是加入外部 Jar 包 {EclipseHomeDir}/needed/cplex/cplex.jar

如此程序即可正确编译,但还不能正常运行。

IBM ILOG CPLEX 官网上的 Tutorial 提及这一问题,并给出解决方案如下:

http://publib.boulder.ibm.com/infocenter/cosinfoc/v12r3/index.jsp

CPLEX > Getting Started with CPLEX > Setting up CPLEX > Setting up Eclipse for the Java API of CPLEX

Before you can run your compiled code, you must create an appropriate run configuration in Eclipse or modify an existing run configuration appropriately. To do so, follow these steps.
1. ...
2. ...
3. Go to the Arguments tab, select VM arguments, and add the path to the CPLEX library. The CPLEX library is conventionally a DLL file on Windows or a .so file on UNIX or GNU/Linux, with an indication of the version number in its name, like cplexXX.dll or cplexXX.so where XX represents a version number of the product.

Tip:
Add the path to the directory or folder containing the CPLEX library (not the name of the file itself).

For example, on UNIX or GNU/Linux , if the file libcplexXX.so is located in the directory COSinstallation/cplex/bin/myPlatform/myLibFormat then add a path similar to this example:
-Djava.library.path= COSinstallation/cplex/myPlatform/myLibFormat

Similarly, on Windows, if the file cplexXX.dll is located in the folder COSinstallation\cplex\myPlatform\myLibFormat then add a path similar to this example:
-Djava.library.path=COSinstallation/cplex/myPlatform/myLibFormat

4. ...

After these steps, you can run your Java application using CPLEX as you run other applications from Eclipse.

按照这一说明,对于我的case,

Run > Run Configurations ... > Java Application > Arguments > VM arguments 中的参数应为

-Djava.library.path= /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/bin/x86-64_sles10_4.1/

-Djava.library.path= {Eclipse_Home_Dir}/needed/cplex/

遗憾的是,这样的设置并不能成功运行,而是会导致类似如下的异常:

Exception in thread "main" java.lang.NoClassDefFoundError: /opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/bin/x86-64_sles10_4/1/
Caused by: java.lang.ClassNotFoundException: .opt.ibm.ILOG.CPLEX_Studio_Academic123.cplex.bin.x86-64_sles10_4.1.

我的解决方法是:

{Eclipse 菜单} Window > Preferences > Run/Debug > String Substitution > New...

建立一个新的变量
variable: cplex_library
value: {EclipseHomeDir}/needed/cplex/

(注-1:{EclipseHomeDir} 应该替换为你自己实际的Eclipse 根目录。)
(注-2:这也就是为什么在开头我们把 cplex.jar 与 libcplex123.so 拷贝到这一目录中的原因)

回到 Run > Run Configurations ... > Java Application > Arguments > VM arguments

-Djava.library.path=${cplex_library}

Apply > run, 可以看到

IBM ILOG License Manager: "IBM ILOG Optimization Suite for Academic Initiative" is accessing CPLEX 12 with option(s): "e m b q ".
Parallel mode: deterministic, using up to 2 threads for concurrent optimization.

说明成功!

3 条评论:

  1. 不好意思 想請教一下
    我的是WINDOWS系統
    但我在ECLIPSE的資料夾裡找不到needed這個資料夾
    請問我的value應該要填啥呢?

    回复删除
    回复
    1. 你可能忽略了这句话:拷贝 cplex.jar 与 libcplex123.so 到 Eclipse 根目录 {EclipseHomeDir} 下的 needed/cplex 子目录中。

      needed 是一个我自己建立的子目录,专门放各种我需要的类库。你可以自己建立一个,然后照文里写的作下去就是了,唯一的区别是windows的路径是盘符加目录,而linux只有目录,你相应修改一下就对了。

      祝好运。

      删除
    2. 感謝!! 成功了!!

      删除