Initial test
Be sure that you have a valid JDK installation (release 1.6 or later)
accessible. If necessary you can download
the latest release.
To make an initial test, open a command/terminal window and make
amplapi/examples/java
the current folder (directory). Then enter the
following commands to compile and then run a simple example:
Windows:
set PATH=%CD%/../../bin/;%PATH%
javac -cp ../../lib/ampl-java.jar FirstExample.java
java -cp ../../lib/ampl-java.jar;. FirstExample <solver>
Linux:
export LD_LIBRARY_PATH=$PWD/../../lib:$LD_LIBRARY_PATH
javac -cp ../../lib/ampl-java.jar FirstExample.java
java -cp ../../lib/ampl-java.jar:. FirstExample <solver>
macOS:
javac -cp ../../lib/ampl-java.jar FirstExample.java
java -Djava.library.path=../../lib -cp ../../lib/ampl-java.jar:. FirstExample <solver>
where optionally <solver>
is the name of a solver that has been installed with AMPL.
(If a solver is not specified, AMPL’s default choice will be used.) Alternatively,
compile and run FirstExample.java
from your preferred Java IDE with
ampl-java.jar
included in the class path. This will solve several small diet
problems and then display the optimal amounts of the foods from the last
solution.
Note that the folder containing the AMPL executable should be in the system search path.
Otherwise, the error message “AMPL could not be started” will be shown.
If the AMPL installation directory is not in the system search path,
you can add it passing a new Environment
to AMPL
as follows:
Environment env = new Environment("full path to the AMPL installation directory");
AMPL ampl = new AMPL(env);
Note that you may need to escape backslashes (e.g., “C:\\ampl\\ampl.mswin64”) if included in the path.
Development
Reference amplapi/lib/ampl-java.jar
in your project that uses the AMPL API. Together with
your existing AMPL implementation, this will provide the full object library
and access to all AMPL functions. The binary files that must be accessible are
platform dependent and listed below, using the main amplapi directory as root:
- Windows:
bin\javaswigwrapper.dll
bin\ampl-2.3.8.dll
- Linux
-
- macOS:
-
The amplapi
folder (directory) can be moved to a different location in your
computer’s filesystem, provided that the location of your AMPL executable has
been placed in the system search path.