当前位置: 代码迷 >> java >> 谁能告诉我如何在javacode中运行此命令?
  详细解决方案

谁能告诉我如何在javacode中运行此命令?

热度:83   发布时间:2023-07-25 19:22:20.0

我需要在JavaCode中运行以下命令:

cd /Users/frankhe/projects/poc_demo/ngcsc-poc-api/../ngcsc-poc-impala; mvn exec:java -Dexec.mainClass=com.cloudera.example.ClouderaImpalaJdbcExample -Dexec.arguments='query.txt'

如果我在shell中运行,此命令很好,但是我需要直接在java代码中运行它,因此我使用了以下命令:

Runtime rt = Runtime.getRuntime();
String cmd = "cd "+gotoPath+"; mvn exec:java -Dexec.mainClass=com.cloudera.example.ClouderaImpalaJdbcExample -Dexec.arguments='query.txt'";
System.out.print(cmd);
rt.exec(cmd);

但是,在我检查结果之后,它没有运行并且没有生成文件,因此似乎cmd没有成功运行。

怎么了 谢谢

我将尝试该解决方案以查看结果,此外,我尝试了以下操作:

String[] cmds = { "/bin/sh", "-c", "cd /Users/frankhe/projects/poc_demo/ngcsc-poc-impala && mvn exec:java -Dexec.mainClass=com.cloudera.example.ClouderaImpalaJdbcExample -Dexec.arguments='query.txt'" };
        Runtime.getRuntime().exec(cmds);

为什么这一个也不起作用? 任何想法? 谢谢

@MadProgrammer,您好,我使用您的解决方案如下:

ProcessBuilder pb = new ProcessBuilder(
                "/usr/local/bin/mvn",
                "exec:java",
                "-Dexec.mainClass=com.cloudera.example.ClouderaImpalaJdbcExample",
                "-Dexec.arguments='/Users/frankhe/projects/poc_demo/ngcsc-poc-impala/query.txt'"
        );
        pb.directory(new File("/Users/frankhe/projects/poc_demo/ngcsc-poc-api/../ngcsc-poc-impala"));
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
            try (InputStream is = p.getInputStream()) {
                int in = -1;
                while ((in = is.read()) != -1) {
                    System.out.print((char)in);
                }
            }
            int exitCode = p.waitFor();
            System.out.println("\nProcess exited with " + exitCode);
        } catch (IOException | InterruptedException ex) {
            ex.printStackTrace();
        }

但是我收到如下错误:

[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: '/Users/frankhe/projects/poc_demo/ngcsc-poc-impala/query.txt' (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at com.cloudera.example.ClouderaImpalaJdbcExample.main(ClouderaImpalaJdbcExample.java:51)

对我来说这没有意义,文件在那里,为什么进程找不到它? 谢谢

您应该首先尝试ProcessBuilder ,它比Runtime.exec更具可配置性,除了允许将错误流重定向到输入流(使其更易于阅读)之外,您还可以指定应该从中执行命令的工作目录。内。

ProcessBuilder pb = new ProcessBuilder(
                "mvn",
                "exec:java",
                "-Dexec.mainClass=com.cloudera.example.ClouderaImpalaJdbcExample",
                "-Dexec.arguments='query.txt'"
);
pb.directory(new File("/Users/frankhe/projects/poc_demo/ngcsc-poc-api/../ngcsc-poc-impala"));
pb.redirectErrorStream(true);

try {
    Process p = pb.start();
    try (InputStream is = p.getInputStream()) {
        int in = -1;
        while ((in = is.read()) != -1) {
            System.out.print((char)in);
        }
    }
    int exitCode = p.waitFor();
    System.out.println("\nProcess exited with " + exitCode);
} catch (IOException | InterruptedException ex) {
    ex.printStackTrace();
}
  相关解决方案