简体   繁体   English

如何在Java程序中获取并运行jar文件?

[英]How do I get and run a jar file within a Java program?

I'm creating an operating system-like program in java and have run into a little trouble. 我正在用Java创建类似于操作系统的程序,但遇到了一些麻烦。 I want the user to be able to run jar files of their choice in it, but I don't really know how though. 我希望用户能够在其中运行他们选择的jar文件,但是我真的不知道如何。

Basically, it will work like this: 基本上,它将像这样工作:

  1. The user opens a program in the OS called "Import Jar". 用户在OS中打开一个名为“导入Jar”的程序。
  2. The user clicks "browse", then a JFileChooser pops up. 用户单击“浏览”,然后弹出一个JFileChooser。
  3. After the user specified a file, they click "Finish". 用户指定文件后,单击“完成”。
  4. The Jar starts up in a JInternalFrame. Jar将在JInternalFrame中启动。

You can use the Runtime.exec(String) method for that. 您可以为此使用Runtime.exec(String)方法。

For example: 例如:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar yourJar.jar");

Does start your jar within a running Java program. 是否在正在运行的Java程序中启动jar In your case you have to ensure that the jar yourJar.jar starts in a JInternalFrame . 在您的情况下,必须确保jar yourJar.jarJInternalFrame启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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