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."
myAdd::usage = "adds 2 numbers"
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;
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
try
{
ml = MathLinkFactory.createKernelLink(mathLinkArgs);
}
catch (MathLinkException e)
{
System.out.println("Fatal error opening link: " + e.getMessage());
return;
}
try
{
//discard initial InputNamePacket
ml.discardAnswer();
System.out.println("Getting Primality Proving...");
ml.evaluate("<< \"PrimalityProving`\"");
ml.discardAnswer();
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`\"");
ml.discardAnswer();
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");
}
catch (MathLinkException e)
{
System.out.println("MathLinkException occurred: " + e.getMessage());
}
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?