|
-
Jul 1st, 2008, 08:17 PM
#1
Thread Starter
Frenzied Member
[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..
-
Jul 2nd, 2008, 01:50 AM
#2
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");
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 2nd, 2008, 05:24 AM
#3
Thread Starter
Frenzied Member
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
Last edited by Pouncer; Jul 2nd, 2008 at 05:49 AM.
-
Jul 2nd, 2008, 06:08 AM
#4
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);
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 2nd, 2008, 07:33 AM
#5
Thread Starter
Frenzied Member
Re: dynamically creating an instance of a class at runtime, experts needed
 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?
Last edited by Pouncer; Jul 2nd, 2008 at 07:42 AM.
-
Jul 2nd, 2008, 08:14 AM
#6
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
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 2nd, 2008, 09:15 AM
#7
Thread Starter
Frenzied Member
Re: dynamically creating an instance of a class at runtime, experts needed
 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?
-
Jul 2nd, 2008, 01:48 PM
#8
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");
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 2nd, 2008, 02:46 PM
#9
Thread Starter
Frenzied Member
Re: dynamically creating an instance of a class at runtime, experts needed
awesome, thanks very much. you sure are great computerJy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|