Results 1 to 5 of 5

Thread: Eclipse can not import package

  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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?

  5. #5
    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.

Similar Threads

  1. Package format in Eee Note Package Manager
    By Sanju!Ekta in forum Portable Devices
    Replies: 6
    Last Post: 11-08-2011, 11:30 PM
  2. Need Help with Eclipse mp4 player
    By Shotgunn95 in forum Technology & Internet
    Replies: 1
    Last Post: 16-04-2010, 09:11 PM
  3. Import glut libraries in eclipse
    By New ID in forum Windows Software
    Replies: 5
    Last Post: 15-01-2010, 02:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,009,236.21198 seconds with 16 queries