I have a problem that is driving me nuts. Matlab sees only some of my classes embeded in a JAR file
If I compile the classes outside of a package and add the path to
Another reason MATLAB will not see a class is when you compile for JRE7 and are using MATLAB 2012b (probably applies to other MATLAB releases).
The symtom is the extremly lame error
The class "JavaNuServer" is undefined.
Perhaps Java is not running.
Solution:
Compile with the javac flags
-source 1.6 -target 1.6
I eventually found the problem which cannot be seen above. The problem (not documented anywhere) was that some of my classes use external packages that I had not imported into Matlab. I was not planning on using any of the functionalities linked with these packages.
Nevertheless, the Matlab error message that it cannot find the class is puzzling. An error message indicating that the class cannot be used because some packages are not referenced would be most useful.
If your package uses external packages, make sure to include all of the relevant jar files in the java classpath or Matlab will not see your dependent classes.
Another tip that I found useful is that the Matlab function "import" will not return an error if you enter a package that does not exist, e.g., import java.doesnotexist.*
works fine. However, import java.doesnotexist.aclass
will not work.
Jason
One more reason why this is happening is the need of some classes to be in the static classpath of Matlab.
In this case, one of my java classes worked perfectly in Matlab2017b after adding more jars to the dynamic classpath via javaaddpath as suggested above. That prevented the java.lang.ClassNotFoundException from happening.
The same approach failed in Matlab2019a.
Now I figured that my problem is solved by simply adding my 'uber' jar containing all the jars my application needs to the classpath.txt file, like so:
# DO NOT MODIFY THIS FILE. IT IS AN AUTOGENERATED FILE.
/Users/andyhueni/Desktop/SPECCHIO_new_build2/SPECCHIO.app/Contents/Java/specchio-client.jar
$matlabroot/java/patch
$matlabroot/java/jar/jmi.jar
...
The location within the classpath.txt (top or end of the file) appears to be of no importance.
Modifying the classpath.txt may not be possible for all users, but there are work-arounds as listed on this excellent source of information: https://undocumentedmatlab.com/blog/static-java-classpath-hacks
This same problem can be caused by multiple types of failures. (All of them frustrating since you get NO information about what failed!) The accepted answer describes one reason and the solution. The answer by Wolfgang Kuehn gets at another possible issue, although I had trouble understanding the point so I decided to write my own answer expanding on it slightly more generally:
Each version of Matlab ships with some specific JRE that's run and supported. Different version of Matlab ship with different version of the JRE. Not matter which version of Matlab and associated JRE you have though, if you complied your external Java classes that are in the jar file with a higher (and incompatible) version of the Java compiler, Matlab will refuse to acknowledge classes even though they are in the jar. If you're lucky, you can fix this by recompiling the Java with a flag specifying compatibility with the version of Java in your instance of Matlab.
To find out which version of Java your Matlab is running, use this command at the Matlab command prompt:
version -java
I encountered the same problem just now. The solution is to update matlab java version by setting a new envir parameter mentioned here
https://cn.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-on-windows
Matlab sucks at error message
To complement the accepted answer - there's a far better way to check if java class was loaded propertly, than through import
command. Use whereisjavaclassloadingfrom
function, provided by Andrew Janke in this answer.
Not only it will tell you whether the class is loaded, it will list all jars that this class is available in. It happens quite often that your class gets masked / overshadowed by another class with the same package/name, located in a different jar. That can happen e.g. if you try to use a newer library than one of those shipped with Matlab, and do not place it above the original one on the static java classpath.