Does JavaLoader work with Class.forName?

29 views
Skip to first unread message

wfisk

unread,
Mar 9, 2010, 3:57:10 PM3/9/10
to javaloader-dev
I am trying to get ColdFusion to work with Processing ( http://processing.org),
an open-source library for drawing shapes, manipulating images. There
is really just one jar file ( processing/core.jar) and I need to
create an instance of processing.core.PApplet. Well maybe a little
more but that's the start.

This works fine when I place the jar file core.jar in the CF lib
folder and restart the CF server. I am able to create an instance and
even create an image (which is the goal). However when I try with
javaLoader, I can still create an instance of PApplet, but I cannot
get any further because the next call fails. When I look through the
java code it seems to be failing because of this line:
Class<?> rendererClass = Class.forName(irenderer);

or at least this line does not find the class (irenderer is simply a
string).

Should Class.forName still work even if the library has been loaded by
javaLoader?

Should I add a specific import statement in my Java code. For example
irenderer can be "processing.core.P3DGraphics". I already have the
line
import processing.core. *;
at the top of my code. Should I also add
import processing.code.P3DGraphics;

And finally just a word of warning for those that are testing out both
having the java jar files in the ColdFusion lib folder or loading them
via javaLoader. When you are testing loading with javaLoader MAKE
SURE THE JAR FILE IS NO LONGER IN YOUR CF LIB folder. I almost went
crazy with that one today.

Thanks for any advice.

William

Mark Mandel

unread,
Mar 9, 2010, 4:11:17 PM3/9/10
to javaloa...@googlegroups.com
Class.forName() should be fine, as long as the library it is requesting class is loaded into JavaLoader as well.

What is the class it is looking for, and the exact error it throws?

Mark
--
E: mark....@gmail.com
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

Hands-on ColdFusion ORM Training @ cf.Objective() 2010
www.ColdFusionOrmTraining.com/

wfisk

unread,
Mar 10, 2010, 3:25:02 AM3/10/10
to javaloader-dev
Mark,

Another newbie mistake. I was writing
papplet = server.javaLoader.create( "processing.core.PApplet" );
canvas = papplet.createGraphics( 400, 400, papplet.P3D );

and forget the init(), so I changed it to

PAppletClass = server.javaLoader.create( "processing.core.PApplet" );
papplet = PAppletClass.init();
canvas = papplet.createGraphics( 400, 400, papplet.P3D ); <----- this
is the line that causes the problem

However that still didn't solve the problem :(.

Unfortunately I do not know exactly
which line in the Java code is causing the problem. I am getting the
error message
<< You need to use "Import Library" to add processing.core.P3DGraphics
to your sketch. >>
and I can see that line in a catch in the Java code so I assume that
the error is
occuring somewhere in these lines

Class<?> rendererClass =

Thread.currentThread().getContextClassLoader().loadClass(irenderer);

Constructor<?> constructor = rendererClass.getConstructor(new
Class[] { });
PGraphics pg = (PGraphics) constructor.newInstance();

pg.setParent(this);
pg.setPrimary(iprimary);
if (ipath != null) pg.setPath(ipath);
pg.setSize(iwidth, iheight);

// everything worked, return it
return pg;

Also, yesterday I thought it was using Class.forName but maybe I was
mistaken.
When I look at the code it seems to be using
Thread.currentThread().getContextClassLoader().loadClass(irenderer);

Could this code still work with JavaLoader? Is there a potential
problem here?

William

> E: mark.man...@gmail.com

wfisk

unread,
Mar 10, 2010, 3:26:56 AM3/10/10
to javaloader-dev
The exception that is being caught is ClassNotFoundException

Dennis Clark

unread,
Mar 10, 2010, 10:05:36 AM3/10/10
to javaloa...@googlegroups.com
I've seen Mark mention using JavaLoader with thread context classloaders before:


Message 12 explains how to do it, but don't ignore his warning in message 13.

Cheers,

-- Dennis

wfisk

unread,
Mar 10, 2010, 12:41:15 PM3/10/10
to javaloader-dev
Thanks for that. Once I saw the constructor for the class I sensed
that it might have been discussed before:
Just I was focussing on Class.forName; I think that was old code that
had been commented out.
Maybe I could subclass the PApplet class and override the method
causing the problem.

Anyway for now I have gone back to the .jar in the CF lib directory;
That works, and I don't have
much time to experiment. Anyway the Processing code writes to a
graphics context in a
java applet class and I don't know if I can get that working on the
server so that it is a bigger problem.
Works fine running locally from the develeoper CF server.

Thanks very much for the help.

William


On Mar 10, 4:05 pm, Dennis Clark <boomf...@gmail.com> wrote:
> I've seen Mark mention using JavaLoader with thread context classloaders
> before:
>

> http://groups.google.com/group/javaloader-dev/browse_frm/thread/879b7...

Mark Mandel

unread,
Mar 10, 2010, 4:34:51 PM3/10/10
to javaloa...@googlegroups.com
On Thu, Mar 11, 2010 at 4:41 AM, wfisk <willia...@gmail.com> wrote:
Thanks for that.  Once I saw the constructor for the class I sensed
that it might have been discussed before:
Just I was focussing on Class.forName;  I think that was old code that
had been commented out.
Maybe I could subclass the PApplet class and override the method
causing the problem.

It's possible. Libraries that do dynamic loading of classes really need a way to manually set the ClassLoader that is being used. Unfortunately very few do.
 

Anyway for now I have gone back to the .jar in the CF lib directory;
That works, and I don't have
much time to experiment.  Anyway the Processing code writes to a
graphics context in a
java applet class and I don't know if I can get that working on the
server so that it is a bigger problem.
Works fine running locally from the develeoper CF server.

Stupid question - it works on your machine, but you're worried it won't work on another machine? No sure what the issue is here?
 
--
E: mark....@gmail.com

wfisk

unread,
Mar 10, 2010, 4:52:16 PM3/10/10
to javaloader-dev
I am not too sure at the moment. The Processing library is set up to
work as an applet and assumes the java libraries assume a graphics
card. So when running on a server that doesn't have one you can have
problems. This is all as I understand it anyway. I am just trying
things out at the moment.

On Mar 10, 10:34 pm, Mark Mandel <mark.man...@gmail.com> wrote:


> On Thu, Mar 11, 2010 at 4:41 AM, wfisk <william.f...@gmail.com> wrote:
> > Thanks for that.  Once I saw the constructor for the class I sensed
> > that it might have been discussed before:
> > Just I was focussing on Class.forName;  I think that was old code that
> > had been commented out.
> > Maybe I could subclass the PApplet class and override the method
> > causing the problem.
>
> It's possible. Libraries that do dynamic loading of classes really need a
> way to manually set the ClassLoader that is being used. Unfortunately very
> few do.
>
>
>
> > Anyway for now I have gone back to the .jar in the CF lib directory;
> > That works, and I don't have
> > much time to experiment.  Anyway the Processing code writes to a
> > graphics context in a
> > java applet class and I don't know if I can get that working on the
> > server so that it is a bigger problem.
> > Works fine running locally from the develeoper CF server.
>
> Stupid question - it works on your machine, but you're worried it won't work
> on another machine? No sure what the issue is here?
>
> --

> E: mark.man...@gmail.com

Reply all
Reply to author
Forward
0 new messages