Installation and Setup for using Java in Mathematica

From Drorbn
Jump to navigationJump to search

How to set up a system for using Java in Mathematica

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 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 the official instructions.
  • Download JRE 5.0 Update 6 (latest as of June 2006) the latest version from Sun and install it following the official official instructions.

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)

  • For detailed documentation on JLink, please consult the Jlink manual in the Mathematica Help Browser. The following are extracted from there.
  • Open a Mathematica notebook, install JLink by (writing and executing):
Needs["JLink`"] 
  • Install Java by:
InstallJava[] 

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

InstallJava[commandLine->"/wherever/java/is"] in Linux
InstallJava[CommandLine -> "d:\\\\path\\to\\javaw.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
AddToClassPath["wherever/my/class/files/are"] in Linux
AddToClassPath["c:\\my\\java\\dir"] in Windows
  • Create New Java Objects by
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 the static methods yet so I am not entirely sure.)