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 / December 2006

Tip: Looking for answers? Try searching our database.

ClassLoaders and finding fully-qualified names

Thread view: 
ballpointpenthief - 29 Dec 2006 05:30 GMT
I haven't managed to find and information on this so far...

There exists a file for 'mypackage.Hello' that a user wants to load as
a Class in an application.

A user selects the Hello.class file from a JFileChooser.
The Hello.class file is sent to subclass of SecureClassLoader.
The ClassLoader needs the fully-qualified classname (mypackage.Hello)
to define the class (along with the bytecode, and the CodeSource),
so....

how can I find out that Hello is in 'mypackage' if all I have is the
File?

Thanks!
Matt
Andrew Thompson - 29 Dec 2006 06:24 GMT
..
> how can I find out that Hello is in 'mypackage' if all I have is the
> File?

 File possibleParentPackage =
     classFile.getParentFile();

Andrew T.
ballpointpenthief - 29 Dec 2006 14:31 GMT
> ..
> > how can I find out that [a class should be] in 'mypackage' if all I have is the
[quoted text clipped - 4 lines]
>
> Andrew T.

OK, so you're saying I can only guess.

Does anyone know where the package name is in the bytecode? Or even
better where I can find out the layout of the bytecode for any
implementation? I've just downloaded the language spec, but I haven't
found what I'm looking for.

This is just for an exercise, I realise this isn't a sensible
workaround.

Cheers,
Matt
Andrew Thompson - 29 Dec 2006 14:39 GMT
> > ..
> > > how can I find out that [a class should be] in 'mypackage' if all I have is the
> > > File?
> >
> >   File possibleParentPackage =
> >       classFile.getParentFile();
....
> OK, so you're saying I can only guess.

Heck no.  You can always 'try' it, no more guessing.

OTOH, unless this is a tool meant for development,
I doubt it is the correct way to go about things, and
even then, it is probably the incorrect way of going
about doing ..whatever this is supposed to achieve
for the end user.

Andrew T.
Thomas Hawtin - 29 Dec 2006 15:01 GMT
> Does anyone know where the package name is in the bytecode? Or even
> better where I can find out the layout of the bytecode for any
> implementation? I've just downloaded the language spec, but I haven't
> found what I'm looking for.

It's in the JVM spec, not the language spec.

http://java.sun.com/docs/books/vmspec/html/ClassFile.doc.html

The name of the class, like all other names, is specified by a reference
into the constant pool. You will therefore need to read the constant
pool. Note that the length of each constant pool entry varies with type.

Tom Hawtin
John Ersatznom - 29 Dec 2006 15:24 GMT
> ..
>
[quoted text clipped - 3 lines]
>   File possibleParentPackage =
>       classFile.getParentFile();

The class file itself surely encodes the fully qualified name of the
class somewhere inside itself. I'd google for "java class file format"
or similarly. (Someone recently had "helloworld.Main" fail to load when
loaded as "Main" from its own directory, but load when loaded as
"helloworld.Main" from one directory up, so it has to have some other
way besides dir structure to know what package a class in a .class file
is in; the obvious way is for it to be self-contained in the class file
proper.)
Arne Vajhøj - 29 Dec 2006 21:04 GMT
> A user selects the Hello.class file from a JFileChooser.
> The Hello.class file is sent to subclass of SecureClassLoader.
[quoted text clipped - 4 lines]
> how can I find out that Hello is in 'mypackage' if all I have is the
> File?

You can specify null to defineClass.

Here are a standalone example:

package december;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ClassnameFinder {
    private static byte[] load(String fnm) throws IOException {
        File f = new File(fnm);
        byte[] b = new byte[(int)f.length()];
        InputStream is = new FileInputStream(f);
        is.read(b);
        is.close();
        return b;
    }
    public static String getClassname(String fnm) throws IOException,
ClassNotFoundException {
        ClassnameFinderHelper help = new ClassnameFinderHelper();
        return help.getClassname(load(fnm));
    }
    public static void main(String[] args) throws Exception {
        System.out.println(getClassname("C:\\X.class"));
    }
}

class ClassnameFinderHelper extends ClassLoader {
    public String getClassname(byte[] bc) {
        return defineClass(null, bc, 0, bc.length).getName();
    }
}

Arne
Tom Hawtin - 30 Dec 2006 00:53 GMT
> You can specify null to defineClass.

Nice (so long as the code is trusted).

>         InputStream is = new FileInputStream(f);
>         is.read(b);

This does not necessarily read the entire class file. You should wrap
with DataInputStream and use readFully, or re-implement that yourself.

Tom Hawtin
Karl Uppiano - 29 Dec 2006 21:43 GMT
>I haven't managed to find and information on this so far...
>
[quoted text clipped - 12 lines]
> Thanks!
> Matt

Class loader behavior depends on the class loader implementation. You can
write a class loader to do whatever you want, within the constraints of the
class loader design patterns. Packages are typically anchored to the
directory hierarchy relative to the classpath directory. This is true for
classes lying "loose" in the file system, and for classes in a jar file.
Without the directory structure, you might have a problem.

It might be helpful to look here
(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html) and
here (http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLClassLoader.html)
to really understand class loaders. If you cannot find a class loader that
does what you need, you might be able to write one. People often confuse the
behavior of the default class loader with class loaders in general, or with
the Java spec itself.


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



©2008 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.