web stats
Unable to get JAR example to work - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2015, 06:38 AM
Barry van der Veen Barry van der Veen is offline
Mirth Newb
 
Join Date: Mar 2011
Posts: 7
Barry van der Veen is on a distinguished road
Default 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
File Type: txt JarTest.txt (225 Bytes, 9 views)
File Type: jar JarTest.jar (781 Bytes, 2 views)
File Type: xml JarTestChannel.xml (14.4 KB, 3 views)
__________________
Apprentice of StefanScholte @ Isala Klinieken
Reply With Quote
  #2  
Old 05-12-2015, 07:21 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,111
narupley is on a distinguished road
Default

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
File Type: jar JarTest.jar (803 Bytes, 6 views)
__________________
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.
Reply With Quote
  #3  
Old 05-12-2015, 11:01 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,111
narupley is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 05-12-2015, 11:15 PM
Barry van der Veen Barry van der Veen is offline
Mirth Newb
 
Join Date: Mar 2011
Posts: 7
Barry van der Veen is on a distinguished road
Default

Thank you, it works like a charm!
After reading your explanation and the Wiki it is completely clear now.
__________________
Apprentice of StefanScholte @ Isala Klinieken
Reply With Quote
  #5  
Old 03-23-2017, 08:47 AM
mxaos mxaos is offline
Mirth Newb
 
Join Date: Jun 2011
Location: Padua, Italy
Posts: 6
mxaos is on a distinguished road
Default

Quote:
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
Reply With Quote
  #6  
Old 03-23-2017, 08:49 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,111
narupley is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 03-23-2017, 11:56 PM
mxaos mxaos is offline
Mirth Newb
 
Join Date: Jun 2011
Location: Padua, Italy
Posts: 6
mxaos is on a distinguished road
Default

Quote:
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.
Reply With Quote
Reply

Tags
jar, java, package

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 09:32 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation