SSJS调用java类中的方法(在java库中) [英] SSJS to call a method in java class (in java library)

查看:32
本文介绍了SSJS调用java类中的方法(在java库中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有 Java 类 (Invoke) 的 Java 库(名为:invoke).在设计器导航窗格中的代码下展开脚本库时会看到它.

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.

代码是:

package com.kkm.vijay;   

public class Invoke {

    public static void main(String[] args) {

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

    }
}

使用以下 ssjs按钮onclick 事件在浏览器中预览时显示 错误:500.

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();

即使我在类中使用了一个函数并将 ssjs 的最后一行更改为 v.fn().还是一样的问题.

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

推荐答案

有很多错误,正如 Fredrik 提到的,您应该打开标准错误页面.

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

您的第一个代码将无法运行,因为它没有正确捕获异常.您还使用了 main() 方法,该方法通常用于执行程序.但是你在没有任何参数的情况下调用它.除非用于执行应用程序,否则请避免使用该方法.

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.

所以改成这样:

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();
        }

    }
}

您应该将该代码放在 Designer 的新 Java 视图中.

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();

测试它应该可以正常工作.接下来的问题是,由于它是 SSJS,应用程序将在服务器上执行.这可能存在安全隐患,您可能需要修改 java.policy 文件才能执行此操作.

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.

相关权限为java.io.FilePermission.

这篇关于SSJS调用java类中的方法(在java库中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