[RESOLVED] dynamically creating an instance of a class at runtime, experts needed
I tried to play around with this, after reading http://java.sun.com/developer/techni...ion/index.html
but I have a problem:
Code:
import java.lang.reflect.*;
public class Test {
public static void main(String args[]) {
try {
Class c = Class.forName("C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\eg\\submissions\\msk9\\ClassTest.class");
Method m[] = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++)
System.out.println(m[i].toString());
}
catch (Throwable e) {
System.err.println(e);
}
}
}
java.lang.ClassNotFoundException: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\eg\submissions\msk9\ClassTest.class
but the .class file is definetely there..
Re: dynamically creating an instance of a class at runtime, experts needed
A class path is not a name, you must add the path to the class path when compiling your code and use Class.forName("Qualified name");
Re: dynamically creating an instance of a class at runtime, experts needed
i'm now using interfaces so i know what method each submitted computer players use.
ComputerPlayerInterface.java
Code:
import java.io.*;
public interface ComputerPlayerInterface {
// Method signatures, which all submitted computer players must have.
String getUsername();
}
TestPlayer.java
Code:
import java.io.*;
public class TestPlayer implements ComputerPlayerInterface{
public String getUsername() {
return "Tom";
}
}
Test.java
Code:
public class Test {
public static void main(String args[]) {
try {
Class c = Class.forName("TestPlayer");
String name = c.getUsername();
System.out.println(name);
}
catch (Exception e) {
System.err.println(e);
}
}
}
i'm trying to get it to print Tom, but it just gives an error saying
Test.java:7: cannot find symbol
symbol : method getUsername()
location: class java.lang.Class
String name = c.getUsername();
^
1 error
computerJ, could you help me fix this? im very confused. the TestPlayer.class is in the same directory as the applicatiion, dont know where im going wrong
Re: dynamically creating an instance of a class at runtime, experts needed
This is how you should do it
Code:
public class Test
{
public static void main(String[] args) throws Exception
{
Class<?> c = Class.forName("TestPlayer");
Object o = c.newInstance();
String x = (String) c.getMethod("getUsername", (Class[]) null).invoke(o, (Object[])null);
System.out.println(x);
}
}
Re: dynamically creating an instance of a class at runtime, experts needed
Quote:
Originally Posted by ComputerJy
This is how you should do it
Code:
public class Test
{
public static void main(String[] args) throws Exception
{
Class<?> c = Class.forName("TestPlayer");
Object o = c.newInstance();
String x = (String) c.getMethod("getUsername", (Class[]) null).invoke(o, (Object[])null);
System.out.println(x);
}
}
thanks computerjy, its perfect. if the getUsername took parameters, what would i change?
and also, is there a way to make the coding look a bit more easy on the eye. like for e.g, some way to dynamically create the instance and then just do something like
String name = theclassinstance.getUsername();
just like we would normally do when dealing with classes?
Re: dynamically creating an instance of a class at runtime, experts needed
A direct way:
Code:
public class Test
{
public static void main(String[] args) throws Exception
{
Class<?> c = Class.forName("TestPlayer");
ComputerPlayerInterface o = (ComputerPlayerInterface) c.newInstance();
String x = o.getUsername();
System.out.println(x);
}
}
And for example if your TestPlayer class had the method getNumber that converts an integer to string the call would look like
Code:
public class Test
{
public static void main(String[] args) throws Exception
{
Class<?> c = Class.forName("TestPlayer");
Object o = c.newInstance();
String x = (String) c.getMethod("getNumber", new Class[] { Integer.class }).invoke(o,
new Object[] { Integer.valueOf(3) });
System.out.println(x);
}
}
And you must make sure that the the parameter in the method getNumber is Integer not int
Re: dynamically creating an instance of a class at runtime, experts needed
Quote:
Originally Posted by ComputerJy
A direct way:
Code:
public class Test
{
public static void main(String[] args) throws Exception
{
Class<?> c = Class.forName("TestPlayer");
ComputerPlayerInterface o = (ComputerPlayerInterface) c.newInstance();
String x = o.getUsername();
System.out.println(x);
}
}
absolutely perfect. thanks very much buddy.
also do you know anything about production code to check that the object instance does actually implement the interface?
Re: dynamically creating an instance of a class at runtime, experts needed
Try this method:
Code:
public class Test
{
public static void main(String[] args) throws Exception
{
Class<?> c = Class.forName("TestPlayer");
for (int i = 0; i < c.getInterfaces().length; i++)
{
if (c.getInterfaces()[i].getName().endsWith("ComputerPlayerInterface"))
{
System.out.println("Correct");
return;
}
}
System.out.println("Invalid class");
}
}
Re: dynamically creating an instance of a class at runtime, experts needed
awesome, thanks very much. you sure are great computerJy