• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class Loaders (theory)  RSS feed

 
Manny Garcia
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for fun I've been reading a couple of articles on Java class loaders. The first two articles I read gave lots of info on how write your own class loaders which I understood but they both lacked one important piece of info which still puzzles me: Why would you write your own class loader?

I can think of some very odd circumstances when you'd want to but could anyone give me a good, reasonable case that didn't use an extremely odd set of circumstances that demonstrated a need for writing your own class loader?

Regards,

Manny
 
Prakash anandaraj Chinnappan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of reasons to write your own class loaders. One of which is say all of your class files are encrypted for security reasons and protect your code. Now you can not load a encrypted class normally you have to come with your own class loader which decrypts and load the class without creating any temp files.
[ March 07, 2008: Message edited by: Prakash anandaraj Chinnappan ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote one to make it easier to dynamically add / revise classes in a running application.

Of course you do get all tangled up in security restrictions and have to use reflection alot.

Bill
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applications that have a mechanism for adding user code (like plugins) frequently need to do this; see here.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shows a file date of 3/9/2008 - it is extremely helpful. How long has that been up, the late file date could be a consequence of disrelated matters.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can imagine loading "java classes" that are not in java format.
Or anytime you need to find the java class in some weird place.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That article I wrote has been there since 2006.

I remember IBM publishing an article on DeveloperWorks about a class loader that was only given Java source code to work with -no class files- and would automatically recompile the source whenever it changed.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since many deployments include the Java compiler, generating the .class file from the .java source is pretty easy. Subject to a suitable jail, of course.

I had a longtime engineer say that its silly to use text files, property files, etc. to be configuration parameters, when you could just as easily use java source code.

This would let you have expressions in your constants and similar powerful constructs.

He never convinced me to implement it, but its a fairly clever idea, and with a class loader, easy to do.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just ran across this article providing some insight on when and how to use custom class loaders.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give any idea of how to load a C/C++ file with java.Is it possible with any of java class loaders or through reflection or introspection.
 
K Aditi
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give any idea of how to load a C/C++ file with java.

Native code can be interfaced with java using Java Native Interface (JNI).
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't the loading of c/c++ source would be possible with reflection api?
 
K Aditi
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't the loading of c/c++ source would be possible with reflection api?

I think use of JNI to load native code is inevitable though you can load them on-the-fly using Reflection API. JNI and Reflection APIs address differnt issues and both can be used in conjunction.
[ June 17, 2008: Message edited by: K Aditi ]
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aditi, can you be bit precise about your last explanation
 
K Aditi
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though I don't have thorough knowledge of Reflecton API what I gathered about it till now is that one of the main purpose of Reflection API is to load classes at runtime just by knowing the name of the Class.

JNI is a mechanism which can leverage the underlying platform specific capabilities which may not be possibly utlized by java.Usually the "capabilities" which harness the platform are written in native code i.e. C/C++.

both can be used in conjunction.

And my conjecture is native class files can be loaded (by making use of JNI) at runtime (by making use of Reflection API).

Hope the point is clear.Correct me if I am wrong.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection is unrelated to using native code - it applies to Java bytecode only.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give any idea of how to load a C/C++ file with java.Is it possible with any of java class loaders or through reflection or introspection.
What exactly do you mean by "load a C/C++ file with Java"? If you mean load the code into memory to execute within the JVM, you're going to have to do that via JNI directly since there are several other operations which need to occur for a native executable - like linking to dynamic libraries - and C++ structures (like classes, enums, structs, pointers) aren't precisely equivalent to Java's so you would need the JNI interface to negotiate on your behalf.

As far as I know, there is no reflection for discovering native code in JNI - this comes about since most production native programs are compiled without metadata so there is insufficient information to do reflection at runtime (unlike Java which always writes this metadata into its bytecode files). Bear in mind also that C isn't OO, so an OO reflection API would be useless here anyway.

It's possible, and quite normal in fact, to go the other way around: you can easily use JNI from a native program in order to discover (via reflection) information about Java classes. Of course, you don't want to use native code unless you really have to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!