java bash,在java程序中执行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"});

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 ?

解决方案

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"

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

Now we translate that into Java:

Process p = new ProcessBuilder(

"/bin/sh",

"-c",

"java -jar M1_MIAGE_PDL_VIZ_GROUPE3.jar menu").start();

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值