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:
Folders when calling JAVA classes in MATLAB

Subject: Folders when calling JAVA classes in MATLAB

From: Santiago Balestrini

Date: 21 Apr, 2009 22:03:01

Message: 1 of 9

I'm calling some JAVA classes that allow me to run an agent-based simulation environment called NetLogo from MATLAB.

My problem is that the NetLogo model is in multiple files (for those familiar with NetLogo, I have a series of NLS files in addition to my NLOGO file). I switch my current directory to where all my NetLogo model files are, but when I import the required JAVA classes and invoke the object, I get an error because NetLogo is looking for the other files in the model in the default work folder in MATLAB. If I copy all the support model files to the work directory it loads the model fine and I can execute it without a problem, but this is not a very elegant solution and I was wondering if anyone out there knew why this would be the case and if there is a way to fix it.

I've even added the folder that has the model to the MATLAB path, but that didn't work. It is as if when running an external JAVA class, MATLAB forces the default directory of that class to be not the current directory, but MATLAB's own default directory.

Any help will be appreciated, and thanks for taking the time to read this,

-Santiago

COPY OF THE ERROR MESSAGE:

??? Java exception occurred:
Could not find C:\Program Files\MATLAB\R2008a\work\setup-procs.nls at position 13 in

at org.nlogo.compiler.Compiler.exception(Compiler.java:343)

at org.nlogo.compiler.StructureParser.parse(StructureParser.java:272)

at org.nlogo.compiler.Compiler.compile(Compiler.java:123)

at org.nlogo.compiler.Compiler.compileProgram(Compiler.java:31)

at org.nlogo.headless.HeadlessWorkspace.openFromMap(HeadlessWorkspace.java:934)

at org.nlogo.headless.HeadlessWorkspace.open(HeadlessWorkspace.java:730)


Error in ==> NLOpenModel at 19
        NLModel.open([varargin{1} '.nlogo']);

Subject: Folders when calling JAVA classes in MATLAB

From: Donn Shull

Date: 22 Apr, 2009 00:07:01

Message: 2 of 9

"Santiago Balestrini" <sanbales@gmail.com> wrote in message <gslfql$3ip$1@fred.mathworks.com>...
> I'm calling some JAVA classes that allow me to run an agent-based simulation environment called NetLogo from MATLAB.
>
> My problem is that the NetLogo model is in multiple files (for those familiar with NetLogo, I have a series of NLS files in addition to my NLOGO file). I switch my current directory to where all my NetLogo model files are, but when I import the required JAVA classes and invoke the object, I get an error because NetLogo is looking for the other files in the model in the default work folder in MATLAB. If I copy all the support model files to the work directory it loads the model fine and I can execute it without a problem, but this is not a very elegant solution and I was wondering if anyone out there knew why this would be the case and if there is a way to fix it.
>
> I've even added the folder that has the model to the MATLAB path, but that didn't work. It is as if when running an external JAVA class, MATLAB forces the default directory of that class to be not the current directory, but MATLAB's own default directory.
>
> Any help will be appreciated, and thanks for taking the time to read this,
>
> -Santiago
>
> COPY OF THE ERROR MESSAGE:
>
> ??? Java exception occurred:
> Could not find C:\Program Files\MATLAB\R2008a\work\setup-procs.nls at position 13 in
>
> at org.nlogo.compiler.Compiler.exception(Compiler.java:343)
>
> at org.nlogo.compiler.StructureParser.parse(StructureParser.java:272)
>
> at org.nlogo.compiler.Compiler.compile(Compiler.java:123)
>
> at org.nlogo.compiler.Compiler.compileProgram(Compiler.java:31)
>
> at org.nlogo.headless.HeadlessWorkspace.openFromMap(HeadlessWorkspace.java:934)
>
> at org.nlogo.headless.HeadlessWorkspace.open(HeadlessWorkspace.java:730)
>
>
> Error in ==> NLOpenModel at 19
> NLModel.open([varargin{1} '.nlogo']);

Hello Santiago,

MATLAB maintains the locations of java classes in the file classpath.txt. You can find it with which('classpath.txt'). In more recent versions of MATLAB there is also a dynamic classpath that you can manipulate with the javaclasspath function.

Hope this helps,

Donn

Subject: Folders when calling JAVA classes in MATLAB

From: Santiago Balestrini

Date: 23 Apr, 2009 20:50:18

Message: 3 of 9

DONN: MATLAB maintains the locations of java classes in the file classpath.txt. You can find it with which('classpath.txt'). In more recent versions of MATLAB there is also a dynamic classpath that you can manipulate with the javaclasspath function.


Hey Donn,

Thanks for the quick reply. I added the folder where I have all my model files using the javaclasspath command, but it didn't do the trick. I still get the same exact error. I don't understand why MATLAB would make the default folder for other JAVA programs be MATLAB's own default folder. This might be something that requires me to do something on both ends (MATLAB and NetLogo). I was just wondering if anyone in this forum has seen this problem before.

Thanks again for the idea though, this javaclasspath command actually helps me make my implementation more elegant.

-Santiago

Subject: Issue with MATLAB Startup Directory when calling JAVA classes

From: Santiago Balestrini

Date: 25 Apr, 2009 04:40:17

Message: 4 of 9

