Reflection on Jar NOT in Classpath?

R

RVince

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.
 
T

Tom Hawtin

RVince said:
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
 
A

Andrew Thompson

Roedy said:
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.

"..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.

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
 
L

Lew

Andrew said:
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.

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.
 
R

Roedy Green

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.
 
A

Andrew Thompson

Roedy said:
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.
 
T

Twisted

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.

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!
 
A

Andrew Thompson

Twisted said:
Andrew T. writ: (code, jars, ..stuff)

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 ;).

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
 
T

Tom Hawtin

Twisted said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top