importing a Java package for use in serverside JavaScript

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

importing a Java package for use in serverside JavaScript

#1

Postby dvanhussel » Tue Apr 09, 2013 3:35 pm

Hi,

I have written a simple base64Encode class in Java that I want to use in serverside-JavaScript.

We already have a .jar file with some custom functions. In the same .jar is also a package 'nl.haarlem.utils' with some, guess what... utility functions :)

I added the new function as a static method to a new Class, named Encoding.

Then I used this import:

Code: Select all

importPackage(nl.haarlem.utils);
This generates the error:
org.mozilla.javascript.EcmaError.ReferenceError: "nl" is not defined
When I add the same class to the 'com.ebasetech.ufs.customfunctions' package of our .jar, I can import the package with:

Code: Select all

importPackage(com.ebasetech.ufs.customfunctions);
Then the new function is available as expected.

Do I have to 'register' our package-name to be able to use it?
0 x

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Tue Apr 09, 2013 4:14 pm

I think this is a problem with the way that Rhino (the Javascript implementation) works. It looks like it accesses java classes using a variable called 'Packages'. Prefixes 'com', 'org' and 'java' seem to have been defined for us (e.g. var com = Packages.com;). But you are trying to access a package called 'nl' which hasn't been defined.

So you should try:

Code: Select all

importPackage(Packages.nl.haarlem.utils);
or if this doesn't work, skip the import, and refer to the full class name e.g.

Code: Select all

var x = new Packages.nl.haarlem.utils.Encoding();
Or alternatively, use a package name that starts with 'com', 'org' or'java'.
0 x

User avatar
dvanhussel
Ebase User
Posts: 161
Joined: Fri Oct 19, 2007 12:45 pm
Location: Haarlem, the Netherlands

#3

Postby dvanhussel » Tue Apr 09, 2013 9:26 pm

Hi Jon,

It was indeed fixed when I added 'Packages' to the package defenition:

Code: Select all

importPackage(Packages.nl.haarlem.utils); 
Thanks!
0 x


Who is online

Users browsing this forum: No registered users and 19 guests