在Java程序中执行另一个jar
我编写了几个简单的java应用程序,名为A.jar、B.jar。
现在我想编写一个GUI java程序,以便用户可以按下按钮A来执行A.jar,按下按钮B来执行B.jar。
我还想在我的 GUI 程序中输出运行时进程的详细信息。
有什么建议吗?
I had written several simple java applications named as A.jar, B.jar.
Now i want to write a GUI java program so that user can press button A to execute A.jar and button B to execute B.jar.
Also i want to output the run-time process detail in my GUI program.
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果你是java 1.6那么还可以执行以下操作:
If you are java 1.6 then the following can also be done:
如果我理解正确的话,您似乎想在 java GUI 应用程序内部的单独进程中运行这些 jar。
为此,您可以使用:
缓冲进程的输出始终是一个好习惯。
If I understand correctly it appears you want to run the jars in a separate process from inside your java GUI application.
To do this you can use:
Its always good practice to buffer the output of the process.
.jar 不可执行。实例化类或调用任何静态方法。
编辑:
创建 JAR 时添加主类条目。
>p.mf(p.mf的内容)
使用 Process 类及其方法,
.jar isn't executable. Instantiate classes or make call to any static method.
EDIT:
Add Main-Class entry while creating a JAR.
>p.mf (content of p.mf)
Use Process class and it's methods,
希望这有帮助:
Hope this helps:
以下工作通过使用批处理文件启动 jar 来实现,以防程序作为独立运行:
在batchfile.bat 中,我们可以说:
The following works by starting the jar with a batch file, in case the program runs as a stand alone:
In the batchfile.bat then we can say:
如果该 jar 在您的类路径中,并且您知道它的 Main 类,则只需调用主类即可。以 DITA-OT 为例:
请注意,这将使从属 jar 与您的 jar 共享内存空间和类路径,并可能导致潜在的干扰。如果您不想让这些东西被污染,您还有其他选择,如上所述 - 即:
If the jar's in your classpath, and you know its Main class, you can just invoke the main class. Using DITA-OT as an example:
Note this will make the subordinate jar share memory space and a classpath with your jar, with all the potential for interference that can cause. If you don't want that stuff polluted, you have other options, as mentioned above - namely:
Runtime.getRuntime.exec(...)
(which wholly isolates the jar, but has the normal "find the application", "escape your strings right", "platform-specific WTF", and "OMG System Threads" pitfalls of running system commands.首先,我们创建一个 FirstFileOutput 类,它有一个 main 方法,该方法输出一行到稳定输出和一行到稳定错误。对于所有第一个过程,我们将再次创建一个 RuntimeExecCheck 类,它将在启动过程中运行我们的 FirstFileOutput 类,之后 RuntimeExecCheck 类将读取稳定的输出,并从 FirstFileOutput 和输出中读取稳定的错误。
First we cerate a class FirstFileOutput having a main method that outputs a line to stable output and a line to stable error. With all first procedure, we'll again create a class RuntimeExecCheck that will run our FirstFileOutput class in starting for process, and after that RuntimeExecCheck class will read the stable output and the stable error from FirstFileOutput and output comes.
您可以使用以下代码:
You can use the below code: