3

Hi I want to execute this command from java code :

cd C:/Users/Amira/junoWorkspace/TestProjectUI
mvn -Dtest=MyClassTest.java test

So I found this method but i didn't find a way to adapt it to my case :

    public static void main(String[] args) throws IOException
    {
            try
            {
                    // Runtime runtime = Runtime.getRuntime();
                   String[] cmd={"C:\\WINDOWS\\System32\\cmd.exe","/C start test.bat"};
                    Process p = Runtime.getRuntime().exec(cmd);
            // TODO code application logic here
            }
            catch(IOException e)
            {
                System.err.println("echec de l'execution du script: "+e);
                System.exit(1);
            }
    }

Any idea ? Cheers

1
  • 1
    Runtime.getRuntime().exec(cmd); doesn't always work (with all the commands). Commented Jul 8, 2012 at 20:30

1 Answer 1

4

What you are looking for is ProcessBuilder which will allow you to set arguments like the working directory etc, it will also allow for multiple arguments, check here and here and ProcessBuilder Demo for some examples on how to use it

Sign up to request clarification or add additional context in comments.

1 Comment

and don't forget to handle the ErrorStream and the InputStream. 1+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.