Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a Program within a Program

Status
Not open for further replies.

JonJMills

Programmer
Nov 23, 2003
2
US
Anyone know how to start another program on a local machine from within a Java app? Any API's?
 
To launch a new JVM to run another java problem, you can do:
Code:
Runtime.getRuntime().exec("java yourProgram");

Depending on your purpose, if you want to maintain control of the new launched program by the parent program, instead of launching a new JVM to run that, you may want to use a different class loader to load the class that you want to run and use reflection to involve the main method.

here is an example from java.sun.com
Code:
File file = new File(".");
ClassLoader loader = new URLClassLoader(
     new URL[] {file.toURL()}
);

// load class through new loader
Class aClass = loader.loadClass(className.getText());
Object objectParameters[] = {new String[]{}};
Class classParameters[] =
     {objectParameters[0].getClass()};
     Method theMethod = aClass.getDeclaredMethod(
     "main", classParameters);
   // Static method, no instance needed
   theMethod.invoke(null, objectParameters);
 
But how would u start a non java program, say solitare thats already on most windows machines. If u know the exact address of the .exe is there an api that can start it running?
 
Thats what byam's example does :

Runtime.getRuntime().exec("C:\someplace\freecell.exe");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top