I'm trying to create a Java program that uses some methods from a mathematica package I've defined. When I run my java program though the output of the program acts as though the Mathematica methods were never defined. I believe my problem has to do with the way that i'm trying to include the Mathematica package in my java code.

Here is the mathematica package (saved as test.m)


test::usage = "My test package."
myAdd::usage = "adds 2 numbers"
myPrint::usage = "prints 'print this out'"

myAdd[x_, y_] := x + y
myPrint[] := "print this out"


Here is my Java program that tries to use the two methods (myAdd and myPrint) defined in this package.

package mathlinktest;

import com.wolfram.jlink.*;

public class MathlinkTest {

    public static void main(String[] args) {

    KernelLink ml;
    String[] mathLinkArgs = {"-linkmode", "launch", "-linkname", "C:\\Program Files\\Wolfram Research\\Mathematica\\8.0\\mathkernel"};

    //create the mathlink
        ml = MathLinkFactory.createKernelLink(mathLinkArgs);
    catch (MathLinkException e) 
        System.out.println("Fatal error opening link: " + e.getMessage());

            //discard initial InputNamePacket

            System.out.println("Getting Primality Proving...");
            ml.evaluate("<< \"PrimalityProving`\"");

            System.out.println("Result of ProvablePrimeQ[1093] (should be true):");
            System.out.println(ml.evaluateToOutputForm("ProvablePrimeQ[1093]",0) + "\n");

            System.out.println("Set Directory...");
            System.out.println(ml.evaluateToOutputForm("SetDirectory[\"C:\\\\Users\\\\cas120030\\\\Dropbox\\\\Acoustic Metamaterials\\\\Cory and Vivek Tinkering\\\\ImportFilePathTest\\\\src\\\\importfilepathtest\"]",0) + "\n");

            System.out.println("Get Test...");
            ml.evaluate("<< \"test`\"");

            System.out.println("Result of 1+1 using Plus[1,1]: " + ml.evaluateToOutputForm("Plus[1,1]", 0) + "\n");

            System.out.println(ml.evaluateToOutputForm("myPrint[]", 0) + "\n");

            System.out.println("testFuntion output: 1 + 2 = " + ml.evaluateToOutputForm("myAdd[1, 2]", 0) + "\n");   

        catch (MathLinkException e) 
            System.out.println("MathLinkException occurred: " + e.getMessage());
        finally {


The File paths are correct as far as I know (they go to the right directories for the files and they are formatted correctly with the correct number of escape characters).

When the program runs I get this as the output:

Getting Primality Proving...
Result of ProvablePrimeQ[1093] (should be true):

Set Directory...
C:\Users\cas120030\Dropbox\Acoustic Metamaterials\Cory and Vivek Tinkering\ImportFilePathTest\src\importfilepathtest

Get Test...

Result of 1+1 using Plus[1,1]: 2


testFuntion output: 1 + 2 = myAdd[1, 2]

As you can see, the method calls to myAdd and myPrint are just printed out as if they were never defined in the mathematica. I thought this meant that I had defined them wrong or was using them incorrectly but I made a Mathematica notebook that was able to import and use both methods just fine using the same mathematica calls as I used here. My next thought was that importing packages was different in MathLink than with in a notebook so I included that section in the code that imports the Primality Proving package of mathematica and tests a function out of it.

For reference I am using java 1.6 and Mathematica 8 on a machine running Windows 7. Sorry for the mountain of text, I wanted to give as thorough idea of what was going on as I could. Anyone have a clue what's up?


1 Answer 1


A few ideas:

  • If it's a package that is to be used regularly, I'd put it in one of the standard locations where M searches for packages, such as e.g. $UserBaseDirectory/Applications.
  • SetDirectory doesn't affect the search locations for packages. SetDirectory sets what's called the "working directory", while Get searches only those locations which are currently in the $Path variable. Or you could use Get with an explicit Path option.
  • In the package itself, you can either omit the sub-context or use `Private`, but not another test`, which is rather meaningless. So, you could replace Begin["test`"] with Begin["`Private`"].
  • If you use a non-standard location for a user-written package, I'd use Get with a path/file name, not with a package context.
  • You can check if a package is executed by creating a visible side-effect, such as including JLink as subordinate package, as in BeginPackage["test`","JLink`"], and then use a line like ShowJavaConsole[] anywhere in the package. If the Java console pops up, your package was found at least started. If the Java console does not pop up, your package was not found. This can be useful when you don't have a M f/e, and you can't check with the M f/e if the definitions in your package were executed. (here, Java is your f/e).
  • $\begingroup$ Nice trick with the Java console, +1. $\endgroup$ Jul 2, 2012 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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