Java – How to call a java program from another java program?

java

Possible Duplicate:
how to compile & run java program in another java program?

eg if i have A.java and B.java then i want to compile and run B.java using A.java.

Best Answer

First, compile your code. I do not think you really want to compile class B from class A as you have written. This almost does not make any sense.

Now, since both are java classes you can just call methods of one class from another directly. If however your really mean that 2 classes are independent programs, so that each one has its own main method you can run one application from another using either Runtime.getRuntime().exec(...) or using ProcessBuilder.

Please pay attention on words really I wrote. I am pretty sure you do not want to call one java program from another. Most chances are that you want to call methods of one class from another, so do this.