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:
Error when running Java the first time after starting Matlab

Subject: Error when running Java the first time after starting Matlab

From: Camron

Date: 22 Dec, 2011 00:04:09

Message: 1 of 3

I have a m-file script that I use to add some *.jar files to my dynamic java path. It then imports some classes and tries to use them. The first time I run my application when having just started Matlab, I get an error "Undefined function or variable 'MyClass' ". This error does not occur if I run my app any number of times later. Everything works perfectly when I close my GUI and simply run it again.

I believe that this has to do with the asynchronous nature of Matlab and Java, but I don't know what to do so that my users will not have to run my app twice to get it to work.

Any thoughts?

Subject: Error when running Java the first time after starting Matlab

From: Camron

Date: 22 Dec, 2011 01:23:08

Message: 2 of 3

Update:

A code snip and some things I have tried.

% simplified example code to explain what I am doing
function myDemo
    pathtojars = {'C:\Users\Camron\Documents\Jars\myjavacode.jar'};

    javaaddpath(pathtojars);

    import com.mypackage.MyClass;

    myClass = MyClass();

end

when running the first time it produces
>> ??? Undefined function or variable 'MyClass'.

I have tried pausing after javaaddpath, and after importing which doesn't work.

I have tried using "clear java" after javaaddpath and also evalin('base','clear java'); after javaaddpath.

reading
http://www.mathworks.com/matlabcentral/newsreader/view_thread/314610#859318
talks about refreshing matlab after editing dynamic path using clear java, but it seems not to work for me.

Running the script the second time produces no errors.

Subject: Error when running Java the first time after starting Matlab

From: Yair Altman

Date: 16 Mar, 2012 11:15:22

Message: 3 of 3

"Camron" wrote in message <jcu0pr$iuu$1@newscl01ah.mathworks.com>...
> Update:
>
> A code snip and some things I have tried.
>
> % simplified example code to explain what I am doing
> function myDemo
> pathtojars = {'C:\Users\Camron\Documents\Jars\myjavacode.jar'};
>
> javaaddpath(pathtojars);
>
> import com.mypackage.MyClass;
>
> myClass = MyClass();
>
> end
>
> when running the first time it produces
> >> ??? Undefined function or variable 'MyClass'.
>
> I have tried pausing after javaaddpath, and after importing which doesn't work.
>
> I have tried using "clear java" after javaaddpath and also evalin('base','clear java'); after javaaddpath.
>
> reading
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/314610#859318
> talks about refreshing matlab after editing dynamic path using clear java, but it seems not to work for me.
>
> Running the script the second time produces no errors.


Perhaps your JAR file depends on some other class or JAR that is not yet loaded in Matlab the first time that you run your program. Later on in your program the dependent JAR file or class is loaded and from then on the depending class (MyClass) can be used).

Matlab's error message is not just cryptic and unhelpful, but actually misleading. I've reported this to MathWorks a long time ago as a potential enhancement, but this has not yet been fixed. I plan to post an article about the different such pitfalls when loading Java classes in Matlab in the upcoming weeks, so keep an eye on my blog.

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