MATLAB Answers

How do I call a simple "Hello World" Java program in MATLAB?

212 views (last 30 days)
I have a simple Java program, and I would like to know how to call it in MATLAB.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Apr 2020
Edited: MathWorks Support Team on 2 Apr 2020
Here is an example. This assumes you already have knowledge of Java.
The following is a Java program:
public class HelloWorld {
public static void main( String args[] )
{
System.out.println( "Hello World!" );
}
}
To call this program in MATLAB:
0. Make sure you have the version of JRE/JDK on your system that is supported with your version of MATLAB. You can get this version by executing this on the MATLAB command prompt:
>> version -java
1. Outside of MATLAB: use Java to compile this class, so you have file HelloWorld.class
2. Locate the classpath.txt file for the MATLAB installation. The location of this file can be found by typing the following command in MATLAB command window:
which classpath.txt
3. Open the 'classpath.txt' with a text editor as Administrator. Add the full path for the directory with the HelloWorld.class to the end of the 'classpath.txt' file as a single line and save the file.
4. Restart MATLAB.
5. In MATLAB: to create an object of class HelloWorld, type:
o = HelloWorld
6. In MATLAB: to execute main() of object o, type:
javaMethod('main', o, '')
Alternately one may also add the directory in which the class files are to the dynamic path. Use the JAVAADDPATH command to add the directory (which contains the HelloWorld.class file) to JAVA's dynamic classpath. This also obviates the need to restart MATLAB. Once this is done the code can be invoked as follows:
o = HelloWorld;
javaMethod('main', o);
For more information on calling Java class from MATLAB, see chapter 3 of the "External Interfaces" documentation. This documentation can be accessed from the following URL:
  3 Comments
Ruixin Zhou
Ruixin Zhou on 4 Jul 2019
Alternative to step 2 and 3:
If you don't have Admin rights, create a javaclasspath.txt in the preferences folder (located at prefdir) then add those desired paths in it.
Check out the link below:

Sign in to comment.

More Answers (4)

sai m
sai m on 24 Feb 2015
step 5 is notworking ??? Undefined function or variable 'Helloworld'. please help me
  6 Comments
Mohammad Reza
Mohammad Reza on 27 Jul 2015
I had the same issue "Undefined function or variable 'HelloWorld'" until I figured out the version of java in matlab and the one used to compile the HelloWorld.java program must be identical.
I have the latest java installed on my machine but Matlab seems to have its own java installation. But it is possible to "tell" matlab to use my version of java by setting an environment variable. So I start matlab as follows:
c:> set MATLAB_JAVA=C:\Program Files\Java\jre1.8.0_45 c:> "C:\Program Files\MATLAB\R2014a\bin\matlab.exe"
jre1.8.0_45 is the one used to compile the HellowWorld.java
And now HelloWorld is reconised as an Object and you can run the following to get the Hello World message
>> o.main('')

Sign in to comment.


Akshay Jain
Akshay Jain on 17 Jun 2016
I am trying to call the HelloWorld code listed above from Matlab 2015b, but am unable to do this. I am using eclipse to write and compile the java code on Windows 10. After compiling the code, I have the HelloWorld.class file in /my_package/bin/my_package/ folder. I have tried the following:
- Changed the MATLAB_JAVA environment variable to the java version used in eclipse as described at http://www.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows. Verified the version update by using java -version command. Tried adding the .class folder location both by static and dynamic method. However, when I run [M,X,J] = inmem command, my java class does not show in the J variable. As a result, when I run o = HelloWorld command, matlab says: Undefined function or variable 'HelloWorld'.
- Also tried compiling the Java code with javaSE-1.7 (the default java version in matlab is Java 1.7.0_60-b19) and removing MATLAB_JAVA environment variable. Still the same problem.
I eventually want to call an opensource library written in Java. But for now, I would be happy if I can at least call the HelloWorld class from Matlab.
Thanks in advance, Akshay
  1 Comment
Akshay Jain
Akshay Jain on 23 Jun 2016
It was a very simple fix. I was writing the java class as part of the package, but trying to access it as if it wasn't. Earlier, I was using:
javaaddclasspath('/my_package/bin/my_package')
and I changed it to:
javaaddclasspath('/my_package/bin')
and then called the class using o = my_package.my_class
and it worked perfectly.
Akshay

Sign in to comment.


Supriyo Srimani
Supriyo Srimani on 7 Jan 2017
This program just prints Hello world...if i have some logic when user give a input in java could it be executed in matlab??? Please give a example like adding two numbers...
  2 Comments
Mehmet Mahmudoglu
Mehmet Mahmudoglu on 12 Dec 2018
Addnumber Class must be defined with package otherwise it is not working
>> obj=numberpack.Addnumber
package numberpack;
public class Addnumber {
public double addtwonum( double a1,double a2)
{
return a1+a2;
}
}

Sign in to comment.


Vanessa King
Vanessa King on 12 Jun 2018
Are we still meant to add the directory to classpath.txt in R2018a? Classpath.txt explicitly says to not modify it as it is autogenerated. Is it adequate to just use the javaaddpath method to add it to the dynamic Java class path?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!