HoriAsso - Documentation

From Drorbn
Jump to navigationJump to search

In this project, we will be mainly using Commands in Mathematica to access Java programs that we write. Here is how to set up the system:

How to set up a system for using Java in Mathematica

How to install Mathematica, J/Link, and Java

  • Jlink is already completely installed in Mathematica 4.2 and later. If you are using earlier versions of Mathematica, you can install JLink following instructions on the official Jlink Setup Documentation.
  • Download JRE 5.0 Update 6 (or the latest version) from Sun and install it following instructions from the official Setup Documentation.

How to start using Jlink in a Mathematica notebook

  • Write the Java source code (.java files) and compile it using the command "javac file.java." Place the compiled files (.class files) in the directory you want. (I have not tried to make .jar files yet so I will not write about it here)
  • Open a Mathematica notebook, install Jlink by writing:
Needs["JLink`"] 
  • Install Java by writing:
InstallJava[] 

or to use a specific java runtime if you have more than one installed on your computer, write:

InstallJava[commandLine->"/wherever/java/is"] in Linux
InstallJava[CommandLine -> "d:\\\\path\\to\\java.exe"] in Windows. 
  • Add the directory which contains the compiled java (.class) files to the class path, ie, where Java will look for class files, by writing

AddToClassPath["wherever/my/class/files/are"] in Linux AddToClassPath["c:\\my\\java\\dir"] in Windows


  • Load the Java classes by writing

LoadJavaClass["Classname1"] LoadJavaClass["Classname2"]


  • Create New Java Objects by writing

JavaNew["classname1",constructorArg1,constructorArg2...]


  • Access the non-static methods and variables of any object the same way you would in Java except replace "." (java) by "@" (Mathematica) and "()" (Java) by "[]" (Mathematica). Access static methods or variables by replacing "." in Java by "`" in Mathematica. (I haven't used this yet so I am not entirely sure.)
  • For detailed documentation on JLink, please consult the Jlink manual in the Mathematica Help Browser.