4

I have two programs and I have to call a program which is in different location i.e., say calling program is in d://start and called program is in f://call. How to do it in java?

Can I use this method to be implemented in calling program?

try
{
    Process p = Runtime.getRuntime().exec(
       new String[] {"cmd.exe", "/c", "F:/call.java"});

    InputStream in = p.getInputStream();
    OutputStream out = p.outputStream();
}

catch (IOException e)
{
  e.printStackTrace();
}
Jonathan
  • 20,053
  • 6
  • 63
  • 70
Murali
  • 774
  • 6
  • 12
  • @NandkumarTekale I have two programs. one is GUI and another one application to rip words in java. I have to call the class which contains the main method when I click the button from the GUI program. Well in your way call the main method from another class in another location. – Murali Nov 29 '12 at 13:37

1 Answers1

2

You can run another Java program by exec a command like:

Runtime.getRuntime().exec("java /directory/com/Main.java")
Runtime.getRuntime().exec("java -cp /directory/package.jar com.Main")

If you need to call methods on that class within the same JVM you can try to load the jar at runtime and then call the classes reflectively.

Community
  • 1
  • 1
Garrett Hall
  • 29,524
  • 10
  • 61
  • 76