当前位置: 首页 > 知识库问答 >
问题:

在Windows上打开git bash并在java中运行命令

潘志国
2023-03-14

我在Windows上使用java,想调用Linux命令,所以我尝试打开git bash并粘贴一些命令。我可以打开git bash,但不能粘贴任何内容。

这将打开git bash罚款:

String [] args = new String[] {"C:\\Progam Files\\Git\\git-bash.exe"}
Process proc = new ProcessBuilder(args).start();

当我这样做时,git bash打开但立即关闭:

String [] args = new String[] {"C:\\Progam Files\\Git\\git-bash.exe", "-c", "cd c:"}
Process proc = new ProcessBuilder(args).start();

共有3个答案

闻人高卓
2023-03-14

如果您安装了Git,而不需要将输出写入临时文件,那么这将在Windows上执行bash脚本。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringJoiner;

public class BashRunner
{
    public static final String BASH_PATH = "C:/Program Files/Git/bin/sh.exe";
    public static final String SCRIPT_NAME = "C:/temp/test-script.sh";

    public static void main(String[] args)
    {
        String output = runCommand(BASH_PATH, "-c", SCRIPT_NAME);
        System.out.println(output);
    }

    public static String runCommand(String... params)
    {
        ProcessBuilder pb = new ProcessBuilder(params);
        Process p;
        StringJoiner joiner = new StringJoiner(System.getProperty("line.separator"));
        try
        {
            p = pb.start();

            final BufferedReader reader = new BufferedReader(
                new InputStreamReader(p.getInputStream()));

            reader.lines().iterator().forEachRemaining(joiner::add);

            p.waitFor();
            p.destroy();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return joiner.toString();
    }
}

脚本的内容:

#!/bin/bash

echo "hello"
echo "world!"

输出:

hello
world!

此外,这将静默执行Git Bash,因为您在处理时不会收到弹出窗口。

孔砚
2023-03-14

你需要弹出bash终端吗?如果没有,这可能会奏效

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class testprog {
    public static void main(String args[]) {
        String s;
        Process p;
        try {
            p = Runtime.getRuntime().exec("ls -aF");
            BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null)
                System.out.println("line: " + s);
            p.waitFor();
            System.out.println ("exit: " + p.exitValue());
            p.destroy();
        } catch (Exception e) {}
    }
}
濮金鑫
2023-03-14

您只需更改路径和git命令。但是git bash输出打印在单独的页面上。txt文件,因为我无法以任何其他方式读取它。

public class GitBash {

    public static final String path_bash = "C:/Program Files/Git/git-bash.exe";

    // Create a file Output.txt where git-bash prints the results
    public static final String path_file_output_git_bash =
            "C:/Users/Utente/Documents/IntelliJ-DOC/IntelliJ_project/Prova/src/main/Git-bash/Output.txt";

    public static void main(String[] args) {
        // Path to your repository
        String path_repository = "cd C:/Users/Utente/Documents/Repository-SVN-Git/Bookkeeper";
        // Git command you want to run
        String git_command = "git ls-files | grep .java | wc -l";

        String command = path_repository + " && " + git_command + " > " + path_file_output_git_bash;

        runCommand(command);
    }

    public static void runCommand(String command) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder();
            processBuilder.command(path_bash, "-c", command);

            Process process = processBuilder.start();

            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println(" --- Command run successfully");
                System.out.println(" --- Output = " + readFileTxt());

            } else {
                System.out.println(" --- Command run unsuccessfully");
            }
        } catch (IOException | InterruptedException e) {
            System.out.println(" --- Interruption in RunCommand: " + e);
            // Restore interrupted state
            Thread.currentThread().interrupt();
        }
    }

    public static String readFileTxt() {
        String data = null;
        try {
            File myObj = new File(path_file_output_git_bash);
            Scanner myReader = new Scanner(myObj);
            while (myReader.hasNextLine()) {
                data = myReader.nextLine();
            }
            myReader.close();
        } catch (FileNotFoundException e) {
            System.out.println(" --- An error occurred");
            e.printStackTrace();
            }
            return data;
        }
    }
}

---2021 3月26日编辑---

无需回答。txt文件:用Java中的ProcessBuilder读取输出git bash

 类似资料:
  • 我在Windows中有。我正在尝试运行,但它给了我错误。 意图:我想使用解析JSON。

  • 我目前正在尝试学习如何使用PhoneGap为android手机开发应用程序。我得到了这本名为《PhoneGap 2x移动应用程序开发》的书。这本书真的很好,不言自明,但我的问题是,当作者想通过命令行创建d项目时,他使用的是Unix命令,因为他运行的是Unix PC。我运行的是Windows,使用CMD来遵循他的命令对我来说有点难,因为我不理解他的所有命令。 他写道: 有人能告诉我如何在window

  • 我们计划使用OpenLDAP进行用户管理,即:在OpenLDAP中维护的所有用户信息,从我们的Java web应用程序调用LDAP服务进行用户身份验证,稍后还计划使用ACL。我对OpenLDAP是完全陌生的,因此我有以下基本问题: > 在OpenLDAP网站上,它说OpenLDAP软件是平台无关的,适用于所有操作系统。但是,当我开始查看文档和FAQ(http://www.OpenLDAP.org/

  • 问题内容: 我正在尝试从Windows中的命令行执行Java程序。这是我的代码: 我不确定如何执行程序-有帮助吗?在Windows上可以吗?为什么它不同于另一个环境(我以为JVM只写一次,可以在任何地方运行)? 问题答案: 假设你的文件位于 运行命令提示符 这使C:\ mywork成为当前目录。 这将显示目录内容。你应该在文件中看到。 这告诉系统在哪里可以找到JDK程序。 这将运行编译器。除了下一

  • 我正试图在Windows中从命令行执行一个Java程序。下面是我的代码: 我不确定如何执行程序-有什么帮助吗?这在Windows上可能吗?为什么它和另一个环境不同(我以为JVM是编写一次,运行任何地方)?

  • 在linux中,您可以使用命令