Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java代码与system cmd行交互_Java_Maven_Apache Nifi_Java Io_Maven Archetype - Fatal编程技术网

如何使用java代码与system cmd行交互

如何使用java代码与system cmd行交互,java,maven,apache-nifi,java-io,maven-archetype,Java,Maven,Apache Nifi,Java Io,Maven Archetype,我正在尝试创建一个Java代码来创建一个nifi定制处理器!因此,为了做到这一点,我需要使用windowscmd windows并启动mvn原型:生成,然后通过键入nifi选择modele nifi,然后通过键入1选择项目,我需要编写groupeId,工件 我需要使用java代码自动完成所有这些:我尝试了以下代码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; im

我正在尝试创建一个Java代码来创建一个nifi定制处理器!因此,为了做到这一点,我需要使用windowscmd windows并启动
mvn原型:生成
,然后通过键入
nifi
选择modele nifi,然后通过键入
1
选择项目,我需要编写groupeId,工件

我需要使用java代码自动完成所有这些:我尝试了以下代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class CmdTest {
    public static void main(String[]args) throws IOException {
        String line;
        OutputStream stdin = null;
        InputStream stderr = null;
        InputStream stdout = null;

          // launch EXE and grab stdin/stdout and stderr
        ProcessBuilder builder = new ProcessBuilder(
                "cmd.exe", "/c", "cd \"C:\\users\\eya\\desktop\\javaprocessor\" && mvn archetype:generate");
            builder.redirectErrorStream(true);
            Process process = builder.start();
          stdin = process.getOutputStream ();
          stderr = process.getErrorStream ();
          stdout = process.getInputStream ();

          // "write" the parms into stdin
          line = "nifi" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "1" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "33" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "javaproceSSor" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "javaprocessor" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "javapro" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          line = "y" + "\n";
          stdin.write(line.getBytes() );
          stdin.flush();

          stdin.close();

          // clean up if any output in stdout
          BufferedReader brCleanUp =
            new BufferedReader (new InputStreamReader (stdout));
          while ((line = brCleanUp.readLine ()) != null) {

              System.out.println ("[Stdout] " + line);
          }
          brCleanUp.close();

          // clean up if any output in stderr
          brCleanUp =
            new BufferedReader (new InputStreamReader (stderr));
          while ((line = brCleanUp.readLine ()) != null) {
            System.out.println ("[Stderr] " + line);
          }
          brCleanUp.close();
    }
}
但它只读“nifi”其他的没有!结果如下:

    [Stdout] 2679: remote -> us.fatehi:schemacrawler-archetype-plugin-command (-)
[Stdout] 2680: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
[Stdout] 2681: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
[Stdout] 2682: remote -> ws.osiris:osiris-archetype (Maven Archetype for Osiris)
[Stdout] 2683: remote -> xyz.luan.generator:xyz-gae-generator (-)
[Stdout] 2684: remote -> xyz.luan.generator:xyz-generator (-)
[Stdout] 2685: remote -> za.co.absa.hyperdrive:component-archetype (-)
[Stdout] Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1589: Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
[Stdout] 1: 1.0-alpha-1
[Stdout] 2: 1.0-alpha-2
[Stdout] 3: 1.0-alpha-3
[Stdout] 4: 1.0-alpha-4
[Stdout] 5: 1.0
[Stdout] 6: 1.1
[Stdout] 7: 1.3
[Stdout] 8: 1.4
[Stdout] Choose a number: 8: Define value for property 'groupId': [INFO] ------------------------------------------------------------------------
[Stdout] [INFO] BUILD FAILURE
[Stdout] [INFO] ------------------------------------------------------------------------
[Stdout] [INFO] Total time:  12.592 s
[Stdout] [INFO] Finished at: 2020-04-12T21:13:20+01:00
[Stdout] [INFO] ------------------------------------------------------------------------
[Stdout] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate (default-cli) on project standalone-pom: null: MojoFailureException: NullPointerException -> [Help 1]
[Stdout] [ERROR] 
[Stdout] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[Stdout] [ERROR] Re-run Maven using the -X switch to enable full debug logging.
[Stdout] [ERROR] 
[Stdout] [ERROR] For more information about the errors and possible solutions, please read the following articles:
[Stdout] [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
如果有任何方法可以自动生成mvn项目,那么非常欢迎您的帮助

编辑: 我尝试过这个,但我不知道如何配置它


别那么做。mvn命令可以接受命令行中所有必需的参数,因此不需要交互操作。请参阅插件的文档,并在单个命令中提供相应的参数。看见
这里有一个例子:

为什么要使用Java来实现这一点?有更多适合这种脚本编写的语言。@m0skit0因为这涉及到一个更大的项目,第一步是自动生成处理器!要做到这一点,我需要使用Java与cmd windows进行交互,即使它是一个更大项目的一部分,您不必在所有事情上都使用Java。。。这就像用锤子在烤面包上抹黄油。我发现了这一点,但它没有让我选择正确的过滤器nifi或我喜欢的原型对不起,我没有得到你的解决方案:(!@maryemneyli您没有搜索功能,而是输入了正确的groupId、artifactId和version作为参数。但是先输入“nifi”再输入“1”有什么好处呢不要直接使用
org.apache.nifi:nifi处理器包原型
?您必须分别指定
archetypeArtifactId
archetypeGroupId
archetypeVersion
:@maryemneyli接受这个。我的评论只是澄清。