Introducing Java type provider

A while ago I blogged about some development I was working on with IKVM and type providers. At the time the type provider only worked with primitive types. Recently I have had time to take this a bit further and with the help of the F# team this type provider is now finally useful :).

iText Example

iText is a PDF generation library available from here.

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class HelloWorld {

    public static final String RESULT
        = "results/part1/chapter01/hello.pdf";

    public static void main(String[] args)
    	throws DocumentException, IOException {
    	new HelloWorld().createPdf(RESULT);

    public void createPdf(String filename)
	throws DocumentException, IOException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(filename));;
        document.add(new Paragraph("Hello World!"));

Once you have downloaded the iText JAR and IKVM (available from here), unzip them and place them into your project directory. Once you have done this you are ready to use the JavaTypeProvider.

module PDF =
    let[<Literal>] private jar = @"D:\Appdev\IKVM.TypeProvider\JavaSource\itext\itextpdf-5.4.4.jar"
    let[<Literal>] private ikvmPath = @"D:\Appdev\IKVM.TypeProvider\IKVM\bin\"

    type private Jar = Java.JavaProvider<JarFile=jar, IKVMPath=ikvmPath>
    type private iText =
    type private PDF =

    let createPDF (filename:string) (contents:string) =
        let document = new iText.Document();
        PDF.PdfWriter.getInstance(document, new |> ignore
        document.add(new iText.Paragraph(contents)) |> ignore

module Program =

    let main argv =
        let fname = "D:\mypdf.pdf"
        PDF.createPDF fname "Hello, from java"
        printfn "PDF written to %s" fname
        System.Console.ReadLine() |> ignore
        0 // return an integer exit code

For the code above you can see that we can open namespaces in the JAR by creating type aliases. Notice that the use of the type. Some types that the IKVM compiler generates are part of the IKVM.OpenJDK set of libraries. You will need to reference the appropriate libraries to use these generated types. In the case of iText you will need to and references to IKVM.OpenJDK.Core and IKVM.AWT.Swing.

The type provider is available on nuget, grab it by

  >Install-Package JavaTypeProvider

More samples and the type provider implementation can be found in the GitHub Repository this is still early days and I would be interested in seeing what this works with and what it doesn’t.

This entry was posted in Development and tagged , , . Bookmark the permalink.

4 Responses to Introducing Java type provider

  1. Pingback: F# Weekly #47, 2013 | Sergey Tihon's Blog

  2. Andrew Olney says:

    This is very impressive. Could you clarify the intended use relative to ikvm? It seems that the advantage is that you don’t have to explicitly call ikvmc, and ostensibly it makes it easier to keep your java jars up to date in the project. It also looks like the generated assembly is smaller because you are only exporting types that are actually used. The flip side is that each compile seems to take longer (I suppose because ikvmc is being called behind the scenes). Please let me know if I have any of this wrong or am missing something.

    • colinbul says:

      Hi Andrew,

      You are right on all accounts.
      As for usage, you are right in that it makes managing the jars easier, above that I’m not really sure about the end goal. I wanted to learn about generated type providers and this seems like a good little project.

      • Andrew Olney says:

        I’m going to keep trying it out and let you know how it goes.

        A strong advantage that I’ve already found is pedagogic — it’s easier for non-CS students learning to program to use the “magic” of your type provider than having to understand what IKVMC is doing.

        It may also be a way of solving class-loading problems on the fly, as this can sometimes be a problem when working with multiple IKVMC dlls.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s