web stats
Dynamically calling jar method without restarting mirth - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-02-2017, 10:00 AM
surveshital surveshital is offline
Mirth Newb
 
Join Date: Jan 2012
Posts: 9
surveshital is on a distinguished road
Default Dynamically calling jar method without restarting mirth

Mirth Version : 3.4.2.8129
Java Version : 1.7.0_79

My aim is to call java method without restarting mirth.

importPackage(Packages.java.lang);
var contextClassLoader = java.lang.Thread.currentThread().getContextClassLo ader();
//logger.debug(LOGHEADER + ".contextClassLoader: " + contextClassLoader.toString());
var isolatedClassLoader = contextFactory.getIsolatedClassLoader();
//logger.debug(LOGHEADER +".isolatedClassLoader : " + isolatedClassLoader.toString());
isolatedClassLoader.loadClass("Packages.example.my test");
logger.debug(LOGHEADER +".loadClass : " + isolatedClassLoader.toString());

Error :

DETAILS:
Wrapped java.lang.ClassNotFoundException: Packages.example.mytest

Please advise...

Thanks!
Reply With Quote
  #2  
Old 05-02-2017, 10:11 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,117
narupley is on a distinguished road
Default

Make sure you add a directory resource for your JAR (Settings -> Resources), include that in the channel/connector you want to use it in (Summary Tab -> Set Dependencies -> Library Resources), and then redeploy the channel.
__________________
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-02-2017, 10:37 AM
surveshital surveshital is offline
Mirth Newb
 
Join Date: Jan 2012
Posts: 9
surveshital is on a distinguished road
Default

Thanks for the reply!

Yes i did added the required jar in resources and check the dependencies setting still getting same error.

Wrapped java.lang.ClassNotFoundException: Packages.example.mytest
at e8b4c68c-901c-487f-8d49-64f6d13ec3fb_Deploy:124 (doScript)
Reply With Quote
  #4  
Old 05-02-2017, 11:14 AM
surveshital surveshital is offline
Mirth Newb
 
Join Date: Jan 2012
Posts: 9
surveshital is on a distinguished road
Default

I guess i'm able to load the class by changing below line.

old : isolatedClassLoader.loadClass("Packages.example.my test");
New : isolatedClassLoader.loadClass("example.my test");

But somehow not able to access the method present inside the class...

var contextClassLoader = java.lang.Thread.currentThread().getContextClassLo ader();
var isolatedClassLoader = contextFactory.getIsolatedClassLoader();

var clazz = isolatedClassLoader.loadClass("example.mytest");
logger.debug(LOGHEADER +".loadClass : " + isolatedClassLoader.toString());

var test = clazz.returnSomething();

logger.info(".test : " + test.toString());

Error:
TypeError: Cannot find function returnSomething in object class example.mytest.

Java Class used:

package example;

public class mytest {

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

Last edited by surveshital; 05-02-2017 at 11:22 AM.
Reply With Quote
  #5  
Old 05-04-2017, 07:06 AM
surveshital surveshital is offline
Mirth Newb
 
Join Date: Jan 2012
Posts: 9
surveshital is on a distinguished road
Default

Thanks Narupley

I was doing wrong, need to initiate the object for that i use the newInstance method and it worked for me.

var obj = clazz.newInstance();
var outText = obj.returnSomething();
Reply With Quote
  #6  
Old 07-17-2019, 01:01 PM
ambsw ambsw is offline
Mirth Newb
 
Join Date: Aug 2014
Posts: 15
ambsw is on a distinguished road
Default

If you need to pass arguments to the constructor, it's even more complicated (and the recommended way isn't working for me). See my JIRA comment for more info. Hopefully, I'll cross-post a solution since Google brought me here when troubleshooting the isolated CassLoader.
Reply With Quote
Reply

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 11:14 AM.


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