Author Topic: Using external .jar in jscript  (Read 228 times)

Thomas Rischbeck (ITMC.ch)

  • Member
  • **
  • Posts: 12
  • working on STAR: strategy - architecture
    • ITMC
Using external .jar in jscript
« on: August 12, 2020, 12:54:38 PM »
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

  • Archi Guy
  • Hero Member
  • *****
  • Posts: 1467
  • Archi Guy
    • Archi
Re: Using external .jar in jscript
« Reply #1 on: August 12, 2020, 13:00:43 PM »
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?
« Last Edit: August 12, 2020, 13:23:15 PM by Phil Beauvoir »
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Thomas Rischbeck (ITMC.ch)

  • Member
  • **
  • Posts: 12
  • working on STAR: strategy - architecture
    • ITMC
Re: Using external .jar in jscript
« Reply #2 on: August 12, 2020, 13:31:45 PM »
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

  • Archi Guy
  • Hero Member
  • *****
  • Posts: 1467
  • Archi Guy
    • Archi
Re: Using external .jar in jscript
« Reply #3 on: August 12, 2020, 13:42:23 PM »
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)

  • Member
  • **
  • Posts: 12
  • working on STAR: strategy - architecture
    • ITMC
Re: Using external .jar in jscript
« Reply #4 on: August 14, 2020, 10:12:57 AM »
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

  • Archi Guy
  • Hero Member
  • *****
  • Posts: 1467
  • Archi Guy
    • Archi
Re: Using external .jar in jscript
« Reply #5 on: August 14, 2020, 10:18:34 AM »
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.
« Last Edit: August 14, 2020, 10:50:10 AM by Phil Beauvoir »
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Phil Beauvoir

  • Archi Guy
  • Hero Member
  • *****
  • Posts: 1467
  • Archi Guy
    • Archi
Re: Using external .jar in jscript
« Reply #6 on: February 08, 2021, 18:23:51 PM »
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
« Last Edit: February 08, 2021, 18:31:40 PM by Phil Beauvoir »
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Thomas Rischbeck (ITMC.ch)

  • Member
  • **
  • Posts: 12
  • working on STAR: strategy - architecture
    • ITMC
Re: Using external .jar in jscript
« Reply #7 on: February 10, 2021, 11:26:17 AM »
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