Sign in to follow this  

[java] Loading a custom class

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I load a custom class? Say Foo.class, with an array of integers. I was hoping there would be a simple way to do this. I tried URLClassLoader, but it gives me tons of errors. It seems more then what I need anyways, there should be an easy way to do this. kinda like this but evaluate the string "Foo" foo = new "Foo"(); I want to be able to load variable name classes. Is this possible? Practical? (For Maps in a rpg for example)

Share this post


Link to post
Share on other sites
You need a ClassLoader when the class hasn't been loaded yet. If the class is already loaded, you could use something like this...

[source=java]
try {
Class yourClass = Class.forName("somepackage.YourClass");

Class[] contstructorParamTypes =
{String.class, int.class, SomethingElse.class};

Object[] constructorParams = new Object[3];
constructorParams[0] = "First param";
constructorParams[1] = new Integer(23);
constructorParams[2] = new SomethingElse();

Constructor constructor =
yourClass.getConstructor(contstructorParamTypes);

Object instance = constructor.newInstance(constructorParams);
}

Share this post


Link to post
Share on other sites
It depends on one thing: if your class already resides in the classpath of your running application, or not; If the former, RayNbow's approach is the way to go, it works just fine, using what's called "reflection". If the latter is the case, you will need a custom class loader, usually a URLClassLoader. That is how you would add plugin jars on the fly to your application, for example.

So, if your maps are stored "outside" the loaded classpath of your program, you would have to load the classes manually using the URLClassLoader:


/**
*
*/

private static URLClassLoader createClassLoader() {
URL[] urls = null;
try {

File dir = new File(DIRECTORY);
String[] files = dir.list(new JARFilenameFilter());

urls = new URL[ files.length ];
for (int x = 0; x < files.length; x++) {
File file = new File(DIRECTORY + files[x]);
urls[x] = file.toURL();
}

} catch (Exception e) {
e.printStackTrace();
}
return new URLClassLoader(urls);
}

/**
* FilenameFilter used for a JAR file
*/

private static final class JARFilenameFilter implements FilenameFilter {
public boolean accept(File file, String str) {
return ( (!file.isDirectory()) || str.endsWith(".jar"));
}
}



Son Of Cain

Share this post


Link to post
Share on other sites

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this