Murali
Murali

Reputation: 772

Calling a class from another program which they are in different locations

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();
}

Upvotes: 4

Views: 831

Answers (1)

Garrett Hall
Garrett Hall

Reputation: 30022

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.

Upvotes: 2

Related Questions