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:
javaaddpath clears workspace variables

Subject: javaaddpath clears workspace variables

From: Dan Gresh

Date: 7 Feb, 2008 17:47:02

Message: 1 of 14

Hi,

On both matlab 2007a and 2007b with Java JARs of source
1.5.0_07, when I use javaaddpath it will sometimes clear all
my workspace variables. Sometimes it does not clear any
variables; sometimes it clears all.

This is a significant issue because not all users can add
the .jars to their static path. Is there any way to stop
this from happening?

Thanks,
Dan

Subject: javaaddpath clears workspace variables

From: Yair Altman

Date: 7 Feb, 2008 20:59:01

Message: 2 of 14

"Dan Gresh" <dgresh@lle.rochester.edu> wrote in message
<fofg6m$gta$1@fred.mathworks.com>...
> Hi,
>
> On both matlab 2007a and 2007b with Java JARs of source
> 1.5.0_07, when I use javaaddpath it will sometimes clear all
> my workspace variables. Sometimes it does not clear any
> variables; sometimes it clears all.
>
> This is a significant issue because not all users can add
> the .jars to their static path. Is there any way to stop
> this from happening?
>
> Thanks,
> Dan


2007a (and earlier) had a nasty undocumented side-effect
that "clear java" cleared all global variables (strangely,
not local vars). Since javaaddpath used this command, it
also had the same side-effect. I think this was solved in
2007b (not 100% sure).

Yair Altman
http;//ymasoftware.com

Subject: javaaddpath clears workspace variables

From: Dan Gresh

Date: 22 Feb, 2008 21:12:01

Message: 3 of 14

Hi,

Thanks for the reply. This definitely seems to be the case.
Furthermore, I just upgraded to MATLAB 7.5.0 (2007b) and the
problem still persists.

Thank you for the informative reply.
Dan

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message
<fofrel$ng9$1@fred.mathworks.com>...
>
>
> 2007a (and earlier) had a nasty undocumented side-effect
> that "clear java" cleared all global variables (strangely,
> not local vars). Since javaaddpath used this command, it
> also had the same side-effect. I think this was solved in
> 2007b (not 100% sure).
>
> Yair Altman
> http;//ymasoftware.com
>

Subject: javaaddpath clears workspace variables

From: Shirley Zheng

Date: 15 Apr, 2011 14:36:04

Message: 4 of 14

I tried 2010a and 2010b, the javaaddpath still clear all variables, it is really nasty! I didn't see this issue is fixed? anybody knows this?

Shirley


"Yair Altman" wrote in message <fofrel$ng9$1@fred.mathworks.com>...
> "Dan Gresh" <dgresh@lle.rochester.edu> wrote in message
> <fofg6m$gta$1@fred.mathworks.com>...
> > Hi,
> >
> > On both matlab 2007a and 2007b with Java JARs of source
> > 1.5.0_07, when I use javaaddpath it will sometimes clear all
> > my workspace variables. Sometimes it does not clear any
> > variables; sometimes it clears all.
> >
> > This is a significant issue because not all users can add
> > the .jars to their static path. Is there any way to stop
> > this from happening?
> >
> > Thanks,
> > Dan
>
>
> 2007a (and earlier) had a nasty undocumented side-effect
> that "clear java" cleared all global variables (strangely,
> not local vars). Since javaaddpath used this command, it
> also had the same side-effect. I think this was solved in
> 2007b (not 100% sure).
>
> Yair Altman
> http;//ymasoftware.com
>

Subject: javaaddpath clears workspace variables

From: Dmitry

Date: 3 May, 2011 19:47:03

Message: 5 of 14

Problem persists in Matlab 2011a, on Win 7 x64, either. For me, it always clears global variables. Very annoying.

And in Matlab documentation it is wrongly put that it issues "clear java". Absolutely not!

Subject: javaaddpath clears workspace variables

From: ImageAnalyst

Date: 3 May, 2011 21:41:08

Message: 6 of 14

On May 3, 3:47 pm, "Dmitry " <vasil...@mit.edu> wrote:
> Problem persists in Matlab 2011a, on Win 7 x64, either. For me, it always clears global variables. Very annoying.
>
> And in Matlab documentation it is wrongly put that it issues "clear java". Absolutely not!

