Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > URLClassloader loading class with unknown package name

Reply
Thread Tools

URLClassloader loading class with unknown package name

 
 
Cory
Guest
Posts: n/a
 
      10-25-2003
I have a directory full of classes, all that implement a common
interface. All of the class files have different package names that I
do not know.

My question is, how do I call the URLClassLoader.loadClass() method to
instantiate a new instance if I don't know the package name?

ex: All I know is that the file's name is MyClass.class.

package com.hey.now;

public class MyClass implements someInterface {
....
}

I just want to say Object o =
(someInterface)myLoader.loadClass(myClass);

any ideas??

Thanks in advance!
 
Reply With Quote
 
 
 
 
David Zimmerman
Guest
Posts: n/a
 
      10-25-2003


Cory wrote:
> I have a directory full of classes, all that implement a common
> interface. All of the class files have different package names that I
> do not know.
>


How can they be in different packages if they're in the same directory?

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      10-25-2003
Drop Java and migrate on C++

No package = no class
Moreover, package out of default classLoader = no package in most cases



Cory <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have a directory full of classes, all that implement a common
> interface. All of the class files have different package names that I
> do not know.
>
> My question is, how do I call the URLClassLoader.loadClass() method to
> instantiate a new instance if I don't know the package name?
>
> ex: All I know is that the file's name is MyClass.class.
>
> package com.hey.now;
>
> public class MyClass implements someInterface {
> ...
> }
>
> I just want to say Object o =
> (someInterface)myLoader.loadClass(myClass);
>
> any ideas??
>
> Thanks in advance!



 
Reply With Quote
 
Michiel Konstapel
Guest
Posts: n/a
 
      10-26-2003
"Cory" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have a directory full of classes, all that implement a common
> interface. All of the class files have different package names that I
> do not know.
>
> My question is, how do I call the URLClassLoader.loadClass() method to
> instantiate a new instance if I don't know the package name?
>
> ex: All I know is that the file's name is MyClass.class.
>
> package com.hey.now;
>
> public class MyClass implements someInterface {
> ...
> }
>
> I just want to say Object o =
> (someInterface)myLoader.loadClass(myClass);
>
> any ideas??
>
> Thanks in advance!


Basically, if you don't know the package, you don't know the class name,
period. The package is an intrinsic part of the name. Now, you should be
able to parse the full name from the class file, perhaps using BCEL or by
looking at the bytes yourself. A standard URLClassLoader won't load them for
you, though, since it expects a class foo.bar.Baz in a directory structure
matching the package name: <base url>/foo/bar/Baz.class. A custom class
loader should do the job, though.
HTH,
Michiel


 
Reply With Quote
 
Cory
Guest
Posts: n/a
 
      10-26-2003
David Zimmerman <(E-Mail Removed)> wrote in message news:<fjDmb.6614$(E-Mail Removed) om>...
> Cory wrote:
> > I have a directory full of classes, all that implement a common
> > interface. All of the class files have different package names that I
> > do not know.
> >

>
> How can they be in different packages if they're in the same directory?


You can easily take a class file (someFile.class) out of a directory
and put it into another.... Is this what you mean?
 
Reply With Quote
 
Cory
Guest
Posts: n/a
 
      10-26-2003
"VK" <(E-Mail Removed)> wrote in message news:<3f9b0251$0$12869$(E-Mail Removed)>.. .
> Drop Java and migrate on C++
>
> No package = no class
> Moreover, package out of default classLoader = no package in most cases
>
>
>
> Cory <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I have a directory full of classes, all that implement a common
> > interface. All of the class files have different package names that I
> > do not know.
> >
> > My question is, how do I call the URLClassLoader.loadClass() method to
> > instantiate a new instance if I don't know the package name?
> >
> > ex: All I know is that the file's name is MyClass.class.
> >
> > package com.hey.now;
> >
> > public class MyClass implements someInterface {
> > ...
> > }
> >
> > I just want to say Object o =
> > (someInterface)myLoader.loadClass(myClass);
> >
> > any ideas??
> >
> > Thanks in advance!



I don't think you understand my question. I have third party class
files that belong to certain third party packages. Here's an example
of the first class file:
---------------------------------

package com.hey.now;

public class MyClass implements someInterface {

public MyClass() {
System.out.println("Created a new MyClass object.");
}

public void init() {
System.out.println("Init called");
}
}
---------------------------------

Now in my code I want to be able to load this class and call init() on
the object. Here's the code I would use to do it if I knew the package
name for MyClass:

---------------------------------

private Class fileToClass(File file) throws IOException {
int magic = 0xCAFEBABE;
String name = file.getName();
Class retClass = null;

if(!file.exists() || !name.endsWith(".class")) {
return null;
}

DataInputStream dataIn = new DataInputStream(
new FileInputStream(file));
if(dataIn.readInt() == magic) {
//Now we know it's a Java CLASS file
URL[] url = new URL[1];
url[0] = file.toURL();

URLClassLoader urlLoader = new URLClassLoader(url);

try {
retClass = urlLoader.loadClass("com.hey.now.MyClass"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

// TODO Auto-generated method stub
return retClass;
}
---------------------------------

Now the only thing I don't know is that 'com.hey.now' is the package
name for this class....

If you can load the class in some other way, I would love to know.

PS. Doing this in C++ would be murder
 
Reply With Quote
 
Cory
Guest
Posts: n/a
 
      10-27-2003
"Michiel Konstapel" <(E-Mail Removed)> wrote in message news:<ZRXmb.28597$n03.1436207@zonnet-reader-1>...
> "Cory" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I have a directory full of classes, all that implement a common
> > interface. All of the class files have different package names that I
> > do not know.
> >
> > My question is, how do I call the URLClassLoader.loadClass() method to
> > instantiate a new instance if I don't know the package name?
> >
> > ex: All I know is that the file's name is MyClass.class.
> >
> > package com.hey.now;
> >
> > public class MyClass implements someInterface {
> > ...
> > }
> >
> > I just want to say Object o =
> > (someInterface)myLoader.loadClass(myClass);
> >
> > any ideas??
> >
> > Thanks in advance!

>
> Basically, if you don't know the package, you don't know the class name,
> period. The package is an intrinsic part of the name. Now, you should be
> able to parse the full name from the class file, perhaps using BCEL or by
> looking at the bytes yourself. A standard URLClassLoader won't load them for
> you, though, since it expects a class foo.bar.Baz in a directory structure
> matching the package name: <base url>/foo/bar/Baz.class. A custom class
> loader should do the job, though.
> HTH,
> Michiel




I would like to be able to read in the classname bytes themselves...
but I haven't found the specs that tell me where these bytes are
stored however. Where should I look for this?

Thanks,
Cory
 
Reply With Quote
 
Anton Spaans
Guest
Posts: n/a
 
      10-27-2003
Hi Cory,

Here is your example code:
===========================
private Class fileToClass(File file) throws IOException {
int magic = 0xCAFEBABE;
String name = file.getName();
Class retClass = null;

if(!file.exists() || !name.endsWith(".class")) {
return null;
}

DataInputStream dataIn = new DataInputStream(
new FileInputStream(file));
if(dataIn.readInt() == magic) {
//Now we know it's a Java CLASS file
URL[] url = new URL[1];
url[0] = file.toURL();

URLClassLoader urlLoader = new URLClassLoader(url);

try {
retClass = urlLoader.loadClass("com.hey.now.MyClass"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

// TODO Auto-generated method stub
return retClass;
}
===============
First of all, the URLClassLoader constructor takes an array of URLs, each
URL describing a *class-path* definition. This means that URLs (as strings)
such as "java/lang/String.class" is not accepted. This and your example URL
refers to a *class*, not a class-path. A valid URL-example would be
"D:/MyProjects/Project1/classes" or "D:/MyLibs/lib1.jar".

In your code, you have the variable 'file'. Call 'file.getCanonicalPath()'
to get a fully qualified path-definition of your file. Then figure out what
part of the returned string is the class-path definition. The tail
(remainder) is your class definition:

E.g. Suppose file.getCanonicalPath() returns
"D:/MyProjects/Project1/classes/com/hey/now/MyClass.class". Then you need to
figure somehow out that "D:/MyProjects/Project1/classes" is the class-path
definition you want. Then remove "D:/MyProjects/Project1/classes" form
"D:/MyProjects/Project1/classes/com/hey/now/MyClass.class". This will result
in this string: "com/hey/now/MyClass.class". Then translate all '/' (or '\')
into dots '.' and remove the ".class" post-fix: "com.hey.now.MyClass".

The tricky part here is how to figure out the class-path definition
"D:/MyProjects/Project1/classes". But as soon as you have this class-path
definition, use it in the constructor of URLClassLoader:

String classPath = ..... // "D:/MyProjects/Project1/classes"
String className = .... // "com.hey.now.MyClass"
...
urlLoader = new URLClassLoader(new URL[] { new URL(classPath) });
...
retClass = urlLoader.loadClass(className);

Hopes this helps!
-- Anton.



 
Reply With Quote
 
Cory
Guest
Posts: n/a
 
      10-28-2003
"Michiel Konstapel" <(E-Mail Removed)> wrote in message news:<ZRXmb.28597$n03.1436207@zonnet-reader-1>...
> "Cory" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > I have a directory full of classes, all that implement a common
> > interface. All of the class files have different package names that I
> > do not know.
> >
> > My question is, how do I call the URLClassLoader.loadClass() method to
> > instantiate a new instance if I don't know the package name?
> >
> > ex: All I know is that the file's name is MyClass.class.
> >
> > package com.hey.now;
> >
> > public class MyClass implements someInterface {
> > ...
> > }
> >
> > I just want to say Object o =
> > (someInterface)myLoader.loadClass(myClass);
> >
> > any ideas??
> >
> > Thanks in advance!

>
> Basically, if you don't know the package, you don't know the class name,
> period. The package is an intrinsic part of the name. Now, you should be
> able to parse the full name from the class file, perhaps using BCEL or by
> looking at the bytes yourself. A standard URLClassLoader won't load them for
> you, though, since it expects a class foo.bar.Baz in a directory structure
> matching the package name: <base url>/foo/bar/Baz.class. A custom class
> loader should do the job, though.
> HTH,
> Michiel



Ah. I found/dled and looked over BCEL. It seems a bit overkill for the
task but def. looks to be interesting for later ptojects. Do you have
any suggestions as far as BCEL tutorials go? There's aren't m/any that
I've found.

Thanks in advance,
-Cory
 
Reply With Quote
 
Cory
Guest
Posts: n/a
 
      10-28-2003
"Anton Spaans" <aspaans at(noSPAM) smarttime dot(noSPAM) com> wrote in message news:<(E-Mail Removed)>...
> Hi Cory,
>
> Here is your example code:
> ===========================
> private Class fileToClass(File file) throws IOException {
> int magic = 0xCAFEBABE;
> String name = file.getName();
> Class retClass = null;
>
> if(!file.exists() || !name.endsWith(".class")) {
> return null;
> }
>
> DataInputStream dataIn = new DataInputStream(
> new FileInputStream(file));
> if(dataIn.readInt() == magic) {
> //Now we know it's a Java CLASS file
> URL[] url = new URL[1];
> url[0] = file.toURL();
>
> URLClassLoader urlLoader = new URLClassLoader(url);
>
> try {
> retClass = urlLoader.loadClass("com.hey.now.MyClass"));
> } catch (ClassNotFoundException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> return null;
> }
> }
>
> // TODO Auto-generated method stub
> return retClass;
> }
> ===============
> First of all, the URLClassLoader constructor takes an array of URLs, each
> URL describing a *class-path* definition. This means that URLs (as strings)
> such as "java/lang/String.class" is not accepted. This and your example URL
> refers to a *class*, not a class-path. A valid URL-example would be
> "D:/MyProjects/Project1/classes" or "D:/MyLibs/lib1.jar".
>
> In your code, you have the variable 'file'. Call 'file.getCanonicalPath()'
> to get a fully qualified path-definition of your file. Then figure out what
> part of the returned string is the class-path definition. The tail
> (remainder) is your class definition:
>
> E.g. Suppose file.getCanonicalPath() returns
> "D:/MyProjects/Project1/classes/com/hey/now/MyClass.class". Then you need to
> figure somehow out that "D:/MyProjects/Project1/classes" is the class-path
> definition you want. Then remove "D:/MyProjects/Project1/classes" form
> "D:/MyProjects/Project1/classes/com/hey/now/MyClass.class". This will result
> in this string: "com/hey/now/MyClass.class". Then translate all '/' (or '\')
> into dots '.' and remove the ".class" post-fix: "com.hey.now.MyClass".
>
> The tricky part here is how to figure out the class-path definition
> "D:/MyProjects/Project1/classes". But as soon as you have this class-path
> definition, use it in the constructor of URLClassLoader:
>
> String classPath = ..... // "D:/MyProjects/Project1/classes"
> String className = .... // "com.hey.now.MyClass"
> ...
> urlLoader = new URLClassLoader(new URL[] { new URL(classPath) });
> ...
> retClass = urlLoader.loadClass(className);
>
> Hopes this helps!
> -- Anton.




Thanks for your reply Anton. I've used the code that loads the url
into the URLClassloader before and it seems to work ok. As for the
package name, I am not making the assumption that the class file is in
the correct directory. In fact, it could be in any directory. I'm
going to look into BCEL and getting the "this_class" entry in the
constant pool of the byte code in order to find the fully qualified
class name.

Thanks again,
Cory Virok
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
applet and URLClassLoader iras Java 0 08-09-2006 09:58 AM
URLClassLoader and Tomcat Romain Java 2 09-29-2005 10:08 AM
URLClassLoader slow in 1.4.2 Timothy Fosdike Java 0 08-26-2004 02:08 AM
help.! Java URLClassloader Sundar Java 1 11-04-2003 04:02 PM
URLClassLoader plugin system.... getting java.lang.NoClassDefFoundError netpro2k Java 2 08-21-2003 03:12 PM



Advertisments