当前位置: 首页 > 面试题库 >

如何从Java程序运行mvn命令?

文凯康
2023-03-14
问题内容

我正在构建一个Java程序来自动化服务器端的过程。通常,我cd到Desktop / GIT /并使用此maven命令“ mvn integration-
test -DskipTests -P Interactive -e”。

我正在构建一个Java程序,并且试图运行该命令行,但到目前为止我没有成功。

到目前为止,这是代码:

public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub

Process theProcess = null;


try
  {
      theProcess = Runtime.getRuntime().exec("mvn integration-test -DskipTests -P interactive -e");
  }
 catch(IOException e)
  {
     System.err.println("Error on exec() method");
     e.printStackTrace();  
  }

// read from the called program's standard output stream
  try
  {
     inStream = new BufferedReader(new InputStreamReader( theProcess.getInputStream()));  
     System.out.println(inStream.readLine());
  }
  catch(IOException e)
  {
     System.err.println("Error on inStream.readLine()");
     e.printStackTrace();  
  }

break;

    }

}

in.close();
}

问题答案:

我设法使用以下代码运行mvn :(我使用此命令:Runtime.getRuntime()。exec(cmd);)

    import java.io.*;
    import java.util.ArrayList;


    public class RunMvnFromJava     {
            static public String[] runCommand(String cmd)throws IOException
{

                // The actual procedure for process execution:
                //runCommand(String cmd);
                // Create a list for storing output.
                ArrayList list = new ArrayList();
                // Execute a command and get its process handle
                Process proc = Runtime.getRuntime().exec(cmd);
                // Get the handle for the processes InputStream
                InputStream istr = proc.getInputStream();
                // Create a BufferedReader and specify it reads
                // from an input stream.

                BufferedReader br = new BufferedReader(new InputStreamReader(istr));
                String str; // Temporary String variable
                // Read to Temp Variable, Check for null then
                // add to (ArrayList)list
                while ((str = br.readLine()) != null) 
                    list.add(str);
                    // Wait for process to terminate and catch any Exceptions.
                        try { 
                            proc.waitFor(); 
                            }
                        catch (InterruptedException e) {
                            System.err.println("Process was interrupted"); 
                            }
                        // Note: proc.exitValue() returns the exit value.
                        // (Use if required)
                        br.close(); // Done.
                        // Convert the list to a string and return
                        return (String[])list.toArray(new String[0]);
}
// Actual execution starts here
            public static void main(String args[]) throws IOException
            {
                try
                {
                    // Run and get the output.
                    String outlist[] = runCommand("mvn integration-test -DskipTests -P interactive -e");
                    // Print the output to screen character by character.
                    // Safe and not very inefficient.
                    for (int i = 0; i < outlist.length; i++)
                        System.out.println(outlist[i]);
                }   
                catch (IOException e) { 
                    System.err.println(e); 
                }
            }
}


 类似资料:
  • 问题内容: 所以我在这里有一个菜鸟般的时刻,我以前从未使用过命令行来运行Java程序,但现在我需要。我遇到的问题是,当我尝试运行程序时,出现ClassNotFoundException。我的课叫做OmadUpdate。我已经使用javac命令将OmadUpdate.java文件编译为OmadUpdate.class。我已经检查了目录,并且它们都绝对存在,但是当我运行java OmadUpdate命

  • 我下载了一个java程序,它由两个文件夹src和classes组成,分别包含源文件和类文件。现在,src和classes文件夹包含几个嵌套的子文件夹,其中最后一个子文件夹分别包含源文件和类文件。更准确地说,源文件和类文件的路径是src/edu/univ/。java和classes/edu/univ/。班假设包含main函数的文件是main。java,如何从命令行运行此程序。 我尝试过: 我也尝试过

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

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

  • 我有一个结构如下的项目: Maven-as-submodule-只是一个分级项目;graph-gen是gradle项目中的Maven模块(不是子模块) 我的目标是从gradle任务运行command:。所以我写了一些: null 任务runMvnInterfaceGenerator(类型:Exec){dependsOn“copy graphstoGenerator”workingDir“./gra

  • 问题内容: 我对Java的经验不是很丰富,这使我发疯。我编写了一个Java程序,需要从命令行运行它。 我可以从命令行编译它,它将在该文件夹中创建所有类,但是当我尝试时说: 线程“主”中的异常java.lang.NoClassDefFoundError:FileManagement / Main 事实是,我已经在远程计算机上尝试了相同的过程,并且工作正常。它不适用于我的。 问题答案: 如果您的课程在