Unable to load Javascript modules Linux

126 views
Skip to first unread message

dkm

unread,
Feb 16, 2017, 6:23:31 PM2/16/17
to Union Platform
I have been running Union for a long time on Windows and now am trying a new Linux VPS. I installed Java as per recommendations (1.8 from Oracle java site). I can start and connect to server fine but when I try join any room with a Javascript module, it fails with error:
ERROR - Error loading script [ShisenMod.js].
javax.script.ScriptException: ReferenceError: "importClass" is not defined in <eval> at line number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)

My javascript module is the same as one running on Windows and is in the 'modules' directory under Union home.
Any ideas?

dkm

unread,
Feb 18, 2017, 2:56:25 AM2/18/17
to Union Platform
I have found the issue. Same problem occurred when I loaded on a new Windows computer with Java 1.8. Going back to Java 1.7 solves the problem with loading Javascript modules.

Derek

unread,
Feb 18, 2017, 6:16:41 PM2/18/17
to Union Platform
Thank you for the report I have created a ticket and will report back when I have more information.

Derek

dkm

unread,
May 31, 2017, 12:57:32 AM5/31/17
to Union Platform
Any news on this issue, Derek?

It appears that javascript modules, even the most simple one, do not work on Java 1.8 (unless I have something peculiar in my setup). Please check.
I can survive for now on Java 1.7 but some other third software is now insisting on Java 1.8 - if I update or it happens by mistake, I am screwed.

Toby G

unread,
Jul 2, 2018, 9:56:03 PM7/2/18
to Union Platform
I encountered this problem quite some time ago, and have been stuck using Java 7 still.
Fortunately, it is still available.

There is this, if anyone wants to try:
https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide

It describes how Rhino has been replaced by Nashorn (whatever that is).
Supposedly there is some compatibility module which enables you to use importClass,
but I could not get it to work.

I'm surprised the Union Developers never encountered this on their own,
since for any new installation, the default Java is now version 8 or higher.
You have to do some work to get Java 7 installed.

Derek

unread,
Jul 19, 2018, 5:17:12 AM7/19/18
to Union Platform
As mentioned, by default Java 1.8 uses the Nashorn engine for Javascript. I'm looking at the best way to allow Rhino to be used as a replacement.

However, in the meantime you may be able to get around the "importClass" issue by instead using Java.type. For example:

importClass(net.user1.union.core.event.RoomEvent);

changes to

var RoomEvent = Java.type("net.user1.union.core.event.RoomEvent");

Changing the importClass to instead use Java.type was the only change I needed to get my test scripts migrated. 

Cheers,

Derek

Toby G

unread,
Jul 24, 2018, 11:31:09 AM7/24/18
to Union Platform
On Thursday, July 19, 2018 at 2:17:12 AM UTC-7, Derek wrote:
However, in the meantime you may be able to get around the "importClass" issue by instead using Java.type. For example:
importClass(net.user1.union.core.event.RoomEvent);
changes to
var RoomEvent = Java.type("net.user1.union.core.event.RoomEvent");

Is that a Java 1.8 construct?   It doesn't work with Java 1.7.
Or do I need to put something else which defines "Java"?

To try it out, I put
> var Logger = Java.type("org.apache.log4j.Logger");

and I got:
> Error: "Java" is not defined.

So I tried with lower case J: java.type
and I got
> Cannot call property type in object [JavaPackage java]. It is not a function, it is "object".

Unfortunately, it is near impossible to find anything about using the word "Java",
since all search results are based on interpreting it as the name of the language.

Derek

unread,
Jul 24, 2018, 2:56:04 PM7/24/18
to Union Platform
"Java" in "Java.type" is a global object defined in Nashorn. You can use it to convert your old Rhino scripts that used importClass in Java 1.7 to be compatible with using Nashorn in Java 1.8.

If you make the change and then upgrade to Java 1.8+ your script should work unless you've got other Rhino->Nashorn incompatibilities.


Toby G

unread,
Jul 25, 2018, 3:54:10 AM7/25/18
to Union Platform


On Tuesday, July 24, 2018 at 11:56:04 AM UTC-7, Derek wrote:
If you make the change and then upgrade to Java 1.8+ your script should work unless you've got other Rhino->Nashorn incompatibilities.

It did! What magic! Thanks a lot, Derek.
 

Toby G

unread,
Jul 25, 2018, 6:25:09 PM7/25/18
to Union Platform
Here is a perl program which will read your code files and convert whole lines that look like this:
importClass(a.b.c.d);
to
var d = Java.type("a.b.c.d");

You can use it as a one time thing to save some editing time.
I'm using it to transition: I'll keep the importClass statements in my sources (for a while),
and as part of the release process run this program to produce the actual module files.

#!/usr/bin/perl
while (<>)
{
 $line
= $_;
 
if (m/importClass\((.*)\);/)
   
{
    $cl
= $1;
    $cl
=~ m/.*\.(.*)/;
    $name
= $1;
    $line
= "var $name = Java.type(\"" . $cl . "\");\n";
   
}
 
print $line;
}



Reply all
Reply to author
Forward
0 new messages