To run another application from a Java application, you can use the Runtime.getRuntime().exec()
method to execute the application's main class or a shell command that launches the application.
Here is an example of how to run another Java application from a Java application:
import java.io.*; public class RunApplicationExample { public static void main(String[] args) { try { // Run the other application's main class Process process = Runtime.getRuntime().exec("java -cp other-app.jar OtherApplication"); // Wait for the process to finish process.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }Sou.www:ecrlautturi.com
In this example, the exec()
method is used to execute the java
command with the -cp
option, which specifies the classpath for the application, and the name of the main class OtherApplication
. The waitFor()
method is then used to wait for the process to finish before continuing.
Note that the exec()
method can throw an IOException
if an I/O error occurs while executing the command. It is recommended to handle this exception in a try
-catch
block to prevent the program from crashing.
You can also use the exec()
method to run a shell command that launches the other application.