Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-03-2010
Member
 
Join Date: Mar 2010
Posts: 162
Eclipse can not import package

Hello,
I have a class in a package that has the name of each class in another package as a combobox, so I withdraw after selecting a user name string in my class. After that, I want to access a method (common to each class of another package).
Here is my part of the code
Code:
		String cnm = "Max"; / / An example of one of the names of classes
	    try {
	    	Class cl = Class.Class.forName(cnm);
optionsV = cl.ListOptions(); / / This method returns a vector (optionV)
	    } catch (Final Exception ex) {
	    	throw new Exception(Java Class not found: " + cnm);
	    }
The problem is that Eclipse is not importing the package , it gives me: The method ListOptions () is undefined for the type Class. If you guys have a solution for this then it would be great. Thanks in advance.
Reply With Quote
  #2  
Old 25-03-2010
Member
 
Join Date: Nov 2009
Posts: 446
Re: Eclipse can not import package

Hello,
Here are some tips for you
1. I suppose "ListOptions" is a static method, if not impossible to draw without making a new ().

2. Eclipse has nothing to do. It is the compiler that does not agree, for the simple reason that Java is not a dynamic language => requires that the method exists in the class that you use. Right now, your object "myClass" belongs to the class "Class" (class-based language). This does obviously not your personal method "ListOptions ()".
I think you can use the introspection to call the method.
Reply With Quote
  #3  
Old 25-03-2010
Member
 
Join Date: Nov 2009
Posts: 335
Re: Eclipse can not import package

Hello,
I agree with the above post and as said that introspection can solve this problem, here is what I am trying to explain
Code:
Use introspection to call the method.
Class cl = Class.Class.forName(ClassName);
Method mth = cl.getDeclaredMethod("ListOptions",null);
Option rtn = (Option) mth.invoke(null, null);
Hope this solution will help you. If you have any more queries in this topic then do post back.
Reply With Quote
  #4  
Old 25-03-2010
Member
 
Join Date: Dec 2009
Posts: 202
Re: Eclipse can not import package

Hello,
Even I have some what similar problem with my code. I have tried you solution, it works by cons have full eclipse of warning, however, all is between a try / catch.

Line 1: Class is a raw type. References to generic type Class <T> should be parametrized

Line 2: The argument of type null should explicitly be cast to Class [] for the invocation

Line 3: Type safety: Unchecked cast from Object to Option

Now my question is that is that this may cause problem later?
Reply With Quote
  #5  
Old 25-03-2010
Member
 
Join Date: Nov 2009
Posts: 330
Re: Eclipse can not import package

Hello,
Code:
Class <?> cl = Class.Class.forName(ClassName);
Method mmth = myClass.getDeclaredMethod("ListOptions",(Class[])null);
Option rtn = (Option) mmth.invoke(null, (Object[])null);
Ok, that should leave only the warning "Type safety. The key is that introspection, risks are present. A better solution is to forget the introspection and use standard OO patterns (strategy, factory, ...). I guess you are getting the point what I am trying to explain here.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Eclipse can not import package"
Thread Thread Starter Forum Replies Last Post
Package format in Eee Note Package Manager Sanju!Ekta Portable Devices 6 11-08-2011 11:30 PM
Need Help with Eclipse mp4 player Shotgunn95 Technology & Internet 1 16-04-2010 09:11 PM
Import glut libraries in eclipse New ID Windows Software 5 15-01-2010 02:10 PM


All times are GMT +5.5. The time now is 10:38 AM.