# Using a user defined Mathematica package with MathLink

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)

BeginPackage["test"]

test::usage = "My test package."
myPrint::usage = "prints 'print this out'"

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

EndPackage[]


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

package mathlinktest;

public static void main(String[] args) {

try
{
}
{
System.out.println("Fatal error opening link: " + e.getMessage());
return;
}

try
{

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

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

}
{
}
finally {
ml.close();
}

}
}


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):
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

myPrint[]

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?

-
Related question –  Leonid Shifrin Jul 2 '12 at 23:53
• 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).