How can I execute JavaScript from Java code ?

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
progm
Active Member

How can I execute JavaScript from Java code ?

Hello everyone, 

I'm trying to execute some JavaScript code ( using Alfresco JavaScript API method ) from Java. Please if someone know how to do this. 

Thanks in advance.

6 Replies
jpotts
Advanced II

Re: How can I execute JavaScript from Java code ?

Without more context the best I can do is suggest that you read the source. Specifically, take a look at 

org/alfresco/repo/action/executer/ScriptActionExecuter.java to see how Alfresco leverages the ScriptService to run server side JavaScript such as when someone configures a folder rule to use "Execute Script".

Depending on what you want to do you might also pick up a few tips by looking at org/alfresco/repo/processor/ScriptServiceImpl.java.

afaust
Master

Re: How can I execute JavaScript from Java code ?

The script action should be the preferred way to call a JavaScript source file sitting on your classpath or stored in a node within the Repository. That way you can be sure that you could use that very same file also for rules or user-triggered actions. ScriptService is the low-level API for invoking JavaScript code and - apart from my attempts of writing a Nashorn based engine or running non-persistent, user-provided JavaScript code - I have so far not found any good reason to use that over the script action in any project.

progm
Active Member

Re: How can I execute JavaScript from Java code ?

Mr. Jeff , Mr. Axel thank you for the reply and the precise answer. 

I did took a look at those classes and did some research. I found this : 

// Create the script node reference
NodeRef script = this.nodeService.createNode(
this.folder,
ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testScript.js"),
ContentModel.TYPE_CONTENT).getChildRef();
this.nodeService.setProperty(script, ContentModel.PROP_NAME, "testScript.js");
ContentWriter contentWriter = this.contentService.getWriter(script, ContentModel.PROP_CONTENT, true);
contentWriter.setMimetype("text/plain");
contentWriter.setEncoding("UTF-8");
contentWriter.putContent("\"VALUE\";");

// Create the action
Action action1 = this.actionService.createAction(ScriptActionExecuter.NAME);
action1.setParameterValue(ScriptActionExecuter.PARAM_SCRIPTREF, script);

// Execute the action
this.actionService.executeAction(action1, this.nodeRef);


Is it like that we can execute JavaScript from a Java class? If it is, how do I import "ScriptActionExecuter"
in my Java code that lets me executing the code above.

Sorry if I misunderstood something, I'm new to this field.

Thanks a lot.

jpotts
Advanced II

Re: How can I execute JavaScript from Java code ?

Yes, there are many ways to do it, but that is one. You are basically leveraging the out-of-the-box script action. This is the same as if you had created a folder rule and selected "Execute Script" as the action, and then specified a JavaScript file that is sitting in the Data Dictionary/Scripts folder.

progm
Active Member

Re: How can I execute JavaScript from Java code ?

Thank you Mr. ‌ for your reply. But how to get this code works inside my Java class ? there is classes not defined, how do I import them all to my code and execute them. If you can please Mr. Jeff describe to me the steps. Thanks a lot. 

douglascrp
Advanced II

Re: How can I execute JavaScript from Java code ?