Reputation: 71
I have an optimization problem modelled and written in IBM ILOG CPLEX Optimization Studio. I want to call .mod and .dat from Java. I found some example to do it. However, I got some error.
My code is shown below. I also added all cplex and opl library
package cplexJava;
import ilog.concert.*;
import ilog.cplex.*;
import ilog.opl.*;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
model();
}
public static void model() {
int status = 127;
IloOplFactory.setDebugMode(true);
IloOplFactory oplF = new IloOplFactory();
IloOplErrorHandler errHandler = oplF.createOplErrorHandler();
IloOplModelSource modelSource = oplF.createOplModelSource("D:/Cplex project/Example_2/Example_2.mod");
IloOplSettings settings = oplF.createOplSettings(errHandler);
IloOplModelDefinition def = oplF.createOplModelDefinition(modelSource,settings);
IloCplex cplex = oplF.createCplex();
cplex.setOut(null);
IloOplModel opl = oplF.createOplModel(def, cplex);
IloOplDataSource dataSource = oplF.createOplDataSource("D:/Cplex project/Example_2/Example_2.dat");
opl.addDataSource(dataSource);
opl.generate();
if (cplex.solve())
{
System.out.println("OBJECTIVE: " + opl.getCplex().getObjValue());
opl.postProcess();
opl.printSolution(System.out);
}
else
{
System.out.println("No solution!");
}
oplF.end();
status = 0;
System.exit(status);
}
}
In my code, the errors came from from oplF.createCplex()
and cplex.solve()
. When I tried to run it, this is the error I got.
I could not figure out why I got the errors from oplF.createCplex()
and cplex.solve()
although I already added the cplex
and opl
library
Upvotes: 0
Views: 1410
Reputation: 1176
Actually your IDE tells you what the problem is: There are possible IloExceptions thrown and you do not handle them. You need to either surround your code with a try catch block, or your main-method should have a "throws IloException" in the signature:
package cplexJava;
import ilog.concert.*;
import ilog.cplex.*;
import ilog.opl.*;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
model();
}
public static void model() {
int status = 127;
try {
IloOplFactory.setDebugMode(true);
IloOplFactory oplF = new IloOplFactory();
IloOplErrorHandler errHandler = oplF.createOplErrorHandler();
IloOplModelSource modelSource = oplF.createOplModelSource("D:/Cplex project/Example_2/Example_2.mod");
IloOplSettings settings = oplF.createOplSettings(errHandler);
IloOplModelDefinition def = oplF.createOplModelDefinition(modelSource,settings);
IloCplex cplex = oplF.createCplex();
cplex.setOut(null);
IloOplModel opl = oplF.createOplModel(def, cplex);
IloOplDataSource dataSource = oplF.createOplDataSource("D:/Cplex project/Example_2/Example_2.dat");
opl.addDataSource(dataSource);
opl.generate();
if (cplex.solve())
{
System.out.println("OBJECTIVE: " + opl.getCplex().getObjValue());
opl.postProcess();
opl.printSolution(System.out);
}
else
{
System.out.println("No solution!");
}
oplF.end();
} catch (IloException ilx) {
// log error message or something like that
}
status = 0;
System.exit(status);
}
}
And please use class names with upper case first letter and package names with all lower case.
Upvotes: 1
Reputation: 1
I also faced the the same problem.
After some trial and error I realized that the correct name is DYLD_LIBRARY_PATH
for macos.
Upvotes: 0
Reputation: 4465
For the OPL Java API, you should only need oplall.jar.
SETUP
On my x86-64 Linux machine with Eclipse 3.6, this is done, like so (hopefully it's similar for you):
COS_INSTALL_DIR/opl/lib/oplall.jar
(where COS_INSTALL_DIR
is the location where you installed CPLEX Optimization Studio)One more thing to do is make sure that your LD_LIBRARY_PATH
environment variable is set to COS_INSTALL_DIR/opl/bin/x86-64_linux
. (NOTE: On Windows, I think you should set the PATH
environment variable instead.) You can set this in Eclipse, like so:
LD_LIBRARY_PATH
in the Name field (try PATH
on Windows)COS_INSTALL_DIR/opl/bin/x86-64_linux
in the Value field (again, where COS_INSTALL_DIR
is the location where you installed CPLEX Optimization Studio)FIX COMPILER ERRORS
Once you have that set up, you'll probably notice that you are still getting compiler errors (the little red squiggle lines indicate this). Hover your mouse over the those and you'll be presented with a list of quick fixes: 1) add throws declaration; 2) Surround with try/catch. Pick one of those to fix the issue. After all of the red squiggly lines are gone you should be able to run your program.
If you're not familiar with fixing compiler errors in Eclipse, maybe this Eclipse tutorial with help. Sometimes you have to select "Project > Clean" to force a recompile.
Upvotes: 0