===================================
The Nashorn scriptengine is being removed in java 11
===================================
(but it hasn't gone yet. See post #26 of this thread)
Important: the scriptengine has nothing to do with browsers nashorn cannot be run in a web page, it is for server side applications.
Here is a small library that allows use of javascript from B4J.
Some functions are easier to perform in js as you have basically all the java functions at your disposal to use.
The B4J project is overly commented to show how to use the lib.
It has example of converting a uniicode string to a string B4J understands.
It implements a crude split function.
The js script can be in-line or read from an external file.
The jNashorn (o-r-n) is the updated library using the preferred script engine for javaFX 8
Almost identical to jScriptEngine in use, but the engine itself very powerful.
Read up on Nashorn to see the full extent of the engine. Requires Java 8 to run.
UPDATE
Found a useful bit of code to allow access to b4j variables and routines from within the js script
(tested on Java 8, with jScriptEngine and jNashorn libraries)
=================================================================
UPDATE 28/10/15
Added 2 new files
nashorn invocable test ( the test file)
jInvokeNashorn (the library)
The first is a test file for the second , which is a new version of the nashorn library.
This version is quite different to the original jNashorn library
It does not support engineGet/Put , evalString or evalFile.
It simply has 2 entry points,
1, initInvocable()
2, Invoke()
initInvocable takes one parameter, it can be a string containing the script or the name of a script file (ending with .js) , if it is a script file it will look in the folder ./scripts for the file.
Invoke takes as many parameters as you like, but the first is always the function name in the script you wish to run.
The test file is well commented on how to use the new library.
You no longer need the get/put/eval in the original library as you can directly invoke functions in the script, which can return values or even call b4j subs directly with parameters.
Forgot to comment some lines in the test program.
the dim b4j as string
and
b4j = me
b4j = b4j.replace("class ","")
is needed to allow the script to access the b4j subs and variables directly.
The Nashorn scriptengine is being removed in java 11
===================================
(but it hasn't gone yet. See post #26 of this thread)
Important: the scriptengine has nothing to do with browsers nashorn cannot be run in a web page, it is for server side applications.
Here is a small library that allows use of javascript from B4J.
Some functions are easier to perform in js as you have basically all the java functions at your disposal to use.
The B4J project is overly commented to show how to use the lib.
It has example of converting a uniicode string to a string B4J understands.
It implements a crude split function.
The js script can be in-line or read from an external file.
The jNashorn (o-r-n) is the updated library using the preferred script engine for javaFX 8
Almost identical to jScriptEngine in use, but the engine itself very powerful.
Read up on Nashorn to see the full extent of the engine. Requires Java 8 to run.
UPDATE
Found a useful bit of code to allow access to b4j variables and routines from within the js script
(tested on Java 8, with jScriptEngine and jNashorn libraries)
B4X:
...
Dim js As jScriptEngine
Dim ss As String = Me
ss = ss.SubString(ss.IndexOf(" ")+1) ' build class name b4j.example.main is default
js.evalstring($"var b4j = Java.type(""$ & ss & $"");"$)
js.evalString($"b4j._test(" there");"$) '<<< runs b4j sub test
End Sub
Sub test(s As String)
Log("hello" & s)
End Sub
=================================================================
UPDATE 28/10/15
Added 2 new files
nashorn invocable test ( the test file)
jInvokeNashorn (the library)
The first is a test file for the second , which is a new version of the nashorn library.
This version is quite different to the original jNashorn library
It does not support engineGet/Put , evalString or evalFile.
It simply has 2 entry points,
1, initInvocable()
2, Invoke()
initInvocable takes one parameter, it can be a string containing the script or the name of a script file (ending with .js) , if it is a script file it will look in the folder ./scripts for the file.
Invoke takes as many parameters as you like, but the first is always the function name in the script you wish to run.
The test file is well commented on how to use the new library.
You no longer need the get/put/eval in the original library as you can directly invoke functions in the script, which can return values or even call b4j subs directly with parameters.
Forgot to comment some lines in the test program.
the dim b4j as string
and
b4j = me
b4j = b4j.replace("class ","")
is needed to allow the script to access the b4j subs and variables directly.
Attachments
Last edited: