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