How can I execute JavaScript from Java code ?

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

How can I execute JavaScript from Java code ?

Jump to solution

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.

1 Solution

Accepted Solutions
jpotts
Professional

Re: How can I execute JavaScript from Java code ?

Jump to solution

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.

View solution in original post

7 Replies
jpotts
Professional

Re: How can I execute JavaScript from Java code ?

Jump to solution

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 ?

Jump to solution

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 ?

Jump to solution

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
Professional

Re: How can I execute JavaScript from Java code ?

Jump to solution

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 ?

Jump to solution

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 ?

Jump to solution
hannahkhisty
Member II

Re: How can I execute JavaScript from Java code ?

Jump to solution

Hey

Create a file with hello.js

Write and save the code in the file

Open terminal

Write command jjs hello.js and press enter

After procedure you will find output.