Hi. I am trying to write a function that when given a class, will tell you the number of methods that class has. I am getting the java.lang.noclassfoundexception message. I read something about adding to the PATH but was unsure. I also tried using HelloWorld.class, but that did not work either. Any help would be much apreciated. Below is my code:

String test = "HelloWorld.java";
Class cls = Class.forName(test); 
int Mcount = cls.getDeclaredMethods().length;
System.out.println("Mcount = " + Mcount);

Do some reading on reflection - the means by which you can use RTTI (runtime type identification) methods to do what you want. Google is your friend...

tHelloWorld.java is the name of a source file, not a class. Assuming you have compiled HelloWorld, and the .class file is somewhere in your classpath, all you need to do is o change line 1 so the string just contains the class name.

hey i figured it out. It was the location of my file. I put in the correct path to the location. I was doing the path wrong originally. Anyways thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.