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:
Forcing a Java class to load

Subject: Forcing a Java class to load

From: Luis

Date: 19 May, 2010 03:24:04

Message: 1 of 6

How do you force a java class to load, so that class initialization occurs?

The obvious approach:

>> java.lang.Class.forName('org.apache.tools.zip.ZipFile')

generates a ClassNotFoundException exception.

The goal is to just get the class initializer to run (and leave the class loaded, as shown by inmem). One kludge would be to find a way to *construct* the class

>> x = org.apache.tools.zip.ZipFile(123)

(if the class happens to have a constructor that takes an int, which it does not). Clearly this is undesirable since one may not want to construct an instance of the class.

Thanks.

Luis

Subject: Forcing a Java class to load

From: Yair Altman

Date: 19 May, 2010 20:09:05

Message: 2 of 6

"Luis " <lemon.loshea.removethisandfruit@gmail.com> wrote in message <hsvlkk$bpu$1@fred.mathworks.com>...
> How do you force a java class to load, so that class initialization occurs?
>
> The obvious approach:
>
> >> java.lang.Class.forName('org.apache.tools.zip.ZipFile')
>
> generates a ClassNotFoundException exception.
>
> The goal is to just get the class initializer to run (and leave the class loaded, as shown by inmem). One kludge would be to find a way to *construct* the class
>
> >> x = org.apache.tools.zip.ZipFile(123)
>
> (if the class happens to have a constructor that takes an int, which it does not). Clearly this is undesirable since one may not want to construct an instance of the class.
>
> Thanks.
>
> Luis


Instantiating a class object is my preferred way, but here's another alternative you could use, which I use in my checkClass utility on the Matlab File Exchange:

try
   thisClass = java.lang.Class.forName(className);
catch
   classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
   thisClass = classLoader.loadClass(className);
end

This enables loading a class that has no constructor, or whose constructors you do not want, or are unable, to invoke.

Note that it uses the undocumented/unsupported JMI classloader, so it might not work on some versions/platforms/classes.

Yair Altman
http://UndocumentedMatlab.com

Subject: Forcing a Java class to load

From: Bastian Ebeling

Date: 17 Oct, 2012 09:02:08

Message: 3 of 6

Hi Yair,

thanks for your lines
"Yair Altman" wrote in message <ht1gh1$s2h$1@fred.mathworks.com>...
> ...
> try
> thisClass = java.lang.Class.forName(className);
> catch
> classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
> thisClass = classLoader.loadClass(className);
> end
> ...
> Yair Altman
> http://UndocumentedMatlab.com
which nearly match the explanations from "http://www.mathworks.de/de/help/matlab/matlab_external/creating-and-using-java-objects.html#bq__5rt-1".
Your deep knowledge seems to me, that you can maybe explain, why connection to a mysql-db within matlab (without db-toolbox) with the following statements is not working? (I think those EDT-calls are the "official" ones comparing to your lines).

mysqlDriver=javaObjectEDT('com.mysql.jdbc.Driver');
dbconn=java.sql.DriverManager.getConnection('jdbc:mysql://localhost:3306/testdb','user','pass');

I always get "Java exception occurred:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb"
The path is setup correctly - and connection via jdbc4-DataSource works. I only wonder, what gets wrong.
Even using the DriverManager with EDT-Commands is not working - and manually registering the driver also fails.
Thanks.

Subject: Forcing a Java class to load

From: Yair Altman

Date: 17 Oct, 2012 19:50:09

Message: 4 of 6

"Bastian Ebeling" wrote in message <k5ls6g$jhs$1@newscl01ah.mathworks.com>...
> Hi Yair,
>
> thanks for your lines
> "Yair Altman" wrote in message <ht1gh1$s2h$1@fred.mathworks.com>...
> > ...
> > try
> > thisClass = java.lang.Class.forName(className);
> > catch
> > classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
> > thisClass = classLoader.loadClass(className);
> > end
> > ...
> > Yair Altman
> > http://UndocumentedMatlab.com
> which nearly match the explanations from "http://www.mathworks.de/de/help/matlab/matlab_external/creating-and-using-java-objects.html#bq__5rt-1".
> Your deep knowledge seems to me, that you can maybe explain, why connection to a mysql-db within matlab (without db-toolbox) with the following statements is not working? (I think those EDT-calls are the "official" ones comparing to your lines).
>
> mysqlDriver=javaObjectEDT('com.mysql.jdbc.Driver');
> dbconn=java.sql.DriverManager.getConnection('jdbc:mysql://localhost:3306/testdb','user','pass');
>
> I always get "Java exception occurred:
> java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/testdb"
> The path is setup correctly - and connection via jdbc4-DataSource works. I only wonder, what gets wrong.
> Even using the DriverManager with EDT-Commands is not working - and manually registering the driver also fails.
> Thanks.

