• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a hook/notification for when a class is loaded?  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to be notified of when a class is loaded/defined by the VM?
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not using the standard ClassLoader, but you can extend it and add whatever hooks you want.
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Run the JVM with parameter: -Djava.system.class.loader=MyClassLoader
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Thanks for the reply!
With my own classloader (which I did implement) I do have the hooks in place, but I want be able to react to when some code defines/loads a class in a different classloader. Is there any way to do that? Are there no hooks for the VM?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're just never satisfied, are you?
I've never used it, but the Java Platform Debugger Architecture (included in the JDK; include tools.jar in classpath) seems to have what you need. If you can figure out how to connect to your VirtualMachine instance, the code is something like this (I think):
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could put a static block in the class. That code will execute the first time, um, Idunno what. But it just might be the moment you need to know about.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim! I'll take a look at that and see if it offers what I'm looking for. Any idea if that'll run with the JRE (and without the SDK) assuming I include the tools.jar?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. If it fires at the right time for you - don't really know your requirement - it's just plain old Java code, nothing at all special. It's a little strange looking, tho, because it's not in any method:

[ February 17, 2004: Message edited by: Stan James ]
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Yup. If it fires at the right time for you - don't really know your requirement - it's just plain old Java code, nothing at all special.

The problem is that I need hooks for classes I don't write too. So I want to know when those are loaded as well. It is a good idea though. Thanks!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea if that'll run with the JRE (and without the SDK) assuming I include the tools.jar?
Sure. The part I was having trouble with though was connecting to the correct VirtualMachine instance. The examples I found (in [jdk_install_dir]/demo/jpda/examples.jar) seem to be oriented toward debugging one JVM from another JVM. Makes sense if you're writing a debugger, but I don't know if that would work for you. I don't know if it's possible to use JPDA to get info on the same JVM you're running from. Probably, but it wasn't immediately clear how; I didn't have time to keep investigating. Please let me know if you find a good way to do it though. (Assuming this technique sounds like it may work for your application.)
[ February 17, 2004: Message edited by: Jim Yingst ]
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
I'll take a look and let you know if I figure it out.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The part I was having trouble with though was connecting to the correct VirtualMachine instance.

Doesn't the JVM listen on a particular port for debugger attachments? I was debugging an applet we use on the intranet at work a few weeks and was considering attaching a debugger to the plug-in and read a bit about it. Here is what the help file for the plug-in says:

Debugging applets in Java Plug-in
The following options are used when debugging applets in the Java Plug-in. For more information on this topic see the Debugging Support in the Java Plug-in Developer Guide.
-Djava.compiler=NONE
-Xnoagent
-Xdebug
-Xrunjdwp:transport=dt_shmem,address=<connect-address>,server=y,suspend=n
The <connect-address> can be any string (example: 2502) which is used by the Java Debugger (jdb) later to connect to the JVM.

I'm not sure if those settings are standard for any JVM or not, but it may give a starting point.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!