Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Reflection on Jar NOT in Classpath?

Thread view: 
RVince - 10 Jul 2007 01:25 GMT
Has anyone ever done, or know if it is even possible, to perform Reflection
on a .class file inside of a jar that is NOT in the classpath?

All I have is, say a full pathname to a jar file, and a pathname inside the
jar file to a class. For example, I may know I have to look in
C:\foo\somejar.jar for a file therein called, say
\aPathInTheJar\someclass.class.

Again, nothing is in the classpath as I cannot know those two parameters
until runtime. Does anyone have any idea where to start on something like
this. I have used Reflection before, but, always on classes which were in
the classpath. Does anyone have any thoughts on this before I dig in and try
to tackle it? Thanks.
Tom Hawtin - 10 Jul 2007 01:47 GMT
> All I have is, say a full pathname to a jar file, and a pathname inside the
> jar file to a class. For example, I may know I have to look in
> C:\foo\somejar.jar for a file therein called, say
> \aPathInTheJar\someclass.class.

Create a java.net.URLClassLoader with a URL pointing to your jar. The
three argument Class.forName will get you Class objects. It's pretty
much standard from there.

Tom Hawtin
Roedy Green - 10 Jul 2007 10:34 GMT
>Has anyone ever done, or know if it is even possible, to perform Reflection
>on a .class file inside of a jar that is NOT in the classpath?

You would have to write a custom class loader than could file the
bytes forming the class.

see http://mindprod.com/jgloss/classloader.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Andrew Thompson - 10 Jul 2007 14:08 GMT
>>Has anyone ever done, or know if it is even possible, to perform Reflection
>>on a .class file inside of a jar that is NOT in the classpath?
>
>You would have to write a custom class loader than could file the
>bytes forming the class.

Sure - if you were restricted to Java 1.1.

>see http://mindprod.com/jgloss/classloader.html

"..Have a look at the java.net.URLClassLoader.
You may find for your given problem you don't
even have to write (a) whole new ClassLoader, just
instantiate one of Sun's. "

URLCL was introduced in 1.2*, and seems a
robust and practical solution to the OP's
requirement.

So, why go to the bother of coding a custom
class loader, in this instance?

* I am making a WAG that the OP intends this
code to run on a 1.2+ JRE.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Lew - 10 Jul 2007 14:14 GMT
> URLCL was introduced in 1.2*, and seems a
> robust and practical solution to the OP's
[quoted text clipped - 5 lines]
> * I am making a WAG that the OP intends this
> code to run on a 1.2+ JRE.

Excepting JME, about which I know nearly nothing, Sun has retired Java 1.3,
much less 1.2, much less 1.1, some time since.  Java 1.4 is in hospice as we
speak.  So your G isn't really so WA.

Signature

Lew

Roedy Green - 12 Jul 2007 04:14 GMT
>So, why go to the bother of coding a custom
>class loader, in this instance?

It is not clear where the bytes forming the class file are.  If live
in a database for example, you won't be able to get at them via an
URL.

Anyway I pointed him to more details on just how to you get a custom
classloader, either by configuring one of Sun's or by writing your
own.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Andrew Thompson - 12 Jul 2007 04:53 GMT
>>So, why go to the bother of coding a custom
>>class loader, in this instance?
>
>It is not clear where the bytes forming the class file are.  If live
>in a database for example, you won't be able to get at them via an
>URL.

The OP stated 'jar' and I presumed '..on file system
or web server'.  But good point, for all I know, the jar
may be stored as a (whadda'ya'callit?) BLOB in the
DB.

>Anyway I pointed him to more details on just how to you get a custom
>classloader, either by configuring one of Sun's or by writing your
>own.

True, and if the OP read all the way to the end of
the page, the information on URLCL was also there.

Thanks for clarifying.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Twisted - 12 Jul 2007 06:39 GMT
> >>So, why go to the bother of coding a custom
> >>class loader, in this instance?
[quoted text clipped - 7 lines]
> may be stored as a (whadda'ya'callit?) BLOB in the
> DB.

Some databases can in effect be accessed via a URL. Those backing Web
servers come to mind. :)

In fact, on a Windows system a custom URL handler that accesses a
database can be written and installed. I'm not sure about Unix systems
(including MacOS X) but they probably have some sort of system-wide
URL handler capability too, at least if they have decent modern window
managers (as MacOS X presumably does).

Another thing to note is that the next generation of filesystems is
likely to be built on full-blown ACID database logic, for robustness,
scalability and other benefits, now that typical home computing
hardware can easily accommodate such a thing. (They may also be
associative, discarding the concept of a unique "parent directory" and
any distinctions between hard symlinks and "the real location" of a
file or directory. Files are deleted when they no longer have any
parents at all, aside from the root never being deleted (it may have
itself as a parent instead of no parent to avoid special-casing it).
Semantic link types might exist as well, allowing other sorts of links
than "this file or directory is a child of that directory". It could
be as big a change as the change from flat to hierarchical in the late
1980s, when there were still personal computers in widespread use with
8-bit microprocessors with no MULTIPLY instructions and a flat
directory of six-character-maximum file names on each disk, clearly
not at all suitable to scaling up much above a few dozen files ... not
that those 5 and a quarter inch floppies could hold very many ...)

