cancel
Showing results for 
Search instead for 
Did you mean: 

Executing a batch from a script

shlomi
Champ in-the-making
Champ in-the-making
Hello,

My alfresco is installed on windows, and I have created a space called incoming. There is a rule on this space, that distribute the files from it to their respective space.

In a specific situation, I want to execute a batch file (.bat), that loads the content of the file into the DB (oracle), using sqlldr, and then move it.

My script is written in JavaScript, and runs on the server side.

I tried the example below:
var oShell = new ActiveXObject("Shell.Application");var commandtoRun = "calc.exe";// Invoke the execute method.oShell.ShellExecute(commandtoRun, "", "", "open", "1");‍‍‍‍‍‍

That has raised a ScriptException, with the error "ActiveXObject is not defined".

Is there a way to do it??

This is really important, and I have to demonstrate this tomorrow.
Please let me know what is the best way to do it..

Shlomi
9 REPLIES 9

kevinr
Star Contributor
Star Contributor
As you said your javascript "runs on the server-side" - it does not have access to any Browser related objects or Browser JavaScript APIs such as ActiveXObject.

shlomi
Champ in-the-making
Champ in-the-making
Hi, Thank you for your reply.

As you said your javascript "runs on the server-side" - it does not have access to any Browser related objects or Browser JavaScript APIs such as ActiveXObject.

I see, in that case, what are my options with integrating a third party application (on the server side) that needs to execute by a rule on a specific file? (say when the file arrives, and have a certain extension to its name, run a third party application, that uploads this file to our database).

Regards,
Shlomi

kevinr
Star Contributor
Star Contributor
A Rule executes a Repository Action object - these objects are Java code (usefully they can also be called via the JavaScript API) - and as such can make use of any Java features such as executing external processes.

Thanks,

Kevin

shlomi
Champ in-the-making
Champ in-the-making
Thank you,
That is exactly what I have done, and it worked perfectly.

Good day,
Shlomi

anovotny
Champ in-the-making
Champ in-the-making
Hi,
I need to perform the same task (run .bat file), but  don't have much experiences with Java and JavaScript and from above text I didn't get it.
Could you please write some short example how to achieve it?
Thank you
Ales

gyro_gearless
Champ in-the-making
Champ in-the-making
Hi foilks,

uncle Gyro's 2 ct for the weekend:

Usually (say in 9 out of 10 cases) it is bad idea to call out to external processes from Java, especially on the server side: you have to take care of OS pecularities, blocking behaviour, security and the like. In an EJB environment this isn't even allowed for that reasons (usually in a non-strict sense, all application servers i know dont thwart that…).

In most cases, Java offers a more "javaish", more straight way to perform task - for the SQLLDR case it would be to open a straight JDBC connection and feed the database!

Said that, of you absolute must, Runtime.exec() or class ProcessBuilder are your friends. But beware: when using exec(), you have to take care to spawn two consumer threads to gobble the subprocess' output streams, otherwise your exec() call may block. I suppose there are some helper classes out there.

HTH
Gyro

anovotny
Champ in-the-making
Champ in-the-making
Hello,
maybe I am missing some important part of knowledge…
I tried to execute this very short script:
var r = Runtime.getRuntime();r.exec("calc.exe");‍‍‍

And received the error
org.alfresco.error.AlfrescoRuntimeException: 10300137 Error during command servlet processing: 10300136 Failed to execute script 'workspace://SpacesStore/866c3275-531c-4823-8ad2-14f2a622baac': 10300135 ReferenceError: "Runtime" is not defined. (workspace://SpacesStore/866c3275-531c-4823-8ad2-14f2a622baac#13)caused by:org.alfresco.scripts.ScriptException: 10300136 Failed to execute script 'workspace://SpacesStore/866c3275-531c-4823-8ad2-14f2a622baac': 10300135 ReferenceError: "Runtime" is not defined. (workspace://SpacesStore/866c3275-531c-4823-8ad2-14f2a622baac#13)‍‍‍

What am I doing wrong?

Thank you.
Ales

kevinr
Star Contributor
Star Contributor
I assume that is JavaScript? For security reason, you cannot execute raw Java code within our Rhino JavaScript integration - to prevent some extremely unpleasant possibilities! See my post above: http://forums.alfresco.com/en/viewtopic.php?p=32386#p32386
you need to wrap your Java code into an Alfresco "action" bean that you can then call from the JavaScript code via the "actions" object - that is exactly what the other user above did to get it working.

Cheers,

Kev

anovotny
Champ in-the-making
Champ in-the-making
Thanks for the answer.
Yes, it is javascript and I got your point.
So I try to follow this http://wiki.alfresco.com/wiki/Custom_Actions and this http://www.ecmarchitect.com/images/articles/alfresco-actions/actions-article.pdf
But I don’t understand where should be placed the code with class definitions. Maybe I am missing some basic knowledge…
Could you please write some short step-by-step procedure like for idiots?
Thanks a lot
Ales