cancel
Showing results for 
Search instead for 
Did you mean: 
Security Alert Log4j Security Vulnerability. Click here to know more.
cancel
Showing results for 
Search instead for 
Did you mean: 

PTC integrity : How can I import my Java packages into a js file

EP_9964574
5-Regular Member

PTC integrity : How can I import my Java packages into a js file

Hello, I have a similar question. How can i call a .vbs or .ps1 script from the javascript thats triggered in RVS? I tried to use 

this [ var objShell = new ActiveXObject("wscript.shell"); ] but it throws error stating ActiveXObject not defined. is there any other way i can call powershell scripts using triggered javascripts? is there any java package that i can directly use? Thanks  

1 ACCEPTED SOLUTION

Accepted Solutions
Rocko
14-Alexandrite
(To:EP_9964574)

An easier way than my previous reply (posted below), without the need for java would be to wrap the java in JS. See the sample trigger code below, you can experiment with it, but the caveats still apply.

var runtime=Packages.java.lang.Runtime.getRuntime();
// create java array, not JS array
var arry=new java.lang.reflect.Array.newInstance(java.lang.String,2);
arry[0]="D:/Windchill/Toolkit/mksnt/touch.exe";
arry[1]="D:/Temp/1.txt";
runtime.exec(arry);

 

View solution in original post

6 REPLIES 6
Rocko
14-Alexandrite
(To:EP_9964574)

First, this is not recommended, as you would spawn an external process, which could bring down the server if it doesn't return. So if you want to go there, you'd better have very robust code. Also, security must be considered.

Then, you are not running in a full-fledged JS environment you know from your browser, you have only limited support for JS objects - RVS uses the Rhino engine.

So one place to look for techniques would be the rhino documentation.

The easiest approach would be to write a starter class in java, and in your trigger just call that starter class. The java launcher method would look like this (sample which provides the script inline and saves a .docx as .pdf):

String[] cmds = new String[5];
cmds[0] = "powershell.exe";
cmds[1] = "-executionpolicy";
cmds[2] = "ByPass";
cmds[3] = "-Command";
cmds[4] = "$filename = '" + filePath + fileName + "';"
+ "$word_app = New-Object -ComObject Word.Application; $document = $word_app.Documents.Open($filename);"
+ "if ($document.TablesOfContents -ne $null) {$document.TablesOfContents.item(1).Update();};";
if (genPDF) {
cmds[4] = cmds[4]
+ "$pdf_filename = '" + filePath + fileName.replace(".docx", ".pdf") + "';"
+ "$document.SaveAs([ref] $pdf_filename, [ref] 17);";

}
cmds[4] = cmds[4] + "$document.Close();";
cmds[4] = cmds[4] + "$word_app.quit();";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmds);
proc.getOutputStream().close();
proc.waitFor();

Put this into a class, compile, drop the class into the java\classes subdirectory as usual, restart the server to load it. Then your trigger can call the method of that class.


EP_9964574
5-Regular Member
(To:Rocko)

I'm actually tying to Call Rest Api (Ex. Azure)  to get and post data after Authentication by caling Powershell in JS and read the response. Can i do it directly with JS trigger without any custom Java package?

Rocko
14-Alexandrite
(To:EP_9964574)

Well you can use any class that comes with standard Java for this if you address the classes like above. Also you should be able to use  HTTPClient class as it is in RVS's classpath. I haven't done it myself, but check this JS example, which may work for you:

https://stackoverflow.com/questions/22157910/using-rhino-js-engine-to-make-http-requests

 

Authentication and request must be done in the same trigger call because the transaction closes when the trigger finishes (unless you store and restore the session key somewhere).

 

Rocko
14-Alexandrite
(To:EP_9964574)

Also, in your trigger directory should be a script called CallBPEL.js which launches a SOAP request, which you could use as a starting point. (Attached for reference)

Rocko
14-Alexandrite
(To:EP_9964574)

An easier way than my previous reply (posted below), without the need for java would be to wrap the java in JS. See the sample trigger code below, you can experiment with it, but the caveats still apply.

var runtime=Packages.java.lang.Runtime.getRuntime();
// create java array, not JS array
var arry=new java.lang.reflect.Array.newInstance(java.lang.String,2);
arry[0]="D:/Windchill/Toolkit/mksnt/touch.exe";
arry[1]="D:/Temp/1.txt";
runtime.exec(arry);

 

View solution in original post

EP_9964574
5-Regular Member
(To:Rocko)

Thanks @Rocko . 

runtime.exec(); Works!! for Powershell call. Thanks
Announcements

Windchill RVS Users


Please take our

Windchill RV&S Authoring with Formula Editor Survey