Search code examples
javareflectionprocessbuilder

Is it possible in Java to invoke main method in new process with reflection


Is it possible to execute a statement such as m.invoke(param1 ,param2) in a new Process.

i.e., I want to execute a Java class file by first iteratively searching for its main class using reflection and on finding the main class to invoke it in a new process.


Solution

  • Definitely.

    1. Find all available classes in Path
    2. Introspect and find the class which has main method[check signature]
    3. Use java ProcessBuilder[JRE 5 +] or create a new java.lang.Process to spawn the new process it should "just work"