如何执行带有参数的命令?

发布于 2024-11-30 12:01:12 字数 339 浏览 2 评论 0原文

如何在 Java 中执行带有参数的命令?

我已经尝试过

Process p = Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php -m 2"});

哪个不起作用。

String[] options = new String[]{"option1", "option2"};
Runtime.getRuntime().exec("command", options);

这也不起作用,因为未指定 m 参数。

How am I to execute a command in Java with parameters?

I've tried

Process p = Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php -m 2"});

which doesn't work.

String[] options = new String[]{"option1", "option2"};
Runtime.getRuntime().exec("command", options);

This doesn't work as well, because the m parameter is not specified.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空袭的梦i 2024-12-07 12:01:12

看看这是否有效(抱歉现在无法测试)

Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});

See if this works (sorry can't test it right now)

Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", "2"});
单挑你×的.吻 2024-12-07 12:01:12

使用 ProcessBuilder而不是Runtime#exec()

ProcessBuilder pb = new ProcessBuilder("php", "/var/www/script.php", "-m 2");
Process p = pb.start();

Use ProcessBuilder instead of Runtime#exec().

ProcessBuilder pb = new ProcessBuilder("php", "/var/www/script.php", "-m 2");
Process p = pb.start();
幸福还没到 2024-12-07 12:01:12

以下应该可以正常工作。

Process p = Runtime.getRuntime().exec("php /var/www/script.php -m 2");

The following should work fine.

Process p = Runtime.getRuntime().exec("php /var/www/script.php -m 2");
病毒体 2024-12-07 12:01:12

下面是用java执行python脚本的java代码。

ProcessBuilder:

  • 第一个参数是虚拟环境的路径
  • 第二个参数是python文件的路径
  • 第三个参数是要传递给python脚本的任何参数
public class JavaCode {
    public static void main(String[] args) throws IOException {
        String lines = null;
        ProcessBuilder builder = new ProcessBuilder("/home/env-scrapping/bin/python",
                "/home/Scrapping/script.py", "arg1");
        Process process = builder.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((lines = reader.readLine())!=null) {
            System.out.println("Line: " + lines);
        }
    }
}

第一个是虚拟环境路径

Below is java code for executing python script with java.

ProcessBuilder:

  • First argument is path to virtual environment
  • Second argument is path to python file
  • Third argument is any argumrnt you want to pass to python script
public class JavaCode {
    public static void main(String[] args) throws IOException {
        String lines = null;
        ProcessBuilder builder = new ProcessBuilder("/home/env-scrapping/bin/python",
                "/home/Scrapping/script.py", "arg1");
        Process process = builder.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((lines = reader.readLine())!=null) {
            System.out.println("Line: " + lines);
        }
    }
}

First is virtual environment path

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文