ClassLoaders and finding fully-qualified names

Discussion in 'Java' started by ballpointpenthief, Dec 29, 2006.

  1. 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
     
    ballpointpenthief, Dec 29, 2006
    #1
    1. Advertising

  2. ballpointpenthief wrote:
    ...
    > how can I find out that Hello is in 'mypackage' if all I have is the
    > File?


    File possibleParentPackage =
    classFile.getParentFile();

    Andrew T.
     
    Andrew Thompson, Dec 29, 2006
    #2
    1. Advertising

  3. Andrew Thompson wrote:
    > ballpointpenthief wrote:
    > ..
    > > how can I find out that [a class should be] in 'mypackage' if all I have is the
    > > File?

    >
    > File possibleParentPackage =
    > classFile.getParentFile();
    >
    > 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
     
    ballpointpenthief, Dec 29, 2006
    #3
  4. ballpointpenthief wrote:
    > Andrew Thompson wrote:
    > > ballpointpenthief wrote:
    > > ..
    > > > 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.
     
    Andrew Thompson, Dec 29, 2006
    #4
  5. ballpointpenthief wrote:
    >
    > 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
     
    Thomas Hawtin, Dec 29, 2006
    #5
  6. Andrew Thompson wrote:
    > ballpointpenthief wrote:
    > ..
    >
    >>how can I find out that Hello is in 'mypackage' if all I have is the
    >>File?

    >
    > 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.)
     
    John Ersatznom, Dec 29, 2006
    #6
  7. ballpointpenthief wrote:
    > 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?


    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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Dec 29, 2006
    #7
  8. ballpointpenthief

    Karl Uppiano Guest

    "ballpointpenthief" <> wrote in message
    news:...
    >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


    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.
     
    Karl Uppiano, Dec 29, 2006
    #8
  9. ballpointpenthief

    Tom Hawtin Guest

    Arne Vajhøj wrote:
    >
    > 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
     
    Tom Hawtin, Dec 30, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S.Marion
    Replies:
    2
    Views:
    498
    Roedy Green
    Mar 3, 2006
  2. Alexander Grigoriev
    Replies:
    0
    Views:
    393
    Alexander Grigoriev
    Sep 12, 2003
  3. Szabolcs Borsanyi

    Pointer to qualified poitner to qualified object

    Szabolcs Borsanyi, May 30, 2008, in forum: C Programming
    Replies:
    13
    Views:
    573
    Keith Thompson
    Jun 8, 2008
  4. winkerbean
    Replies:
    2
    Views:
    393
    winkerbean
    Oct 28, 2008
  5. steve
    Replies:
    1
    Views:
    129
Loading...

Share This Page