Skip to main content

Java 8 - Unanswered JavaScripting issues

4 replies [Last post]
mbilgin
Offline
Joined: 2006-05-18
Points: 0

Since I have not received any replies to my two previous JavaScript related issues, I am posting them together here again hoping that someone might show some interest (b104 has been used for testing).

1) - importClass / importPackage is broken

//---BEGIN CODE ---
importClass (java.util.Vector) ;

function test()
{
vect = new Vector() ;
}
//--- END CODE ---

throws javax.script.ScriptException: ReferenceError: "importClass" is not defined in at line number 1

This works perfectly when Java 7 is used.

Instead the following code seems to work in Java 8

//--- BEGIN CODE ---
function test()
{
vect = new java.util.Vector() ;
}
//--- END CODE ---

2) Strings that are passed back to Java are not an instance of java.lang.String

In this instance, a simple scripting is trying to pass a simple string back to Java, e.g.

//--- BEGIN CODE ---
myJavaObjectMethod.setString("Hello") ;
//--- END CODE ---

It appears that the object received by the Java setString method is a jdk.nashorn.internal.runtime.ConsString.

This then later is causing class cast exceptions:

java.lang.ClassCastException: jdk.nashorn.internal.runtime.ConsString cannot be cast to java.lang.String

To work around this, I have tried below which seems to get rid of the exception

//--- BEGIN CODE ---
myJavaObject.setString(new String("Hello")) ;
//--- END CODE ---

These two issues will break existing pre Java 8 JavaScript code bases.

Any answers, comments, feedback will be appreciated.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mbilgin
Offline
Joined: 2006-05-18
Points: 0

Looks like the second issue has been fixed in b106. The first one is still an issue and I have created a bug report with a simple test code. The following is the test code submitted.

// --- BEGIN CODE ---
package java8scripting;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class Java8Scripting {
public static void main(String[] args)
{
try
{
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript") ;
engine.eval("importClass(java.util.Vector)\n" +
"print(\"Hello world\") ;");
}
catch (Exception ex)
{
Logger.getLogger(Java8Scripting.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

// --- END CODE ---

ashf
Offline
Joined: 2013-09-16
Points: 0

It looks like a bug and should be submitted via bug reporting channel.

mbilgin
Offline
Joined: 2006-05-18
Points: 0

Thanks, already created a bug report with Oracle for the first issue.

rogerl
Offline
Joined: 2004-11-15
Points: 0