Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question follows on from my earlier one, Seeking strategies to deploy a function securely without a front end.

As it pursues a different track to solve the same problem it seemed distinct enough and perhaps interesting enough to others to merit its own question.

I need to deploy a single function into a Windows based technology environment so that the users can call the function from some of their process workflows.

The users don't need a front end or user interface of any kind. In fact a front end would cause problems.

I have some additional code security concerns, but approaches to that have begun to emerge (see the earlier question referenced above). Running without a front end would also keep code from prying eyes.

Question:

Can one launch a Player Pro kernel independently of the front end via something like a Mathematica Script to run the contents of the computable document without displaying the document itself?

References or examples of how to do this appreciated.

share|improve this question
    
I am almost sure that, alas, the answer is no. But since I don't know for sure, this is a comment rather than an answer. –  Leonid Shifrin Jan 12 '13 at 15:13
    
@LeonidShifrin -- Can't blame a guy for hoping. –  Jagra Jan 12 '13 at 15:21
add comment

3 Answers

up vote 5 down vote accepted

Of course this is just an academic technical exercise and cannot be really used due to legal restrictions of the CDF license.

On Linux and Windows with JLink this seems to work easily:

[rolfm@localhost cdffun]$ cat CDFFun.java

    /********************************************************
     * CDFFun.java
     * ************************************************************/

        import com.wolfram.jlink.*;
        public class CDFFun{
                public static void main(String[] argv) {
                        KernelLink ml = null;
                        try {
                                ml = MathLinkFactory.createKernelLink(argv);
           ml.discardAnswer();
                        } catch (MathLinkException e) {
           System.out.println("Fatal error opening link: " + e.getMessage());
                                return;
                        }
                        {
           String version = ml.evaluateToOutputForm("$Version", 0);
               System.out.println("$Version = " + version);
           String instdir = ml.evaluateToOutputForm("$InstallationDirectory", 0);
               System.out.println("$InstallationDirectory= " + instdir);
           String syslen = ml.evaluateToOutputForm("Length[Names[\"System`*\"]]", 0);
           System.out.println("Length[Names[\"System`*\"] = " + syslen);
           String instjava = ml.evaluateToOutputForm("{Needs[\"JLink`\"]; JLink`InstallJava[]}", 0);
           System.out.println("InstallaJava[] = " + instjava);
           String packagesloaded = ml.evaluateToOutputForm("$Packages", 0);
               System.out.println("$Packages = " + packagesloaded);
                        }
                        {
                                ml.close();
                        }
                        }
        }

[rolfm@localhost cdffun]$ cat comp.sh
  #!/bin/bash
  javac -classpath
  .:/usr/local/Wolfram/CDFPlayer/9.0/SystemFiles/Links/JLink/JLink.jar CDFFun.java

[rolfm@localhost cdffun]$ ./comp.sh

[rolfm@localhost cdffun]$ cat run.sh
        #!/bin/bash
        java -classpath
        .:/usr/local/Wolfram/CDFPlayer/9.0/SystemFiles/Links/JLink/JLink.jar
        CDFFun -linkmode launch -linkname
        '/usr/local/Wolfram/CDFPlayer/9.0/Executables/math -mathlink'

