Accessing methods in an external JAR

Just new to Servoy, not sure of the best way to do this. I am migrating from FMP and there I used Jplugin to call the Java methods.

My app needs to call methods from classes provided in JARs from a third party, and record the response. The JARs are specified on the CLASSPATH.

EG, Their EClaim-3.x.jar contains the class EClaimAPI.class among others.
I’m calling this with e.g.

sessionId = EClaimAPI.getInstance().createSessionEClaim(arg1,arg2);
or
rval = EClaimAPI.getInstance().getUniqueId(sessionId,result);

(BTW result is a returning a vector)

Do I need to create a plugin to access this, or is there another way?

antonio:
Do I need to create a plugin to access this, or is there another way?

AFAIK, you cannot directly access arbitrary Java methods from Servoy’s methods JavaScript code. You have to create a plugin that exposes the proper methods to Servoy and acts as a bridge between Servoy and your third party Java libraries.
I hope I’m not wrong ':slight_smile:

As far as I remember (tried a while ago), you can call any Java method (if the class is reacheable) if you start your call with “java”. So in your case you could try

sessionId = java.EClaimAPI.getInstance().createSessionEClaim(arg1,arg2); 

This works

sessionId = Packages.EClaimAPI.getInstance().createSessionEClaim(arg1,arg2);

Thanks for the heads up!

PS, then I simply created some global methods for each of the calls, to make them all easily available.

Yes, right, Packages is the one. I think if you want to access a standard Java package, then you need Java…

So far, so good. I also got this working (in developer): creating classes and calling methods from an external jar. For this I placed the external jar in the application_server\plugins directory and adressed the jar like Packages.net.sf.SomeClass.

But what do I have to do if I want to deploy this to an application server? Copying the jar is not enough, I get the error message “TypeError: SomeClass is not a function, it is org.mozilla.javascript.NativeJavaPackage.”
Already restarted application server and cleaned java cache.

Hi ,

Can anybody please help us pointing us in the right direction in deploying external jar’s on the application server ?

jnlp file required ? Jar file location ?

Thanks,

I haven’t tested this, but try putting them in the beans folder.

Good resources for you outside of the forum regarding this topic:

http://www.mozilla.org/rhino/scriptjava.html

http://servoy-stuff.net

Hope the information is helpful.

I guess you’re talking about Smart Client, right?

In that scenario, you need a JNLP file to “tell” Servoy to ship the JAR file off to the Smart Client.

For examples, see the the existing JNLP files in the application_server\plugins directory

Paul

That is correct Paul, smart client running with an application server in which our solution was imported. But keep in mind: it is not a plugin it is just an external jar from which we want to access a class in the following way

var _java_class = new Packages.org.myclasses.SomeClass()

We have created the following jnlp

<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="%%serverURL%%" href="/servoy-client/plugins/myClasses.jar.jnlp">
   <information> 
      <title>myClasses</title> 
      <description>myClasses</description>
   </information>
   <resources>
      <jar href="/plugins/myClasses.jar" download="eager"/>
   </resources>
   <component-desc/>
</jnlp>

and copied the myClasses.jar and myClasses.jar.jnlp to the plugins directory of the application server. Restart, cleaning catalina cache, cleaning client java cache did not have the effect that the jar was loaded to the client (checked it in the java cache).

The jnlp needs to match a plugin that implements the IClientPlugin interface and then you include your extra jar in the JNLP for that “plugin”.

Another option is adding the jar to your beans folder: the libraries that are located in the beans folder get automatically shipped to the Smart Client.

Paul

pbakker:
Another option is adding the jar to your beans folder: the libraries that are located in the beans folder get automatically shipped to the Smart Client.

Thanks for confirming this Paul.
Out of interest, what will happen with the WebClient?

Hi!

I’m doing some experiences with JBarcodeBean and…

pbakker:
adding the jar to your beans folder: the libraries that are located in the beans folder get automatically shipped to the Smart Client.

… yes, it works properly!!! But…

antonio:
what will happen with the WebClient?

In fact, from Developer it works right in both scenarios, but when launched against an external server (against a production server) the .jar is shipped if launched in Smart Client, but when launched with Web Client, it crash…

Exists some way to do this? I mean, using an external .jar (without making a Servoy bean) through Internet in Web Client?

Best regards,

Gerardo.

but when launched with Web Client, it crash…

“crash” is a large, fuzzy concept… What exactly happens? What error is raised/logged in the log?

Paul

Hi Paul!

Firts of all, sorry for the poor information I wrote…

When I said crash, I meant classes are not being recognized in webclient when launched in remote… I’m doing something like this:

function _setBarcodeType(pElement, pBarcodeType)
{
	// Importing required package
	var barcodePkg = new JavaImporter(Packages.jbarcodebean);  
  
	if (!pBarcodeType)
		pBarcodeType = _barcode_type;
	
	with(barcodePkg) {
		// Create codeType object 
		// var objBarcodeType = eval("new " + pBarcodeType + "()");
		// var objBarcodeType = new Code11();
		var objBarcodeType = new Packages.jbarcodebean.Code11();		
		// Set codeType
		pElement.setCodeType(objBarcodeType);
	}
}

When executing var objBarcodeType = new Packages.jbarcodebean.Code11();, error raised is:

[attachment=0]errorWClientConPackagesPunto.JPG[/attachment]

When var objBarcodeType = new Code11(); (same error with eval strategy), the error is this:

[attachment=1]errorWClientConWith.JPG[/attachment]

As I said before, this code runs properly in Smart Client but Web client…

Best regards,

Gerardo.

Are the libs using Swing? If so you can not use it in a webclient.

When you start a debug webclient you should see more logging info/a stacktrace in Eclipse or your console.

How did you include the jar file in your Servoy installation? Throught the beans folder, or with a jnlp file in the plugins directory?

Paul

pbakker:
How did you include the jar file in your Servoy installation? Throught the beans folder, or with a jnlp file in the plugins directory?

By now, I included it in beans folder (to get access in Developer)… I was wondering what happened if I included it with a jnlp… Do you think I’d must to include in plugin’s folder with a jnlp?

Best regards,

Gerardo.