Bastian - JDBC requires using the static rather than dynamic Java classpath.
A detailed description of using JDBC with Matlab is included in section 2.2 of My Matlab-Java programming book: http://undocumentedmatlab.com/matlab-java-book/

MathWorks' DB Toolbox also uses the same JDBC driver, which is why they also force users to add it to their static Java classpath.

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Forcing a Java class to load

From: Bastian Ebeling

Date: 18 Oct, 2012 08:51:08

Message: 5 of 6

Hi Yair,

thanks - it was new to me, that there is a difference on using static or dynamic classpath. I just learned, that the static one also speeds up the hole thing.

"Yair Altman" wrote in message <k5n25h$e2e$1@newscl01ah.mathworks.com>...
> ...
> Bastian - JDBC requires using the static rather than dynamic Java classpath.
> A detailed description of using JDBC with Matlab is included in section 2.2 of My Matlab-Java programming book: http://undocumentedmatlab.com/matlab-java-book/
>
> MathWorks' DB Toolbox also uses the same JDBC driver, which is why they also force users to add it to their static Java classpath.

But now the bad news:
For deploying an apllication I only found out (unter http://www.mathworks.de/support/solutions/en/data/1-3Q6KZW/index.html?solution=1-3Q6KZW) classes will be automatically added to the dynamic path. I did not find a way to get that into the static path.
Do you have a solution?
Greetings

Bastian

Subject: Forcing a Java class to load

From: Yair Altman

Date: 18 Oct, 2012 18:19:09

Message: 6 of 6

"Bastian Ebeling" wrote in message <k5ofts$hpg$1@newscl01ah.mathworks.com>...
> Hi Yair,
>
> thanks - it was new to me, that there is a difference on using static or dynamic classpath. I just learned, that the static one also speeds up the hole thing.
>
> "Yair Altman" wrote in message <k5n25h$e2e$1@newscl01ah.mathworks.com>...
> > ...
> > Bastian - JDBC requires using the static rather than dynamic Java classpath.
> > A detailed description of using JDBC with Matlab is included in section 2.2 of My Matlab-Java programming book: http://undocumentedmatlab.com/matlab-java-book/
> >
> > MathWorks' DB Toolbox also uses the same JDBC driver, which is why they also force users to add it to their static Java classpath.
>
> But now the bad news:
> For deploying an apllication I only found out (unter http://www.mathworks.de/support/solutions/en/data/1-3Q6KZW/index.html?solution=1-3Q6KZW) classes will be automatically added to the dynamic path. I did not find a way to get that into the static path.
> Do you have a solution?
> Greetings
>
> Bastian


You can manually add the classpath.txt file to the "Other files" section when compiling, and/or place this file in the deployed folder. Since classpath.txt does *NOT* accept relative filepaths but only absolute filepaths, this means that you must ensure that in all your deployed environments the relevant JDBC JAR file is located in the same absolute path.

One way to do this dynamically is to place the JAR file in your deployed folder, and add the following pseodo-code to the beginning of your Matlab app:

function varargout = myApp(varargin)
   staticPaths = lower(javaclasspath('-static'));
   if all(cellfun('isempty',strfind(staticPaths,lower(jarFilename))))
      % Not in static classpath, so add it to the classpath and restart the app
      deployFolder = fileparts(mfilename('fullpath'));
      fid = fopen('classpath.txt','at')
      fprintf(fid,'%s/%s\n',deployFolder,jarFilename);
      fclose(fid);
      [varargout{:}] = myApp(varargin{:});
      exit;
   end
   % continue with the regular processing...
end

More information about this and other Matlab-Java topics can be found in my website and my Matlab-Java programming book.

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