Emaneitron
Emaneitron

Reputation: 83

Compile Cplex in Java in linux

I'm trying to compile my file.java with an optimization problem with CPLEX notation in a cluster which uses linux (and I'm a Windows user) and a used (through and application to send commands)

javac file.java

and I get errors like :

file.java:4: error: package ilog.concert does not exist
import ilog.concert.IloException;
                   ^
file.java:5: error: package ilog.concert does not exist
import ilog.concert.IloLinearNumExpr;
                   ^
file.java:6: error: package ilog.concert does not exist
import ilog.concert.IloNumVar;
                   ^
file.java:7: error: package ilog.concert does not exist
import ilog.concert.IloNumVarType;
                   ^
file.java:8: error: package ilog.concert does not exist
import ilog.concert.IloRange;
                   ^
file.java:9: error: package ilog.cplex does not exist
import ilog.cplex.IloCplex;

So it doesn't recognize the library (and therefore the imports) which is supposedly in this ubication in the cluster

/home/apps/cplex/12.6.1/cplex/lib/cplex.jar

My question is, do I have to add something to the javac command line or is not connected the paths (like int Windows)?

Upvotes: 3

Views: 1346

Answers (2)

John Bollinger
John Bollinger

Reputation: 180093

Whether on Windows, Linux, OS X, or any other OS, the Java compiler needs to know where to look for the classes used by your program but not included among its sources. It uses the classpath for that purpose.

How the contents of the classpath are determined is a bit complex, and it can have implementation-specific details, but for your particular case you should probably just use the -cp option to javac to tell it where to find the needed classes:

javac -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar file.java

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

Use the -cp command line argument to add the jar file to your compile-time classpath. You'll need to specify the classpath when you run the code too.

$ javac -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar file.java
$ java -cp /home/apps/cplex/12.6.1/cplex/lib/cplex.jar:. file

Ideally, start using Java packages rather than the default package, and follow Java naming conventions.

Also, if you're not familiar with Java to start with, I would read some tutorials etc before you start trying to run anything complex like this.

Upvotes: 3

Related Questions