Clear Filters
Clear Filters

Error using load and program hangs

3 views (last 30 days)
I am trying to call Matlab (ver 7.10) from Java. I have received some autogenerated Java wrapper classes generated from Matlab Builder JA.
The Matlab program is extremely simple, it takes one parameter, prints it out and returns the same parameter again.
My Test Java class has a main method with this code:
public static void main (String[] args)
{
MWCharArray a = null;
TestTool2 test = null;
System.out.println("Starting");
try {
test = new TestTool2();
//Object[] result = test.testJava2(1, "Hello World");
a = new MWCharArray("Mary Smith");
Object[] result = test.testJava2(1, a);
for (int i=0; i < result.length; i++) {
System.out.println("Result=" + result[i]);
}
System.out.println("Done");
} catch (Throwable t) {
t.printStackTrace();
} finally {
a.dispose();
test.dispose();
}
System.out.println("Stopping");
}
When I run my program, I get some error messages. However the Matlab function is still called, it also prints out my input and return output correctly. Unfortunately after that the program just hangs. It never terminates. It does print out Stopping but never goes back to the command line prompt. What am I missing?
Here is the output of the program:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\work\code\neogrid\ifiv\test\MatlabTest\TestTool2>run
Starting
??? Error using ==> load
Unable to read file C:\Program Files (x86)\MATLAB\MATLAB Compiler
Runtime\v710\lastdir: No such file or directory.
Error in ==> startup at 1
Mary Smith
Result=Mary Smith
Done
Stopping
  1 Comment
John Datson
John Datson on 6 Jul 2011
When checking in Process Explorer, it looks like there is an extra thread running that doesnt die.
The thread is created when I instantiate the TestTool2 class, which is autogenerated by Matlab Builder JA.
Anybody got an idea to what the problem could be, how do I terminate the thread?

Sign in to comment.

Accepted Answer

John Datson
John Datson on 7 Jul 2011
Thank you for your reply Kaustubha,
We updated to MCR 7.14 and I received a new JAR file that I tested up against.
Still many threads are running even after different dispose commands has been executed. This time usually around 5-7 threads where as before it was always 3.
However the program now terminates as it is supposed to and the threads seems to die as they should.
It would be interesting to know why so many threads are created and if you should do anything to shut them down in a clean way ?
But things seems to work okay now, so I am happy. I guess MCR 7.10 had some bugs regarding Java.

More Answers (1)

Kaustubha Govind
Kaustubha Govind on 6 Jul 2011
Do you have a custom startup.m file in the MATLAB installation that you used to compile with MATLAB Builder JA? To locate it, type the following at the MATLAB prompt:
which startup
Examine this file to see if you have any LOAD commands to load data from a MAT-file. If yes, wrap them inside an IF statement checking for (~isdeployed), so that compiled applications do not attempt to execute these commands.
  2 Comments
John Datson
John Datson on 6 Jul 2011
Apparently yes, there was a startup file that has now been changed as you specify. That got rid of the error using load.
Unfortunate it does not solve the main problem, namely the program not terminating. I have added a few more logs to my program. Before I call any matlab code I have one thread running (according to Thread.activeCount()) and after my call to the matlab code I suddenly have 3 threads running.
So far I have solved this problem by calling Thread.currentThread().getThreadGroup().interrupt(); and that works, but is a really ugly hack.
So what could the problem be, what are those threads being created doing, and why dont they terminate?
Kaustubha Govind
Kaustubha Govind on 7 Jul 2011
John: Sorry, I'm not very familiar with Builder JA, so I don't know why you see additional threads created. Do you see similar behavior when deploying one of the documentation examples: http://www.mathworks.com/help/toolbox/javabuilder/ug/bsqrikz-1.html
If you don't get a reply here, it might be worth contacting Tech Support.

Sign in to comment.

Categories

Find more on Java Package Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!