Results 1 to 13 of 13
  1. #1
    luckyy is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Thumbs down Am having trouble to calculate number of methods in class

    This is the code i have tried to count total number of public methods declared in any class entered as input to the class.

    At compilation its giving no error but when i run it displaying ClassNotFoundException ...

    Some body please fix my code please i waste enough time and final came to conclusion that i need some assistance from an expert programmer..

    help me


    import java.lang.*;
    import java.io.*;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;

    public class NumberOfMethods{
    public static void main(String args[]) {

    try {
    System.out.println("Getting line number of a paritcular file example!");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Please enter file name with extension:");

    String str = bf.readLine();
    Class cls = Class.forName(str);
    Method methods[] = cls.getDeclaredMethods();

    System.out.println("Total number of methods in the class are "+methods.length);
    System.out.println("Public Methods:");

    for (int i = 0; i < methods.length; i++) {
    int modifiers = methods[i].getModifiers();
    if (Modifier.isPublic(modifiers)) {
    System.out.println(" " + methods[i].getName());
    }
    }
    } catch (Exception e) {
    System.out.println("Exception: " + e);
    }
    }
    }

    anxiously waiting for reply

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by luckyy View Post
    anxiously waiting for reply

    the exception is thrown from this line of code

    Java Code:
    Class cls = Class.forName(str);

    the printStackTrace() is

    Java Code:
    java.lang.ClassNotFoundException: D:\Documents and Settings\t120468\workspace\ComboBox\src\ComboBoxExample.java
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Unknown Source)
    	at myjava.examples.org.NumberOfMethods.main(NumberOfMethods.java:21)
    i couldn't figure out what's wrong.

  3. #3
    luckyy is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank you.. i analyzed that i couldn't find the error to fix.
    One of friend suggested me Factory Method of design patterns
    Can some one with the knowledge on factory method can help me to fix the code...

    Experienced java developers am looking for your help...

    Thank you...

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    What you are passing the Class.forName() is incorrect.

    Instead of passing it the entire PATH to the file, pass it the entire qualified NAME of the file.



    For example,
    I have a a project named PrintDrivers

    Inside PrintDrivers, there is a class at com.printdrivers.pdf called TextField.(This means the class resides inside folder "pdf", which is in folder "printdrivers", which is in folder "com")

    If I wanted to obtain that class using the Class.forName method, I would need to pass it "com.cybra.printdrivers.pdf.TextField" as the parameter.


    I hope this helps

  5. #5
    luckyy is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    What you are passing the Class.forName() is incorrect.

    Instead of passing it the entire PATH to the file, pass it the entire qualified NAME of the file.



    For example,
    I have a a project named PrintDrivers

    Inside PrintDrivers, there is a class at com.printdrivers.pdf called TextField.(This means the class resides inside folder "pdf", which is in folder "printdrivers", which is in folder "com")

    If I wanted to obtain that class using the Class.forName method, I would need to pass it "com.cybra.printdrivers.pdf.TextField" as the parameter.


    I hope this helps
    Thank you for having a look at my problem...
    i tried ur solution but still am getting the same error...

    That is a full working code ..

    Just give a try...
    i didn't use any IDE i have the habbit of doing in notepad and compile and run in CMD ... try like that or the other way in IDE

  6. #6
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    I'm guessing since you are programming through notepad the class does not exist inside a package. So the parameter should just be the name of the class. There should be no .java or .class.
    Last edited by StormyWaters; 04-15-2010 at 08:58 PM. Reason: clarity

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Actually looking at your code again you are asking for the extension. This is not part of the fully qualified name of the class.

    If you are still experiencing problems with this, please provide the location of the two classes and what you are entering in for the fully qualified name.
    Last edited by StormyWaters; 04-15-2010 at 09:00 PM. Reason: clarity

  8. #8
    luckyy is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    Actually looking at your code again you are asking for the extension. This is not part of the fully qualified name of the class.

    If you are still experiencing problems with this, please provide the location of the two classes and what you are entering in for the fully qualified name.
    Both the classes are in forlder C:\Java Practice\
    I mean both the files are in same folder...

  9. #9
    luckyy is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    Actually looking at your code again you are asking for the extension. This is not part of the fully qualified name of the class.

    If you are still experiencing problems with this, please provide the location of the two classes and what you are entering in for the fully qualified name.
    Hello Stormy Waters i have one more Q related to this only...

    String str = ExampleProgram.java; ----1
    1) If am suppose to display as well count all the Public Methods from the above class "ExampleProgram.java" using "getDeclaredMethods()", i need an object to class "ExampleProgram.java", how do i generate object for that class is it like

    String str = ExampleProgram.java;
    str ep = new str();

    now will 'ep' be object of ExampleProgram.java..

    help me someone plz...


    AM a student i have to submit Program Analyzer application for JAVA source file by generating McCabe Complexity and HalStead Complexity, also Number of Lines of Code, Methods, Attributes, unique Operands and total Operands, unique operators and total Operators.... am done everything and struck at this small point...

    When i asked professor for help he asked me to generate the object using Factory Methods of Design Patterns, if some body had any idea about Factory Methods please help me to "generate Object of class that's there in String variable"

    Waiting for help

  10. #10
    luckyy is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    C:\Java Practice>set CLASSPATH=C:\Java Practice

    C:\Java Practice>echo %CLASSPATH%
    C:\Java Practice

    C:\Java Practice>javac NumberOfMethods.java

    C:\Java Practice>java NumberOfMethods
    Getting line number of a paritcular file example!
    Please enter file name with extension:
    Factorial4.java
    Exception: java.lang.ClassNotFoundException: Factorial4.java

    C:\Java Practice>java NumberOfMethods
    Getting line number of a paritcular file example!
    Please enter file name with extension:
    ExampleProgram.java
    Exception: java.lang.ClassNotFoundException: ExampleProgram.java

    C:\Java Practice>echo %CLASSPATH%
    C:\Java Practice

    C:\Java Practice>set CLASSPATH=C:\Java Practice\

    C:\Java Practice>echo %CLASSPATH%
    C:\Java Practice\

    C:\Java Practice>java NumberOfMethods
    Getting line number of a paritcular file example!
    Please enter file name with extension:
    Factorial4.java
    Exception: java.lang.ClassNotFoundException: Factorial4.java

    C:\Java Practice>java NumberOfMethods
    Getting line number of a paritcular file example!
    Please enter file name with extension:
    Factorial4.java
    Exception: java.lang.ClassNotFoundException: Factorial4.java

    i tried with the suggestions but still am having the same problem

    please help me some one..

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Cross-post: Java Programming - Am having trouble to calculate number of methods in class

    To the original poster, welcome to the forum, and I hope your question gets answered, but I do request that if you cross-post a question you provide links to the other cross-posts. Nobody likes duplicating work that's already been done. So, that being said, has this been cross-posted anywhere else?


    Thanks for your cooperation.

  12. #12
    luckyy is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    am sorry for posting here and in sun forums...

    i will not repeat

  13. #13
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Why are you doing .java... I have stated this is not part of the fully qualified name. Just use the name of the class. So you'd type in Factorial4 not Factorial4.java.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  3. Trouble with Random number generation
    By SteroidalPsycho in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 11:15 PM
  4. trouble with a class
    By Alexander Montero in forum New To Java
    Replies: 2
    Last Post: 06-17-2009, 10:01 PM
  5. Need help with calculate the number of seconds
    By ProUnbeatable in forum New To Java
    Replies: 5
    Last Post: 10-11-2008, 03:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •