问题描述
我已经有一段时间了,但我找不到解决方案。我正在尝试在.jar文件中的Linux上执行bash命令。
为此,我尝试了很多东西,包括:
It's been quite a while since I'm looking for but I don't find the solution. I'm trying to execute bash command on Linux within .jar file.For that, I tried many things, including this :
Process p = new ProcessBuilder("java", "-jar", "M1_MIAGE_PDL_VIZ_GROUPE3.jar", "menu").start();
Runtime.getRuntime().exec("/bin/sh -c java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu");
Runtime.getRuntime().exec(new String[]{"/bin/sh -c", "java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu"});
所以,当我点击.jar文件时,我想该程序打开一个bash ,并执行命令(java -jar ...),执行该程序的另一部分。
So, when I click on the .jar file, I would like to that the program open a bash, and execute the command (java -jar ...), to execute another part of the program.
关于如何做的任何想法?
Any ideas as to how to do it ?
推荐答案
要理解这一点,首先需要了解如何在shell提示符下运行该命令。
To understand this, you first need to understand how you would run that command at a shell prompt.
$ sh -c "java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu"
注意双引号的位置。第一个参数是 -c
。第二个参数是引号内的东西;即 java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar菜单
Note where the double quotes are. The first argument is -c
. The second argument is the stuff inside the quotes; i.e. java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu
现在我们将其翻译成Java:
Now we translate that into Java:
Process p = new ProcessBuilder(
"/bin/sh",
"-c",
"java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu").start();
话虽如此,上述内容实际上并没有实现。当然,它不会打开一个新的控制台窗口来显示控制台输出等。与WindowsCMD.exe不同,UNIX / Linux shell不提供控制台/终端功能。为此,您需要使用终端应用程序。
Having said that, the above doesn't actually achieve anything. Certainly, it doesn't open a fresh console window to display the console output etcetera. Unlike Windows "CMD.exe", UNIX / Linux shells do not provide console / terminal functionality. For that you need to use a "terminal" application.
例如,如果您使用GNOME
For example, if you are using GNOME
Process p = new ProcessBuilder(
"gnome-terminal",
"-e",
"java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu").start();
将(可能)做你想做的事。
will (probably) do what you are trying to do.
这篇关于在java程序中执行bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!