Regardless, I can tell you right now that even the venerable NTFS is
showing age and creaking under modern loads of millions of files,
hundreds of gigs, and really long paths and file names to keep all
those millions straight. A DB based system would tolerate duplicate
file names, since a GUID key would be used under the hood to identify
each file, and ordinarily would not be needed by or exposed to the
user. This is what allows the "unique parent" and distinction of links
from "the real file" to go away, and it even permits anonymous files.
Temp files need never risk collision again. Users making temporary
files or copies or otherwise fiddling around needn't bother coming up
with unique names for things that may not even last as long as coming
up with a name and typing it would take. When a file becomes important
that didn't used to be it can be given a proper name, or renamed from
a poorer name to a more descriptive one, and its GUID won't change so
it will still be found by anything that references it, providing the
referencing uses the GUID under the hood. Applications can appear as
containers with their code and data files inside (kind of like JAR
files now?)...if a file named "foo.cfg" is inside the application, it
uses this file as a config file ... finding the application's stuff is
easy as it's inside the application itself, in a sense. User-specific
data is in the intersection of that user's home "directory" and the
application container; the application loads the first "foo.cfg" it
sees that is in this instance's user's home directory and in its own
container; or loads all of them, with the possibility that an
arbitrary one determines particular settings that are set different
ways in different copies. Users who want specifically predictable
behavior should of course make sure only one such file exists in their
own home "directory". File permissions are also made hugely simpler
with a database backing the filesystem that can encode elaborate
permissions. An associative logical filesystem makes permissions
easily based simply on a file being in a (descendant of a) particular
container or not. One for each user, with sub-containers for write or
execute; a world one; one for files the web server makes public; etc.;
user containers put in further containers can create overlapping user
groups which might have associated permissions managed in a similar
way. Applications can get their permissions controlled likewise.
Perhaps putting the application container inside a user container
makes it setuid that user; perhaps a link can be put that makes it so
only when launched via that link. The link itself can be made world
readable or whatever by also including it in other containers. Other
containers might sandbox an application placed inside, restricting it
to sub-normal user privileges. This might be useful for new, untested,
or suspicious stuff, or anything pending a virus-scan in case someone
launches it before someone else gets around to testing it. They might
be emulated, at a steep cost in performance, and only able to "infect"
the temporary VM in the emulator, so the infection goes away when the
emulator is terminated. Only files and containers instanced under the
sandbox appear in the VM emulation, and become potentially vulnerable
to malicious code deleting or modifying them. Indeed, malicious code
may only be able to delete the reference in the sandbox, leaving the
file intact if it is still referenced elsewhere as well, and modifying
a file with multiple references may by default have "copy-on-write"
semantics. Wipe the sandbox and any infection goes away, and there can
be no data loss.

In light of capabilities like these, and consistency/transactional
integrity/scalability advantages, filesystems like this seem
inevitable; it's only a matter of time and research.

Then loading classes from a database will be routine, and loading
classes NOT from a database will be unusual!
Andrew Thompson - 12 Jul 2007 08:51 GMT
> Andrew T. writ:
(code, jars, ..stuff)
>> ...may be stored as a (whadda'ya'callit?) BLOB in the
>> DB.
>
>Some databases can in effect be accessed via a URL. Those backing Web
>servers come to mind. :)

URL's are wonderful things, for getting at resources.  :-)
...
>Another thing to note is that the next generation of filesystems is
>likely to be built on full-blown ACID database logic, ...

"Atomicity, Consistency, Isolation and Durability"?
Sounds good to me - and a lot more than my current
FS offers.
...
>Regardless, I can tell you right now that even the venerable NTFS is
>showing age and creaking under modern loads of millions of files,
>hundreds of gigs, and really long paths and file names to keep all
>those millions straight. ...

As someone who has lost entire NTFS filesystem(s),
I actually cringe as I read that.
...
>In light of capabilities like these, and consistency/transactional
>integrity/scalability advantages, filesystems like this seem
>inevitable; it's only a matter of time ..

Time?!  It's over-bloody-due, if you ask me!

> ...and research.
...
>Then loading classes from a database will be routine, and loading
>classes NOT from a database will be unusual!

Give me a full RDB file system, with those
qualities you mentioned above, and I'll be
happy ( at least with the FS ;).

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Tom Hawtin - 12 Jul 2007 14:33 GMT
> [...]
>>> It is not clear where the bytes forming the class file are.  If live
>>> in a database for example, you won't be able to get at them via an
>>> URL.

> In fact, on a Windows system a custom URL handler that accesses a
> database can be written and installed. I'm not sure about Unix systems
> (including MacOS X) but they probably have some sort of system-wide
> URL handler capability too, at least if they have decent modern window
> managers (as MacOS X presumably does).

Which (Sun) Java will completely ignore, AFAIK.

But what you can do is supply a custom java.net.URLStreamHandler to the
java.net.URL constructor and pass that onto the java.net.URLClassLoader.
Implement URLStreamHandler.openConnection and a couple URLConnection
methods, and Bob is your mother's brother (in theory).

Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.