File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HELP - dynamic creation of classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "HELP - dynamic creation of classes" Watch "HELP - dynamic creation of classes" New topic
Author

HELP - dynamic creation of classes

Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
Hi,
I'm trying to dynamically create classes. I have an interface called Algorithm in package "App.algorithms". There are algorithms in the same package implementing the interface. From a different package called "App.gui" I want to create a new object/instance of the algorithm with the name className. Here is part of the code:

I've tried setting the className to "App.algorithms.DijkstraShortestPath" and "DijkstraShortestPath" explicitly (where DijkstraShortestPath.java is a class in "App.algorithms") but it still fails on the second line. Am I missing something? Maybe I need to do something with ClassLoader? Please can you provide a bit of code. Thanks in advance.
Phil
Phil Lesh
Greenhorn

Joined: Oct 16, 2002
Posts: 17
Hi again,
The exception that the above code throws is the InstantiationException. How would I instantiate the class within the above code? Thanks again!
Phil
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
From the javadocs for newInstance ...
Throws:
IllegalAccessException - if the class or its nullary constructor is not accessible.
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
ExceptionInInitializerError - if the initialization provoked by this method fails.
SecurityException - if there is no permission to create a new instance.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Nullary is a new word to me. Get thee to a nullary? Does that just mean you have to have a no-parameter constructor? By the way, you have to have a no-parameter constructor.
If you must use a constructor with parameters, you have to get into reflection, and invoke it. Here's some code I have around:

See the whole project at Jim's Pages - Ooops. That zip is gone awol. I will try to remember to upload it at home tonite.
[ April 08, 2003: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Isn't a nullary a school where pointers go to learn how to be null? Kind of like a nunary...


Java API Documentation
The Java Tutorial
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
All of which means that you have to create an instance of one of the classes that implements the Algorithm interface and then cast it to the Algorithm type.
You can not create an instance of an interface directly.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
GeeCON Prague 2014
 
subject: HELP - dynamic creation of classes