Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
JAR resource not found when calling Java from Matlab

Subject: JAR resource not found when calling Java from Matlab

From: Kaspar

Date: 27 Aug, 2009 13:55:18

Message: 1 of 4

Hi all

I was trying to construct a Java object in Matlab after making Matlab aware of the JAR file containing the class using javaaddpath(). That works so far. However, the Java code wants to access a resource file that also resides in this JAR using ClassLoader.getSystemResourceAsStream(fileName). When running the JAR from the command line, this works well, but not if run within Matlab.

After investigating a bit, I printed the runtime classpath when the JAR is called using System.getProperty("java.class.path"). Strangely, only Matlab JARs are listed in the runtime classpath, and the JAR file I added with javaaddpath() is missing! This means that apparently the JVM running in Matlab doesn't even "properly" know about the JAR and where to look for resources.

Any ideas on why the resource cannot be found and how to fix this?

Kind regards
Kaspar

Subject: JAR resource not found when calling Java from Matlab

From: Donn Shull

Date: 27 Aug, 2009 14:20:18

Message: 2 of 4

"Kaspar " <kaspar.thommen.remove.this@ubs.com> wrote in message <h76386$675$1@fred.mathworks.com>...
> Hi all
>
> I was trying to construct a Java object in Matlab after making Matlab aware of the JAR file containing the class using javaaddpath(). That works so far. However, the Java code wants to access a resource file that also resides in this JAR using ClassLoader.getSystemResourceAsStream(fileName). When running the JAR from the command line, this works well, but not if run within Matlab.
>
> After investigating a bit, I printed the runtime classpath when the JAR is called using System.getProperty("java.class.path"). Strangely, only Matlab JARs are listed in the runtime classpath, and the JAR file I added with javaaddpath() is missing! This means that apparently the JVM running in Matlab doesn't even "properly" know about the JAR and where to look for resources.
>
> Any ideas on why the resource cannot be found and how to fix this?
>
> Kind regards
> Kaspar


Hi Kaspar,

Instead of using javaaddpath, edit classpath.txt and add your jar file to the end. I have seen other cases where it is necessary to use the static classpath rather than the dynamic classpath to get things to work.

Good Luck,

Donn

Subject: JAR resource not found when calling Java from Matlab

From: Kaspar

Date: 27 Aug, 2009 14:38:02

Message: 3 of 4

Donn,

Thanks for your suggestion - that's exactly what I tried, and it works! Still strange, though... I would really prefer to add the path dynamically.

Anyone else has an idea on this issue?

Kaspar


"Donn Shull" <donn.shull.no_spam@aetoolbox.com> wrote in message <h764n2$e9f$1@fred.mathworks.com>...
> "Kaspar " <kaspar.thommen.remove.this@ubs.com> wrote in message <h76386$675$1@fred.mathworks.com>...
> > Hi all
> >
> > I was trying to construct a Java object in Matlab after making Matlab aware of the JAR file containing the class using javaaddpath(). That works so far. However, the Java code wants to access a resource file that also resides in this JAR using ClassLoader.getSystemResourceAsStream(fileName). When running the JAR from the command line, this works well, but not if run within Matlab.
> >
> > After investigating a bit, I printed the runtime classpath when the JAR is called using System.getProperty("java.class.path"). Strangely, only Matlab JARs are listed in the runtime classpath, and the JAR file I added with javaaddpath() is missing! This means that apparently the JVM running in Matlab doesn't even "properly" know about the JAR and where to look for resources.
> >
> > Any ideas on why the resource cannot be found and how to fix this?
> >
> > Kind regards
> > Kaspar
>
>
> Hi Kaspar,
>
> Instead of using javaaddpath, edit classpath.txt and add your jar file to the end. I have seen other cases where it is necessary to use the static classpath rather than the dynamic classpath to get things to work.
>
> Good Luck,
>
> Donn

Subject: JAR resource not found when calling Java from Matlab

From: Yair Altman

Date: 27 Aug, 2009 21:34:03

Message: 4 of 4

"Kaspar " <kaspar.thommen.remove.this@ubs.com> wrote...
> Thanks for your suggestion - that's exactly what I tried, and it works! Still strange, though... I would really prefer to add the path dynamically.


We all would... This has been an ongoing nuisance with the Java classloader for a long time. MathWorks claims this is not a bug but an expected behavior (see here for example: http://www.mathworks.com/support/solutions/en/data/1-1YFUFB/ ). While it may be the expected behavior with the current classloader, I think it should be modified. Anyway, until the classloader is modified in some future Matlab release, that's apparently the way things are...

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us