0

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?

  • Hi Hemanth, it is possible to be used from Web application running on any application server including Tomcat. What you need to make sure is check if your working directory is pointing to the place where your DLLs are located or use full path names. Is there any particular error you get? – Przemysław Ładyński Feb 06 '18 at 09:31
  • java.lang.NoClassDefFoundError: Could not initialize class com.javonet.internal.JniConnection But the JniConnection class is well defined in the jar file – Hemanth Krishna Feb 06 '18 at 10:51
  • Ok it looks like your classpath is incorrect and javonet JAR not resolvable properly. How did you embed Javonet in your web project? did you merge the jars or you deploy it separately and set in class path for tomcat i.e. like here: https://stackoverflow.com/questions/6163447/including-external-jar-in-tomcat-classpath? – Przemysław Ładyński Feb 06 '18 at 11:46
  • It works fine when I run it as a console application but it shows this error only when i run it with tomcat server – Hemanth Krishna Feb 06 '18 at 12:28

1 Answers1

0

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.

  • @hemanth-krishna please check this answer and also the sample source code submitted to GitHub that I just added here. In the GitHub version it is simplified project which consists just of Web App. – Przemysław Ładyński Feb 06 '18 at 13:52
  • It works!!! Thank you so much. I was working it in Intellij IDEA but when I moved it to Eclipse it worked perfectly. I guess Intellij IDEA has some problems. – Hemanth Krishna Feb 08 '18 at 05:40