Archi Forum

Discussion Boards => jArchi => Topic started by: Thomas Rischbeck (ITMC.ch) on August 12, 2020, 11:54:38 AM

Title: Using external .jar in jscript
Post by: Thomas Rischbeck (ITMC.ch) on August 12, 2020, 11:54:38 AM
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?
Title: Re: Using external .jar in jscript
Post by: Phil Beauvoir on August 12, 2020, 12: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?
Title: Re: Using external .jar in jscript
Post by: Thomas Rischbeck (ITMC.ch) on August 12, 2020, 12: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?

Title: Re: Using external .jar in jscript
Post by: Phil Beauvoir on August 12, 2020, 12: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.
Title: Re: Using external .jar in jscript
Post by: Thomas Rischbeck (ITMC.ch) on August 14, 2020, 09: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!
Title: Re: Using external .jar in jscript
Post by: Phil Beauvoir on August 14, 2020, 09: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.
Title: Re: Using external .jar in jscript
Post by: Phil Beauvoir 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
Title: Re: Using external .jar in jscript
Post by: Thomas Rischbeck (ITMC.ch) 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.