----------------------------------------------------------------------------------------------------
I've found that also. When I issue javaclasspath(), it blows away my
global variables. So I put this note in my code (where I used to have
it):

% IMPORTANT NOTE: IF YOU HAVE JAVACLASSPATH() IN HERE, IT WILL BLOW
AWAY
% ANY GLOBAL VARIABLES YOU HAVE IN YOUR CALLING ROUTINE.
% INSTEAD, ADD THE PATH AS A LINE TO THE FILE:
% C:/Program Files/MATLAB/R2011a/toolbox/local/classpath.txt
% javaclasspath('C:/Program Files/MATLAB/Apache Commons/commons-
configuration-1.6/commons-configuration-1.6.jar');

and I put the path I wanted to add into the "C:\Program Files\MATLAB
\R2011a\toolbox\local\classpath.txt" file. If you put it there
instead of in your program then it will still have the path defined
but it won't blow away your global variables in your program.
-ImageAnalyst

Subject: javaaddpath clears workspace variables

From: Steven_Lord

Date: 4 May, 2011 14:34:33

Message: 7 of 14



"Dmitry " <vasilyev@mit.edu> wrote in message
news:ippm3n$6bl$1@fred.mathworks.com...
> Problem persists in Matlab 2011a, on Win 7 x64, either. For me, it always
> clears global variables. Very annoying.
> And in Matlab documentation it is wrongly put that it issues "clear java".
> Absolutely not!

Your statement "Absolutely not!" is incorrect. JAVAADDDPATH does in fact
execute "clear java" when called (well, it doesn't directly, but it calls
something that does.) As far as I'm aware this is intentional, so as to pick
up modified class definitions for Java classes that were added to the Java
dynamic class path. See the last few paragraphs in the Examples section of
the JAVAADDPATH reference page.

http://www.mathworks.com/help/techdoc/ref/javaclasspath.html

As documented on the reference page for CLEAR, "clear java" calls "clear
all" which clears global variables.

This is NOT a bug. You could contact Technical Support and ask them to enter
an enhancement request asking for a way to add classes to the dynamic class
path without clearing Java.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: javaaddpath clears workspace variables

From: Yair Altman

Date: 4 May, 2011 18:59:07

Message: 8 of 14

"Steven_Lord" <slord@mathworks.com> wrote in message ...
> This is NOT a bug. You could contact Technical Support and ask them to enter
> an enhancement request asking for a way to add classes to the dynamic class
> path without clearing Java.


Dear Steve - I've already reported this to Tech Support quite a few years ago and asked for their help in fixing this. Apparently this is not high on the MathWorks' priorities list...

Yair Altman
http://UndocumentedMatlab.com

Subject: javaaddpath clears workspace variables

From: Mark

Date: 12 Sep, 2011 17:40:31

Message: 9 of 14

I know this is an old thread, but I feel compelled to add my two cents. As a preface, I'm a software professional with 30 years experience who does a lot of commercial quality programming in MATLAB... so I'm not a noob.

Documenting that javaaddpath clears global variables somewhere doesn't then make it "NOT a bug" because it is a known behavior. Clearing the workspace makes this function pretty much useless, particularly for those using GUIDE to produce user interfaces. When deploying my application to clients, rather than being able to simply use javaaddpath in my code to establish where my Java is... I must instead hand-edit my clients classpath.txt files on every machine. This is a real pain and dilutes the impression of "quality" my code makes on my clients.

Rant over.

Now off to edit classpath.txt so I can move forward.

Mark

Subject: javaaddpath clears workspace variables

From: bor

Date: 23 Jul, 2012 16:59:16

Message: 10 of 14

"Mark " <mark.neil@kla-tencor.com> wrote in message <j4lg6f$2oj$1@newscl01ah.mathworks.com>...
> I know this is an old thread, but I feel compelled to add my two cents. As a preface, I'm a software professional with 30 years experience who does a lot of commercial quality programming in MATLAB... so I'm not a noob.
>
> Documenting that javaaddpath clears global variables somewhere doesn't then make it "NOT a bug" because it is a known behavior. Clearing the workspace makes this function pretty much useless, particularly for those using GUIDE to produce user interfaces. When deploying my application to clients, rather than being able to simply use javaaddpath in my code to establish where my Java is... I must instead hand-edit my clients classpath.txt files on every machine. This is a real pain and dilutes the impression of "quality" my code makes on my clients.
>
> Rant over.
>
> Now off to edit classpath.txt so I can move forward.
>
> Mark

I 100% agree. I've been really disappointed with MatLab GUIs. It really is a shame because matlab is great in many ways but really not for application development.
And big thanks to yair altman for his blog; it makes matlab GUI programming a lot less horrible.

Subject: javaaddpath clears workspace variables

From: Ryan Wong

Date: 16 Nov, 2012 03:29:13

Message: 11 of 14

This is a summary of the workarounds.

(1) To just get rid of the issue, edit the following file to add the classpath there. (Remember to replace the MATLAB version in the path.)

"C:\Program Files\MATLAB\R2011a\toolbox\local\classpath.txt"

(2) If unfortunately this happens in a large code base and that debugging is not possible (due to the side effect that "clear java", "clear all" also clears all breakpoints), then do this in a command line:

dbstop in clear

This will give a warning, which is okay. This command line allows a troubleshooter to identify the lines of culprits by executing the offending package code.

Thanks,
Ryan Wong

"Dan Gresh" wrote in message <fofg6m$gta$1@fred.mathworks.com>...
> Hi,
>
> On both matlab 2007a and 2007b with Java JARs of source
> 1.5.0_07, when I use javaaddpath it will sometimes clear all
> my workspace variables. Sometimes it does not clear any
> variables; sometimes it clears all.
>
> This is a significant issue because not all users can add
> the .jars to their static path. Is there any way to stop
> this from happening?
>
> Thanks,
> Dan

Subject: javaaddpath clears workspace variables

From: Alexander

Date: 2 Oct, 2013 15:58:05

Message: 12 of 14

Another workaround for this is to assign your global variables to a local function variable, and then assign it back after the call to javaclasspath/javaaddpath, as local function variables do not seems to be cleared by the call.


"Ryan Wong" <rwong_002@hotmail.com> wrote in message <k84bu9$q6b$1@newscl01ah.mathworks.com>...
> This is a summary of the workarounds.
>
> (1) To just get rid of the issue, edit the following file to add the classpath there. (Remember to replace the MATLAB version in the path.)
>
> "C:\Program Files\MATLAB\R2011a\toolbox\local\classpath.txt"
>
> (2) If unfortunately this happens in a large code base and that debugging is not possible (due to the side effect that "clear java", "clear all" also clears all breakpoints), then do this in a command line:
>
> dbstop in clear
>
> This will give a warning, which is okay. This command line allows a troubleshooter to identify the lines of culprits by executing the offending package code.
>
> Thanks,
> Ryan Wong
>
> "Dan Gresh" wrote in message <fofg6m$gta$1@fred.mathworks.com>...
> > Hi,
> >
> > On both matlab 2007a and 2007b with Java JARs of source
> > 1.5.0_07, when I use javaaddpath it will sometimes clear all
> > my workspace variables. Sometimes it does not clear any
> > variables; sometimes it clears all.
> >
> > This is a significant issue because not all users can add
> > the .jars to their static path. Is there any way to stop
> > this from happening?
> >
> > Thanks,
> > Dan

Subject: javaaddpath clears workspace variables

From: clau st

Date: 18 Jun, 2014 16:07:09

Message: 13 of 14

It seems that in GUI you can't load a java class in 1 function then use the class in another, or so it seems for my Matlab 2013b, or I'm missing something.
If I don't import the java class package in the function where I have calls to java custom classes, then the constructors won't load or something... would show that there is no function for my data type.
So for handling custom java classes from Matlab GUI I've found the following workaround:
function ...
global global_variables_X;
local_variables_X=global_variables_X; % save your globals to local space
javaaddpath /java/;
import java_class_package.*; % also I get errors when I load only one class at a time, instead of the whole package.
global variables_X;
global_variables_X=local_variables_X; % resave your variables to global space
insert calls to custom java classes..


"Alexander" wrote in message <l2hfqd$m7u$1@newscl01ah.mathworks.com>...
> Another workaround for this is to assign your global variables to a local function variable, and then assign it back after the call to javaclasspath/javaaddpath, as local function variables do not seems to be cleared by the call.
>
>
> "Ryan Wong" <rwong_002@hotmail.com> wrote in message <k84bu9$q6b$1@newscl01ah.mathworks.com>...
> > This is a summary of the workarounds.
> >
> > (1) To just get rid of the issue, edit the following file to add the classpath there. (Remember to replace the MATLAB version in the path.)
> >
> > "C:\Program Files\MATLAB\R2011a\toolbox\local\classpath.txt"
> >
> > (2) If unfortunately this happens in a large code base and that debugging is not possible (due to the side effect that "clear java", "clear all" also clears all breakpoints), then do this in a command line:
> >
> > dbstop in clear
> >
> > This will give a warning, which is okay. This command line allows a troubleshooter to identify the lines of culprits by executing the offending package code.
> >
> > Thanks,
> > Ryan Wong
> >
> > "Dan Gresh" wrote in message <fofg6m$gta$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > On both matlab 2007a and 2007b with Java JARs of source
> > > 1.5.0_07, when I use javaaddpath it will sometimes clear all
> > > my workspace variables. Sometimes it does not clear any
> > > variables; sometimes it clears all.
> > >
> > > This is a significant issue because not all users can add
> > > the .jars to their static path. Is there any way to stop
> > > this from happening?
> > >
> > > Thanks,
> > > Dan

Subject: javaaddpath clears workspace variables

From: Paul Sexton

Date: 18 Nov, 2014 23:08:06

Message: 14 of 14

I ran into a brick wall this month because of this bug as well. Six years have elapsed since this thread started, and we're still forced to go to incredible lengths to actually use Java in Matlab, with Mathworks either unwilling or unable to fix it.

Additionally, I've discovered that even if you try to be clever, and only access your Java objects through static methods, so that none of your classes show up in the results from "[~, ~, C] = inmem", those objects still get cleared. You won't get any warnings about objects still existing, and addjavapath won't crash on you, but it will still wreck havoc on your java environment.

Paul Sexton

"clau st" <ravenclau@gmail.com> wrote in message <lnsdfd$1ba$1@newscl01ah.mathworks.com>...
> It seems that in GUI you can't load a java class in 1 function then use the class in another, or so it seems for my Matlab 2013b, or I'm missing something.
> If I don't import the java class package in the function where I have calls to java custom classes, then the constructors won't load or something... would show that there is no function for my data type.
> So for handling custom java classes from Matlab GUI I've found the following workaround:
> function ...
> global global_variables_X;
> local_variables_X=global_variables_X; % save your globals to local space
> javaaddpath /java/;
> import java_class_package.*; % also I get errors when I load only one class at a time, instead of the whole package.
> global variables_X;
> global_variables_X=local_variables_X; % resave your variables to global space
> insert calls to custom java classes..
>
>
> "Alexander" wrote in message <l2hfqd$m7u$1@newscl01ah.mathworks.com>...
> > Another workaround for this is to assign your global variables to a local function variable, and then assign it back after the call to javaclasspath/javaaddpath, as local function variables do not seems to be cleared by the call.
> >
> >
> > "Ryan Wong" <rwong_002@hotmail.com> wrote in message <k84bu9$q6b$1@newscl01ah.mathworks.com>...
> > > This is a summary of the workarounds.
> > >
> > > (1) To just get rid of the issue, edit the following file to add the classpath there. (Remember to replace the MATLAB version in the path.)
> > >
> > > "C:\Program Files\MATLAB\R2011a\toolbox\local\classpath.txt"
> > >
> > > (2) If unfortunately this happens in a large code base and that debugging is not possible (due to the side effect that "clear java", "clear all" also clears all breakpoints), then do this in a command line:
> > >
> > > dbstop in clear
> > >
> > > This will give a warning, which is okay. This command line allows a troubleshooter to identify the lines of culprits by executing the offending package code.
> > >
> > > Thanks,
> > > Ryan Wong
> > >
> > > "Dan Gresh" wrote in message <fofg6m$gta$1@fred.mathworks.com>...
> > > > Hi,
> > > >
> > > > On both matlab 2007a and 2007b with Java JARs of source
> > > > 1.5.0_07, when I use javaaddpath it will sometimes clear all
> > > > my workspace variables. Sometimes it does not clear any
> > > > variables; sometimes it clears all.
> > > >
> > > > This is a significant issue because not all users can add
> > > > the .jars to their static path. Is there any way to stop
> > > > this from happening?
> > > >
> > > > Thanks,
> > > > Dan

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