1

I want to load a .java file from a specified path and retrieve the methods declared in it. Please find below the sample.

There is a java file in a location D:/Sample/Test.java

I have declared two methods in it test1() and test2().`

I know that by using classobj.getClass().getDeclaredMethods() I can retrieve the methods declared in it. But my problem is in loading the .java file.

Could anyone please provide me a solution for this..

Regards, Adarsh K S

Rohan
  • 3,068
  • 1
  • 20
  • 26
Adarsh
  • 177
  • 1
  • 13
  • You can't load .java files, they have to be compiled to .class files first. – CDahn Jun 13 '14 at 05:50
  • Consider having a look at this [answer](http://stackoverflow.com/questions/21544446/how-do-you-dynamically-compile-and-load-external-java-classes/21544850#21544850) which demonstrates the dynamic compliation of a `.java` file and loading of the resulting class file – MadProgrammer Jun 13 '14 at 05:53

1 Answers1

3

First you need to compile that java class

then you can use URLClassLoader to load class from specific location and then you can invoke getDeclaredMethods()

jmj
  • 237,923
  • 42
  • 401
  • 438