Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2007, 09:38 AM
Member
 
Join Date: Jul 2007
Posts: 8
snooze-g is on a distinguished road
My own ClassLoader didn't work.
Hi all.
I have a problem when I trying to get a instance of my TestClass object.
System get me a "cannot find symbol...".
I think that i have a problem with resolveClass method, as i know that method going to set Link to my class.
Please help.

Thanks Snooze-g.



Code:
import java.io.FileInputStream; import java.util.Hashtable; public class SimpleClassLoader extends ClassLoader { private Hashtable classes = new Hashtable(); public SimpleClassLoader() { } private byte getClassImplFromDataBase(String className)[] { System.out.println(" >>>>>> Fetching the implementation of " + className); byte result[]; try { FileInputStream fi = new FileInputStream("c:\\bindata\\" + className + ".class"); result = new byte[fi.available()]; fi.read(result); return result; } catch (Exception e) { return null; } } public Class loadClass(String className) throws ClassNotFoundException { return (loadClass(className, true)); } public synchronized Class loadClass(String className, boolean resolveIt) throws ClassNotFoundException { Class result; byte classData[]; System.out.println(" >>>>>> Load class : " + className); result = (Class) classes.get(className); if (result != null) { System.out.println(" >>>>>> returning cached result."); return result; } try { result = super.findSystemClass(className); System.out.println( " >>>>>> returning system class (in CLASSPATH)."); return result; } catch (ClassNotFoundException e) { System.out.println(" >>>>>> Not a system class."); } classData = getClassImplFromDataBase(className); if (classData == null) { throw new ClassNotFoundException(); } result = defineClass(className, classData, 0, classData.length); if (result == null) { throw new ClassFormatError(); } if (resolveIt) { resolveClass(result); } classes.put(className, result); System.out.println(" >>>>>> Returning newly loaded class."); return result; } public static void main(String[] args) { SimpleClassLoader simp = new SimpleClassLoader(); try { Class c = simp.loadClass("TestClass"); TestClass t = null; try { t = (TestClass) c.newInstance(); } catch (IllegalAccessException ex) { } catch (InstantiationException ex) { } System.out.println(t.toString()); System.out.println(c.getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }

Last edited by snooze-g : 07-17-2007 at 11:45 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2007, 12:12 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
This is a compiler array. The compiler complains because it can not find the class you want to use but i understand that you want to load that class dynamically without knowing its implementation details!

As far as i see, there is only one solution:

1. You will create an interface and include that interface to this project. Lets call it TestInterface.

2. The interface will have all the methods you want to call from your TestClass.

3. Your TestClass will implement this interface. (e.g. TestClass implements TestInterface)

4. Modify your code and cast your dynamically created class into TestInterface object (not TestClass object). Like this:

Code:
SimpleClassLoader simp = new SimpleClassLoader(); try { Class c = simp.loadClass("TestClass"); TestInterface t = null; try { t = (TestInterface) c.newInstance(); } catch (IllegalAccessException ex) { } catch (InstantiationException ex) { } System.out.println(t.toString()); System.out.println(c.getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); }
5. You will successfully compile your code in this case and use the methods you want to call from your project without knowing implementation details of TestClass.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Will this applet ever work? willemjav Java Applets 4 04-20-2008 06:40 PM
how would i get this to work...? deeadeed New To Java 6 12-06-2007 03:58 AM
what do I have to install to work with JSP boy22 JavaServer Pages (JSP) and JSTL 1 08-05-2007 05:08 AM
Program don't work baltimore New To Java 1 08-04-2007 10:51 PM
eclipse classloader problem sandor Eclipse 2 05-10-2007 04:26 PM


All times are GMT +3. The time now is 06:31 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org