[rolfm@localhost cdffun]$ ./run.sh
                $Version = 9.0 for Linux x86 (64-bit) (December 19, 2012)
                $InstallationDirectory= /usr/local/Wolfram/CDFPlayer/9.0
                Length[Names["System`*"] = 4529
                InstallaJava[] =
                {LinkObject['/usr/local/Wolfram/CDFPlayer/9.0/SystemFiles/Java/Linux-x86-64/bin/java'
                -classpath "/usr/local/Wolfram/CDFPlayer/9.0/SystemFiles/Links/JLink/JLink.jar"
                -Xmx256m  -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader
                -Djava.util.prefs.PreferencesFactory=com.wolfram.jlink.DisabledPreferencesFactory
                com.wolfram.jlink.Install -init "/tmp/m000001113431", 51, 2]}
                $Packages = {JLink`, ResourceLocator`, PacletManager`, QuantityUnits`,
            Parallel`NullMaster`, Parallel`, System`, Global`}

        [rolfm@localhost cdffun]$

Windows (with cygwin, starting from the CDF Player/9.0 folder with the same CDFFun.java):

$ SystemFiles/Java/Windows/bin/javac -classpath ".;SystemFiles\Links\JLink\JLink.jar" CDFFun.java

Rolf Mertig@merkur /cygdrive/c/Program Files (x86)/Wolfram Research/Wolfram CDF Player/9.0
$ SystemFiles/Java/Windows/bin/java -classpath ".;SystemFiles\Links\JLink\JLink.jar" CDFFun -linkmode launch -linkname ./MathKernel.exe
    $Version = 9.0 for Microsoft Windows (32-bit) (December 19, 2012)
$InstallationDirectory= C:\Program Files (x86)\Wolfram Research\Wolfram CDF Player\9.0
    Length[Names["System`*"] = 4530
    InstallaJava[] = {LinkObject["C:\Program Files (x86)\Wolfram Research\Wolfram CDF Player\9.0\SystemFiles\Java\Windows\bin\javaw" -classpath "C:\Program Files (x86)\Wolfram Research\Wolfram CDF Player\9.0\SystemFiles\Links\JLink\JLink.jar" -Xmx256m  -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "C:\cygwin\tmp\m-4a0c30df-9187-4b01-bad2-7e04d22230c1", 50, 2]}
    $Packages = {JLink`, ResourceLocator`, PacletManager`, QuantityUnits`, Parallel`NullMaster`, Parallel`, System`, Global`}

share|improve this answer
    
Rolf as per my comment, I was told this was not possible. But that statement was made to me a couple of months back with V8. You have the V9 CDF beta right? –  Mike Honeychurch Jan 22 '13 at 20:26
    
And the website still says it is not possible:wolfram.com/player-pro/how-player-pro-compares.html –  Mike Honeychurch Jan 22 '13 at 21:00
    
@MikeHoneychurch No, just the official 9.0.0 player. –  Rolf Mertig Jan 23 '13 at 0:18
add comment

This may help. I have no experience with Player Pro, but the free CDF Player will load a script if and only if it is Encoded[]; this has to be done by a full version of Mathematica. It is loaded into CDF Player with the call:

Get["filePathToEncodedScript.m"]

The problem is that there is no way to get a result of the operation out of the Player kernel without some sort of frontend. But you can use a Button function in CDF Player which will use the Player frontend to display the result.

For instance this must be created and evaluated in a full version of Mathematica and saved as a .cdf which can later be run by CDF Player,

Button["Press Button to Run", Get["filePathToEncodedScript.m"]]

where the script's final command displays the result of the functions. Since all the code in the script is Encoded[], the user only sees the final result and the code remains secure. After the button code is evaluated, it can be removed from the .cdf file as well, leaving only the button. Some text can be added above the button to describe what the function does. The user presses the button to get the result. The whole thing further can be imbedded in a web page if needed. If the script and the .cdf file are distributed in the same folder, a local file name: encodedScript.m can be used. The code in the button will find a file name in the same folder.

share|improve this answer
add comment

I made a test file "test.m"

Export["test.png",ListPlot[RandomReal[{0,100},{100}]]];

and ran it in the terminal:

/Applications/Mathematica.app/Contents/MacOS/MathKernel -noprompt -run 
"<</Users/mikehoneychurch/Desktop/test.m"

and it worked fine. Then tried with Player Pro

/Applications/Wolfram\ Player\ Pro.app/Contents/MacOS/MathKernel -noprompt -run
 "<</Users/mikehoneychurch/Desktop/test.m"

...and nothing happens. I know from discussions with Wolfram people that it is their expectation that Player Pro and CDF front ends are always to be used rather than allowing the kernel to be used in some other application front end. This was one supposed reason for the exclusion of JLink.

share|improve this answer
    
I still hold out a little bit of hope that someone may have a work around or an undocumented way of doing this, but I think you've properly assessed the situation. Thanks for the help. –  Jagra Jan 14 '13 at 13:15
    
JLink is not excluded. See my answer. –  Rolf Mertig Jan 21 '13 at 22:59
    
@RolfMertig Andre told me in an email that it was! –  Mike Honeychurch Jan 21 '13 at 23:15
    
@MikeHoneychurch strange. It seems to work. –  Rolf Mertig Jan 21 '13 at 23:22
    
@RolfMertig in that case I'm assuming that it is not Wolframs intention that it should work?? –  Mike Honeychurch Jan 21 '13 at 23:29
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.