call java class from SAS. ERROR: Can not find class

advertisements
    data Numbers;
        do i=1 to 10;
            output;
        end;
    run;

    data PrimeNumbers;
        set Numbers;
        if _N_=1 then declare javaobj j("C:\Program Files\Java\jdk1.7.0_10\bin\checkPrimeNumber");
        isPrimeNumber=j.callIntMethod("checkPrimeNumber",i);
        if isPrimeNumber=1 then output;
    run;

    /*-SET CLASSPATH "C:\Program Files (x86)\Java\jre7\bin"*/

    /*--------------------------Sample java code----------------------------
    public class checkPrimeNumber
    {
        public checkPrimeNumber()
        {
        }
        public static void main(String args[])
        {
            checkPrimeNumber CPN=new checkPrimeNumber();
            System.out.println ("Result:"+CPN.checkPrimeNumber(23));
        }
        public int checkPrimeNumber(int num)
        {
            for (int i=2; i<=(num/2); i++)
                if ((num%i) == 0)
                 return 0;
            return 1;
        }
    }
    --------------------------------------------------------------------------*/

Log:------->

        ERROR: Could not find class C:\checkPrimeNumber at line 45 column 44.  Please ensure that the
               CLASSPATH is correct.
        ERROR: DATA STEP Component Object failure.  Aborted during the EXECUTION phase.
        java.lang.ClassNotFoundException: C:\checkPrimeNumber
             at java.net.URLClassLoader$1.run(Unknown Source)
             at java.security.AccessController.doPrivileged(Native Method)
             at java.net.URLClassLoader.findClass(Unknown Source)
             at java.lang.ClassLoader.loadClass(Unknown Source)
             at java.lang.ClassLoader.loadClass(Unknown Source)

I have set the CLASSPATH environment variable to C:\Program Files\Java\jdk1.7.0_10\bin and compiled & executed the program from command prompt. Code works fine from command prompt. But SAS code throws an Error, so I added -set CLASSPATHC:\Program Files\Java\jdk1.7.0_10\binstatement insasv9.cfglocated atSASFoundation\9.3\nls\en`. I still get the same error( same as mentioned above). What could be wrong? Where should I add classpath variable in SAS files/folders so that SAS identify java classpath?


In your sasv9.cfg file you should look for -JREOPTIONS option. Inside this option add you path to the parameter -Dsas.app.class.path

When declaring java object in data step you can pass only the class name - not the all path. So the declaration should looks like:

declare javaobj j("checkPrimeNumber")

And the last one. The return value from java method is passed to the last argument in data step function. Calling java method should go like this:

j.callIntMethod("checkPrimeNumbers",i,isPrimeNumber);

Cheers, Chris