I've checked and the problem is that whatever I set the MATLAB startup directory, that is the default directory of the external JAVA classes that I call from MATLAB. I cannot change those JAVA classes, so I was wondering if anyone else had had this problem and knew of a way to deal with it. I've tried to use userpath to change the current directory, but that does not solve the problem. The only way I can get the JAVA classes to use a different directory, is to set it in the windows shortcut. This is clearly not an option since I want to have the flexibility to have the JAVA classes and their associated files in whatever folder they may be.

Thanks for reading this, any help will be welcome,

-Santiago

Subject: Issue with MATLAB Startup Directory when calling JAVA classes

From: PrakritiPriyanka

Date: 25 Apr, 2009 14:00:55

Message: 5 of 9

On Apr 25, 9:40=A0am, "Santiago Balestrini" <sanba...@gmail.com> wrote:
> I've checked and the problem is that whatever I set the MATLAB startup di=
rectory, that is the default directory of the external JAVA classes that I =
call from MATLAB. I cannot change those JAVA classes, so I was wondering if=
 anyone else had had this problem and knew of a way to deal with it. I've t=
ried to use userpath to change the current directory, but that does not sol=
ve the problem. The only way I can get the JAVA classes to use a different =
directory, is to set it in the windows shortcut. This is clearly not an opt=
ion since I want to have the flexibility to have the JAVA classes and their=
 associated files in whatever folder they may be.
>
> Thanks for reading this, any help will be welcome,
>
> -Santiago

I want to run a JAVA class in matlab editor.... can anyone pls help
me...

Subject: Issue with MATLAB Startup Directory when calling JAVA classes

From: Santiago Balestrini

Date: 25 Apr, 2009 17:15:03

Message: 6 of 9

> I want to run a JAVA class in matlab editor.... can anyone pls help me...

What do you mean by wanting to run a JAVA class in MATLAB editor? b/c All you need to do is add the path to the JAR file(s) that has your JAVA classes in classpath.txt (type which classpath.txt to find out where it is), and then simply use import to import the package. At that point you can instantiate any JAVA object in those classes, there is very good help for this in the MATLAB manual.

Hope this helps,

Cheers!

PS: Now, if you want to edit JAVA files in MATLAB editor, I guess you could do that, I don't know why. You would have to compile them, in order to execute them.

Subject: Issue with MATLAB Startup Directory when calling JAVA classes

From: matt dash

Date: 25 Apr, 2009 18:04:02

Message: 7 of 9

"Santiago Balestrini" <sanbales@gmail.com> wrote in message <gsu47h$pnb$1@fred.mathworks.com>...
> I've checked and the problem is that whatever I set the MATLAB startup directory, that is the default directory of the external JAVA classes that I call from MATLAB. I cannot change those JAVA classes, so I was wondering if anyone else had had this problem and knew of a way to deal with it. I've tried to use userpath to change the current directory, but that does not solve the problem. The only way I can get the JAVA classes to use a different directory, is to set it in the windows shortcut. This is clearly not an option since I want to have the flexibility to have the JAVA classes and their associated files in whatever folder they may be.
>
> Thanks for reading this, any help will be welcome,
>
> -Santiago

Hey Santiago, if you can send me your files i'll take a look at it.
-Matt (daskil...)

Subject: Issue with MATLAB Startup Directory when calling JAVA classes

From: Bhavesh

Date: 8 Sep, 2010 00:42:06

Message: 8 of 9

"matt dash" <n.a@mail.com> wrote in message <gsvjai$q94$1@fred.mathworks.com>...
> "Santiago Balestrini" <sanbales@gmail.com> wrote in message <gsu47h$pnb$1@fred.mathworks.com>...
> > I've checked and the problem is that whatever I set the MATLAB startup directory, that is the default directory of the external JAVA classes that I call from MATLAB. I cannot change those JAVA classes, so I was wondering if anyone else had had this problem and knew of a way to deal with it. I've tried to use userpath to change the current directory, but that does not solve the problem. The only way I can get the JAVA classes to use a different directory, is to set it in the windows shortcut. This is clearly not an option since I want to have the flexibility to have the JAVA classes and their associated files in whatever folder they may be.
> >
> > Thanks for reading this, any help will be welcome,
> >
> > -Santiago
>
> Hey Santiago, if you can send me your files i'll take a look at it.
> -Matt (daskil...)








How to use java package in matlab??? I have tried to add JAR(s) file to 'classpath.txt' ...but it's not working....


Help help ..please guys...

Subject: Issue with MATLAB Startup Directory when calling JAVA classes

From: Yair Altman

Date: 8 Sep, 2010 07:30:13

Message: 9 of 9

"Bhavesh " <s.rk@hotmail.co.uk> wrote in message
> How to use java package in matlab??? I have tried to add JAR(s) file to 'classpath.txt' ...but it's not working....


You can add either JAR files or folder full-path names to your classpath.txt file (which is located in your %matlabroot/toolbox/local/ folder). Each addition must be on a separate line. Examples:

$matlabroot/java/jar/widgets.jar
$matlabroot/java/patch/
C:\Program Files\Java\my-code.zip

You can then use your code in Matlab provided that you:
1) use the correct FQCN (e.g., com.mypackage.MyClass)
2) your class was declared as public
3) one of the following:
3a) your class has public static methods that you use directly (a=com.mypackage.MyClass.myStaticMethod(1,2,3)) or:
3b) your class has public constructors that you use to instantiate an object ((a=com.mypackage.MyClass(1,2,3))

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