在java程序中执行bash命令

编程入门 行业动态 更新时间:2024-10-26 10:36:38
本文介绍了在java程序中执行bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经有一段时间了,但我找不到解决方案。我正在尝试在.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命令

本文发布于:2023-07-07 09:07:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   程序   java   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!