ClassLoaders and finding fully-qualified names

B

ballpointpenthief

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
 
A

Andrew Thompson

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

ballpointpenthief

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

Andrew Thompson

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

Thomas Hawtin

ballpointpenthief said:
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
 
J

John Ersatznom

Andrew said:
ballpointpenthief wrote:
..


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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

ballpointpenthief said:
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
 
K

Karl Uppiano

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

Tom Hawtin

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

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top