前提当然是要成功安装CPLEX,我的系统为 Ubuntu 9.10 Karmic + CPLEXStudioAcademic123(安装在 /opt/ibm/ILOG/CPLEXStudioAcademic123/ 下)
我的做法是:
- 在
/opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/lib
下找到 cplex.jar
- 在
/opt/ibm/ILOG/CPLEX_Studio_Academic123/cplex/bin/x86-64_sles10_4.1
下找到 libcplex123.so
- 拷贝 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.
说明成功!