1

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.

Sanyam Goel
  • 2,138
  • 22
  • 40
  • You can start a new JVM,of course. That would probably be a new process. But I think that's not what you want. What about threads? Why a process? – Thomas Uhrig Jul 18 '12 at 18:34
  • @ThomasUhrig Yes indeed you are right but then I would like you to go through my already posted problem http://stackoverflow.com/questions/11300700/how-to-prevent-jframe-from-closing/11306236#11306236 – Sanyam Goel Jul 18 '12 at 18:36
  • ah...! and now you want to know how to start a new JVM? Something like Runtime.getRuntime().exec("java -jar yourApp.jar"); should work, I think. – Thomas Uhrig Jul 18 '12 at 18:43
  • @ThomasUhrig No but this would not solve my problem. I can only run target application through reflection and not through jar invoke and I do not see any way of passing statement "m.invoke(param1 ,param2)" to exec() method. – Sanyam Goel Jul 18 '12 at 18:47

1 Answers1

5

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"
Puspendu Banerjee
  • 2,631
  • 16
  • 19
  • But this is where the problem emerges. What command should be passed to the instance of process builder to execute the main method. – Sanyam Goel Jul 18 '12 at 18:52
  • when you execute a jar file (or even a single class), the JVM will always execute main method. – Thomas Uhrig Jul 18 '12 at 19:12
  • say we have a class Foo.class (which has a static main method) and it is in the jar file bar.jar. Then to execute the jar using the static main method present in Foo class use "java -cp bar.jar Foo" as command. I assumed that java executable in there in your PATH environment variable – Puspendu Banerjee Jul 18 '12 at 21:22
  • @sand1988 let me know if that solves your problem. +1 on your question. – Puspendu Banerjee Jul 18 '12 at 21:28
  • @PuspenduBanerjee yes this is fine but my limitation is I cannot execute the application from jar All I can do is to read contents of jar and then execute it using reflection and then the problem arises.. The other problem faced is http://stackoverflow.com/questions/11300700/how-to-prevent-jframe-from-closing/11306236#11306236 So the only solution I could see is to invoke the application in new pocess. Let me sum up everything: 1) I have a java application that loads a executable jar file 2) Using reflection it invokes main of the jar 3) Now if the target application contains Frames – Sanyam Goel Jul 19 '12 at 06:11
  • continued above comment ... my application also closes on closing main frame if any of the target jar file. Although dispose on close is a solution but I cannot change the contents of target jar to be invoked. Now is there a solution meeting all my requirements – Sanyam Goel Jul 19 '12 at 06:18