1

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.

Simon O'Doherty
  • 9,259
  • 3
  • 26
  • 54
vijay
  • 51
  • 1
  • 9

1 Answers1

5

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.

Java view of image

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.

Simon O'Doherty
  • 9,259
  • 3
  • 26
  • 54
  • 1
    It now worked Simon. But, What if i use default package for the java class? How the ssjs code changes? Also how to use a jar file(in system folder) in the same fashion? – vijay Jun 05 '13 at 08:45
  • You should refrain from using the default package for Java classes (bad programming). JARs in the lib/ext folder should be accessible the same way. For inside the NSF see the following. http://stackoverflow.com/questions/14464827/are-jar-files-in-webcontent-web-inf-lib-available-to-java-design-elements-in-dom – Simon O'Doherty Jun 05 '13 at 09:38
  • 1
    As simon stated.. Never use the default package. Package names are there to organize code. Package names give your code a context and a sort of meaning. If you put a 'file' object in the default package It isn't really clear what the purpose of this class is , when it is used and who / where it was written. If you put it in a package lets say your.application.util. Another developer knows its there customized for your application, it's somehow used as a util class and it is a file. When you need that class lateron you know it was orignaly created for application x. – jjtbsomhorst Jun 08 '13 at 08:27