execute CMD command through JAVA

Asked

Viewed 711 times

1

I am having trouble executing a command in CMD through JAVA. This command is executed by a . jar that lies inside a directory "C: Copyutility Copyutility.jar. So I need to enter this directory and run the command.

For this I am using the following Servlet below, which receives the parameters from a form on a .html. page I have already done the test and the values are being sent correctly. I believe the problem lies in the execution of the command.

Public class ExecCopyTestPlanServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        //Search Request Parameters
        String strArea = request.getParameter("Area");
        String strID = request.getParameter("ID");
        String strTPID = request.getParameter("TPID");
        String strCommand = "java -jar RQMCopyUtility.jar" + strArea + strID + strTPID;


        Process exec;  
            try {  
                exec = Runtime.getRuntime().exec("cmd /C c:\\CopyUtility"+ strCommand);
                if ( exec.waitFor() == 0)  
                    System.out.println("Executado.");  
                else  
                    System.out.println("ERRO");  
            } catch (IOException e) {  
                e.printStackTrace();  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
        }  

    }
}

On this line I’m executing the command:

exec = Runtime.getRuntime().exec("cmd /C c:\\CopyUtility"+ strCommand);

However it is not working because the command is not running, it seems that it is not entering inside the directory "C: Copyutility".

You need to open CMD.exe and leave the window open while the command is executed?

  • 1

    It is concatenating directly into the string without space, but still does not make sense the command built. Besides, what is the need to invoke cmd? Wouldn’t it be enough to call java directly? ex: java -jar c:\\CopyUtility\\RQMCopyUtility.jar

  • Hi Daniel. Really.. interesting this solution, I had not tried it. I will make this correction and return if it works out. Thank you very much.

  • c:\\CopyUtility that "c:" should not be capitalized: "c:"?

  • Hi Daniel... it worked. Thank you very much!!! Regards,

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.