Using external .jar in jscript

Started by Thomas Rischbeck (ITMC.ch), August 12, 2020, 11:54:38 AM

Previous topic - Next topic

Thomas Rischbeck (ITMC.ch)

We'd like to create WORD files from a script.

For this, we consider using the external library https://www.javadocx.com/documentation. This is basically a .jar file.

The script would look like this (taking https://www.n-k.de/riding-the-nashorn/#_invoking_java_methods_from_javascript into account) :

console.log("New Archi Script");
var mydocx = Java.type("com.javadocx.CreateDocx");
var thedoc = new mydocx();
//thedoc.addText("And now the same link with some additional formatting:");
thedoc.createDocx("XXX");

However, there is a class not found exception. Is there a way to modify the classpath and use external .jar files to reference from within scripts?
ITMC AG • Bahnhofstrasse 10 • CH-8001 Zürich
Dr. Thomas Rischbeck
PhD in Computer Science
Managing Partner   
rischbeck@itmc.ch
www.itmc.ch

Phil Beauvoir

August 12, 2020, 12:00:43 PM #1 Last Edit: August 12, 2020, 12:23:15 PM by Phil Beauvoir
I'm not sure changing the classpath is possible as I think it's the classpath of jArchi itself, unless someone has done this before. Maybe you could spawn another Java process and command with the right classpath?
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Thomas Rischbeck (ITMC.ch)

Where would you do that? Are you refering to the $EXEC command in Nashorn? I don't think this would work, because I need the connection between the objects in the Archi model and the WORD commands.

Or do you mean we could start Archi directly with another classpath?

ITMC AG • Bahnhofstrasse 10 • CH-8001 Zürich
Dr. Thomas Rischbeck
PhD in Computer Science
Managing Partner   
rischbeck@itmc.ch
www.itmc.ch

Phil Beauvoir

I think it's likely that because of the way that Eclipse-based plugins have their own classpath that the classpath can only be set in jArchi itself when jArchi is compiled. Unless anyone knows different I would say that this is not possible afterwards.
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Thomas Rischbeck (ITMC.ch)

Dear Phil

thanks for your help and pointers - we've now found a (somewhat ugly) workaround to solve our issue.
We've recompiled the Script plugin with the additional classpath from within the Eclipse RCP development environment.

This results in a somewhat different MANIFEST.MF file - the additional line references the external JAR that we want to use from within JavaScript:

Bundle-ClassPath: lib/javadocx.jar,
.


In addition, the lib/javadocx.jar must be added to com.archimatetool.script_0.7.1.202001311431.jar.

With these two changes it works and the full functionality of javadocx is available, great!
ITMC AG • Bahnhofstrasse 10 • CH-8001 Zürich
Dr. Thomas Rischbeck
PhD in Computer Science
Managing Partner   
rischbeck@itmc.ch
www.itmc.ch

Phil Beauvoir

August 14, 2020, 09:18:34 AM #5 Last Edit: August 14, 2020, 09:50:10 AM by Phil Beauvoir
That is the correct (and probably only) way to do it for any additional dependency. Eclipse plug-ins are OSGi bundles and the classpath and all dependencies have to be declared in the bundle itself. That's just the way it is.
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Phil Beauvoir

February 08, 2021, 18:23:51 PM #6 Last Edit: February 08, 2021, 18:31:40 PM by Phil Beauvoir
See https://forum.archimatetool.com/index.php?topic=958.0 for how to add a jar to the classpath in Nashorn and in GraalVM.

I also added the info to the wiki - https://github.com/archimatetool/archi-scripting-plugin/wiki/Runtime-Options
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Thomas Rischbeck (ITMC.ch)

Thanks Phil!

Very useful to know how to achive this in GraalVM!
Recently, I'm doing a lot of scripting and have switched to Graal due to its support for classes.
I'm working on generic "virtual views" that are fully driven by parametrized JScripts.

Cheers, Thomas.
ITMC AG • Bahnhofstrasse 10 • CH-8001 Zürich
Dr. Thomas Rischbeck
PhD in Computer Science
Managing Partner   
rischbeck@itmc.ch
www.itmc.ch