using Quantlib from Java

A free/open-source library for quantitative finance

One of these days I’m going to give an overview of all the excellent open-source software I use on a daily basis. Until that day comes, I’ll observe that finance remains one of the big areas where open-source software has made relatively limited inroads.Java

Two production-quality packages fight that unhappy state: QuantLib – a comprehensive framework for quantitative finance – and QuickFix – a full-featured FIX engine. Both are C++ libraries and both provide very nice interfaces to facilitate integration with other languages, including Java. QuantLib is a big and complicated library and integrating it with Java is not totally obvious. Below, I’ll describe how to build and use QuantLib from Java.

These instructions are based on a unix installation. I’m not really a windows developer and don’t have all the shiny tools that windows developers use, so it’s not an area of focus for me. That said, I have managed to build QuantLib under windows by using MinGW+MSYS but it wasn’t terribly easy and I don’t currently have a working installation, so I won’t cover that here. If this is your aim, don’t be dismayed as it is possible and it had all the functionality I enjoy under linux.

Using QuantLib from Java (on linux)

  • Build QuantLib
    • Requires a working version of Boost. This may prove to be the hardest step of all and you’ll need to use the ample documentation provided by the Boost team.
    • Once you have a working copy of Boost, building QuantLib should require little more than
    • sh autogen.sh
      ./configure

      make
      sudo make install

  • Build QuantLib-SWIG
    • Requires a working copy of SWIG. Again, look to the SWIG instructions, but it should be easy.
    • Once SWIG is available, building the QuantLib/SWIG interfaces should only require:
    • sh autogen.sh
      ./configure \

      –with-jdk-include=${JAVA_HOME}/include \
      –with-jdk-system-include=${JAVA_HOME}/include/linux
      make -C Java
      sudo make install

  • Now you’ll have a Jar file with all of the SWIG/JNI stubs in it available in /usr/local/lib/QuantLib.jar. Add this to your classpath.
  • Programs which call QuantLib functionality will need to have the LD_LIBRARY_PATH set. This can be done by invoking the vm with something like:
  • -Djava.library.path=/usr/local/lib

  • Programs which call QuantLib functionality will also need to explicitly load the QuantLib libraries. This can be done with something like the following static block appearing before your main method:
  • static { // Load QuantLib
    try { System.loadLibrary("QuantLibJNI"); }
    catch (RuntimeException e) { e.printStackTrace(); }
    }

  • That’s it. Now test your configuration by running the examples in Quantlib-SWIG/Java/examples.

It’s worth understanding how Quantlib is being used from java. SWIG is creating a JNI interface into those methods within Quantlib which have been exposed through their declaration in the swig *.i files. These files are found in Quantlib-SWIG/SWIG and they determine what functionality from Quantlib will be available to you. You’ll likely need to get familiar with a subset of those files that you care about. If you find that some functionality you care about isn’t exposed in those files, you may need to expose it yourself.

There’s a learning curve, but it’s worth traversing so you can get at all the rich functionality so many smart people have put together.

  1. November 19th, 2008 at 16:24 | #1

    You can also use Java directly, without SWIG wrappers.

    JQuantLib is a port of QuantLib to Java.
    At the moment (nov/2008) we have 40% of classes translated and we are going to our second release, which will provide a reasonable subset of QuantLib.

    Thanks

    Richard Gomes
    http://www.jquantlib.org/index.php/User:RichardGomes

  2. Arnaud
    July 9th, 2010 at 11:16 | #2

    Hello,
    I’m using windows and actually struggling to create the .jar filer using Quantlib’s instruction (which unfortunately aren’t exactly self explanatory as I use apparently different version of software).
    Anyway, I was wondering if you could send me your jar file by email or point me to a website which may provide it?
    I tried to use JQuantlib, but couldn’t find any documentation of how to use pricing for American Options with discrete dividend so decided to try this SWIG…

  3. July 10th, 2010 at 19:43 | #3

    Hi Arnaud,

    Unfortunately, sending you the .jar file won’t be much help as you’ll need quantlib (and boost) built as well as the swig bindings, so I’m afraid you’ll have to actually go through the build process. If you’re using visual c++ under windows, this shouldn’t be tough. It’s possible (or, at least, it used to be) to build with mingw, but I haven’t done that in a long time as I’m only using quantlib under unix these days so couldn’t provide direct insights. The quantlib mailing lists are active and people will answer specific questions there. Good luck!

  4. Arnaud
    July 13th, 2010 at 02:22 | #4

    Hello and thank you very much for your answer.

    As you had probably realised I really am a newbie in Java programming, and I hadn’t realised that the .Jar file wasn’t enough.

    Thank you very much for your answer.

    Arnaud

  5. Billy Ng
    January 18th, 2011 at 12:18 | #5

    I am a MSc student planning for a project.
    This is my first financial project which involves adding in a Term Structure into an existing Package based on J2EE.

    Would that be a good approach for a first time project to use the QuantLib-SWIG wrapper approach?
    Many Thanks

    Billy Ng

  6. January 24th, 2011 at 09:15 | #6

    JQuantLib 0.2.4 is released.

    Notice that JQuantLib is a 100% Java port of QuantLib.
    There are no SWIG wrappers involved.

    More information at
    http://forums.jquantlib.org/viewtopic.php?f=16&t=13

    Thanks

  1. July 27th, 2010 at 20:46 | #1