Loading a DLL in MATLAB through JAVA

6 views (last 30 days)
Hi,
I have a running application in Mathlab, that also uses some C DLL files.
I have created a JAVA package using the JA builder of mathlab functions. Then I put the DLLs in the JAVA main project folder.
I could invoke any method that doesnt use the DLL and it works fine. But whenever I invoke a method that uses the DLL files, it crashes and here is the stacktrace.
{??? Error using ==> loadlibrary at 477 FAiled..Error using ==> loadlibrary at 477 There was an error loading the library There was an error loading the library "C:\Users\AUG\Documents\Backup\SourceTracing\epanet2.dll" "C:\Users\AUG\Documents\Backup\SourceTracing\epanet2.dll" Undefined function or variable 'epanet2_proto'. Undefined function or variable 'epanet2_proto'.
Error in ==> epanetloadfile at 31
Caused by: Error using ==> feval Undefined function or variable 'epanet2_proto'. } ... Matlab M-code Stack Trace ... file C:\Program Files\MATLAB\R2010b\toolbox\compiler\mcr\matlab\general\loadlibrary.m, name loadlibrary, line 477. file C:\Users\AUG\AppData\Local\Temp\AUG\mcrCache7.14\Source12\SourceTrace\epanetloadfile.m, name epanetloadfile, line 31. com.mathworks.toolbox.javabuilder.MWException: Error using ==> loadlibrary at 477 There was an error loading the library "C:\Users\AUG\Documents\Backup\SourceTracing\epanet2.dll" Undefined function or variable 'epanet2_proto'. at com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval(Native Method) at com.mathworks.toolbox.javabuilder.internal.MWMCR.access$600(MWMCR.java:23) at com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mclFeval(MWMCR.java:902) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.mathworks.toolbox.javabuilder.internal.MWMCR$5.invoke(MWMCR.java:800) at $Proxy0.mclFeval(Unknown Source) at com.mathworks.toolbox.javabuilder.internal.MWMCR.invoke(MWMCR.java:475) at SourceTrace.SourceTrace.epanetloadfile(SourceTrace.java:580) at leakdetection.water.source.tracing.Test.<init>(Test.java:46) at leakdetection.water.source.tracing.Test.main(Test.java:19) BUILD SUCCESSFUL (total time: 13 seconds)

Accepted Answer

Friedrich
Friedrich on 11 Jul 2011
Hi,
When compiling code which uses loadlibrary you have to take care of some things first:
If you need to distribute your shared library to other users who will use it in MATLAB themselves or who will use your MATLAB functions which call the library; they would also need a compiler to create a thunkfile, unless you create one for them and work with a prototype M-file:
1. Use the 'mfilename' option of LOADLIBRARY, this will generate a prototype M-file and the thunkfile:
loadlibary('myLibrary','myLibrary.h','mfilename','myLibrary.m')
2. In the code which uses the library, instead of using the header:
loadlibrary('myLibrary','myLibrary.h')
Now use the prototype M-file:
loadlibrary('myLibrary',@myLibrary)
If your end-users call the library by themselves you may need to tell them this.
3. Distribute the actual library (myLibrary.dll), the prototype M-file (myLibrary.m) and the thunkfile (myLibrary_thunk_pcwin64.dll) to your end-users.

More Answers (1)

Kaustubha Govind
Kaustubha Govind on 11 Jul 2011
As the error suggests:
Undefined function or variable 'epanet2_proto'
You need to add the prototype file epanet2_proto.m to the archive (add it under "Additional Files" if using DEPLOYTOOL or append using -a option if using MCC).

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!