Announcement

Collapse
No announcement yet.

Unable to get JAR example to work

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to get JAR example to work

    I am unable to get the JAR example to work.

    My PC is running Mirth 3.2.1.7650 (x64) on Windows 7.
    java(c) version: 1.8.0_45

    What I have tried:
    - Compiling the example code (attached as .txt) with:
    Code:
    javac JarTest.java
    - Creating a JAR file from the class file. (JAR attached)
    Code:
    jar cvf JarTest.jar JarTest.java
    - Copy the resulting JAR into the custom-lib folder of my Mirth server.
    - Restarted Mirth server

    In a Mirth javascript SourceConnector I try to create an instance of JarTest.
    Code:
    var test = new Packages.org.azufre.test.JarTest();
    test.Repeat();
    return "";
    When I deploy the channe (attached)l I get the error:
    Code:
    TypeError: [JavaPackage org.azufre.test.JarTest] is not a function, it is object.
    After that I have tried to use importPackage to no avail.
    Funny thing is that after I have restarted Mirth the JAR file is locked, I cannot delete or move it from the custom-lib folder.
    Mirth is using it in some way.

    This is driving me completely mad. What am I doing wrong?
    Attached Files

  • #2
    You're not archiving the JAR correctly. You have the JarTest class, but you're placed it inside a "temp" directory and archived that. That won't work. You need to use the correct package structure (org.azufre.test).

    Start with this directory structure:
    • JarTest
      • src
        • org
          • azufre
            • test
              • JarTest.java
      • classes


    Then in the top JarTest directory, compile it into the classes folder:

    Code:
    javac -d classes src/org/azufre/test/*
    Now you'll have this:
    • JarTest
      • src
        • org
          • azufre
            • test
              • JarTest.java
      • classes
        • org
          • azufre
            • test
              • JarTest.class


    Now, archive the JAR using the same org/azufre/test package structure:

    Code:
    jar -cf JarTest.jar -C classes org/azufre/test/JarTest.class
    Now that you have your JAR, put it in custom-lib, or any other folder of your liking. In the Administrator go to the Settings -> Resources tab. Select the resource pointing to your folder (or create a new one if need be), and then hit Reload Resource. You should see your new JAR in the Loaded Libraries list at the bottom.

    Now, on your channel, go to the Summary tab and hit Set Libraries. Make sure the resource you want to use is checked for the context you want to use it in. If you're just using the default resource, it should already be checked by default.

    After redeploying your channel, it should now work fine. Note that since you're just doing a System.out.println, it won't show up in the server logs (though System.err will). It will show up in the actual MC shell though if you're not running it as a service and have access to the shell. You can also use log4j in your custom class to echo things out.
    Attached Files
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      FYI I updated that page on the wiki, so it should be more correct and hopefully clearer.
      Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

      Nicholas Rupley
      Work: 949-237-6069
      Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


      - How do I foo?
      - You just bar.

      Comment


      • #4
        Thank you, it works like a charm!
        After reading your explanation and the Wiki it is completely clear now.

        Comment


        • #5
          Originally posted by narupley View Post
          FYI I updated that page on the wiki, so it should be more correct and hopefully clearer.
          Dear all,

          I apologize for coming back on this very old subject, but also in my case I cannot see where I fail.

          I do believe to follow the right steps outlined in your very good resource linked above, but the bothersome error "[JavaPackage com.example.JarTest] is not function, it is object" is still there.

          The source code:

          package com.example;

          public class JarTest {

          public String JarTest() {
          return "Hello constructor";
          }

          public String returnSomething() {
          return "Hello, World!";
          }
          }


          Note: the presence of the constructor, neither its type, doesn't change the problem.

          The directory hierarchy:

          .
          ├── src
          │....├── com
          │...........└── example
          │...................└── JarTest.java


          I compiled the source code (from the upper directory . ):

          $ javac -d classes src/com/example/*

          I got this structure:


          ├── classes
          │.....└── com
          │............└── example
          │....................└── JarTest.class


          I built the jar file:
          jar -cf JarTest.jar -C classes com

          that appear at the level of the dot in the structure above; the structure of the jar is respectful of this hierarchy:

          $ jar tf JarTest.jar
          META-INF/
          META-INF/MANIFEST.MF
          com/
          com/example/
          com/example/JarTest.class


          I put the jar in the server at the <mirthhome>/custom-lib directory.

          I restarted the service (I'm using the version 2.1.1.5490 of Mirth.)

          I used the pakcage inside one destination:

          var obj = new Packages.com.example.JarTest;
          logger.info('[channel_name]: ' +obj.returnSomething());


          Deploying and starting the channel results in the infamous error message, so Mirth cannot locate the class I'm calling from the Javascript channel..

          I thank you in advance and if some other information is needed, please let me know it.

          Marco

          Comment


          • #6
            You're not instantiating the object correctly. Correct syntax requires parentheses.
            Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

            Nicholas Rupley
            Work: 949-237-6069
            Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


            - How do I foo?
            - You just bar.

            Comment


            • #7
              Originally posted by narupley View Post
              You're not instantiating the object correctly. Correct syntax requires parentheses.
              Already tried, but

              var obj = new Packages.com.example.JarTest();

              doesn't change anything...

              [2017-03-24 08:53:33,222] ERROR (org.mule.impl.DefaultComponentExceptionStrategy:9 5): Caught exception in Exception Strategy for: c2009c97-de42-4a6c-8e62-745d03e65399: java.lang.Exception: org.mozilla.javascript.EcmaError: TypeError: [JavaPackage com.example.JarTest] is not a function, it is object.

              Comment

              Working...
              X