SSJS to call a method in java class (in java library)

僤鯓⒐⒋嵵緔 提交于 2019-12-19 10:48:15

问题


I've created a java library (named: invoke) with a java class (Invoke). Its seen when expanding Script libraries under code in the designer navigation pane.

The code is:

package com.kkm.vijay;   

public class Invoke {

    public static void main(String[] args) {

        Runtime r = Runtime.getRuntime();
        Process p = r.exec("C://some.exe");

    }
}

Used the following ssjs to an onclick event of a button shows Error:500 when previewed in browser.

importPackage(com.kkmsoft.vijay);
var v=new Invoke();
v.main();

Even i used a function inside the class and changed the last line of ssjs to v.fn(). Yet the same problem.


回答1:


There are a number of things wrong, and as Fredrik mentions you should switch on the standard Error page.

Your first code won't run because it is not correctly capturing the Exception. You are also using a main() method, which is normally used to execute a program. But you are calling it without any arguments. Avoid using that method unless it is for executing an application.

So change it to this:

package com.kkm.vijay;   

import java.io.IOException;

public class Invoke {

    public void mainCode() {

        Runtime r = Runtime.getRuntime();
        try {
            Process p = r.exec("C://WINDOWS//notepad.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

You should put that code in the new Java view in Designer.

Next your button code needs to change.

var v=new com.kkm.vijay.Invoke();
v.mainCode();

Testing that it should work fine. The issue next is, as it is SSJS the application will execute on the server. There may be security implications in this, and it may need you to modify the java.policy file in order to do this.

The related permission will be java.io.FilePermission.



来源:https://stackoverflow.com/questions/16932516/ssjs-to-call-a-method-in-java-class-in-java-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!