Search code examples
tomcatwebjavonet

can javonet be used to call native dll's from a web application


I'm using javonet to load a c# or .NET dll into java and it works perfectly when ran as a console application but doesn't work with a web application.

Does it even work for web applications?


Solution

  • I have just created samples Tomcat v9.0 project for you so you can use it as a reference. You can find it here: https://github.com/Javonet/tutorials/tree/master/JavonetTomcatWebApp

    The sample consists of two Eclipse projects:

    • TomcatMiddleApp compiled to JAR (regular Java package which define one class that using Javonet generates random number using .NET Random class)
    • TomcatJavonetTest web project (regular web project which adds Javonet and TomcatMiddleApp as references and defines one *.jsp that displays the Random number)

    My setup in Eclipse looks like this: enter image description here

    As you see I just copy Javonet and TomcatMiddleApp to "WEB-INF/lib" folder.

    The content of my TestJavonet.java file is:

        package TomcatMiddleApp;
    
    
    import com.javonet.Javonet;
    import com.javonet.JavonetException;
    import com.javonet.JavonetFramework;
    import com.javonet.api.NObject;
    
    
    public class TestJavonet {
        public static final String TMP_DIR = System.getProperty("java.io.tmpdir");
        public String CallDotNetRandom()
        {
            try {
                System.out.println("Setting temporary directory for Javonet lic file to: "+TMP_DIR);
                //Notice I set here temp dir for Javonet lic file by default it will try to save in system32 and will fail
                Javonet.setLicenseDirectory(TMP_DIR);
                Javonet.activate("******", "******",JavonetFramework.v45);
                NObject objRandom = com.javonet.Javonet.New("System.Random");
                Integer value = objRandom.invoke("Next",10,20);
                return value.toString();
    
            } catch (JavonetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return e.getMessage();
            }
        }
    }
    

    And my main.jps file is:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    TomcatMiddleApp.TestJavonet middleApp = new TomcatMiddleApp.TestJavonet();
    %>
        <%= "Hello Javonet! Random number generated on .NET side:"+middleApp.CallDotNetRandom() %>
    </body>
    </html>
    

    Nothing special in other files. You do not need to use separate JAR to define your logic you can put the classes in Tomcat web project directly.

    And you can easily use .NET libraries in your Java Web project.