简体   繁体   中英

execute .jar file from another Java program

I'm wondering how can I execute a Java program when I enter input into another Java program? Like the program I'm running can accept user input and based on that input it searches for a file with the same name and executes it. Is it even possible?

Yes, this is possible although when executing a .jar file, you may receive some errors when attempting to launch the jar if it isn't a frame or some kind of desktop application-type thing. You could accept user input with a scanner, for example, asking the user to define a path to a .jar file they wish to execute.

Here is an example of running a .jar file:

Desktop.getDesktop().open(new File("/Path/To/File"));

In the /Path/To/File part, you could just use user input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM