Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I run cmd (command line) and running my batch file from Java this way:

final String cmd = "cmd /c C: && dir && cd C:\MyApp\Maxi && dir && C:\MayApp\Maxi\deploy.bat";

try {


            Process process = Runtime.getRuntime().exec(cmd);

            final InputStream in = process.getInputStream();

            int ch;

            while((ch = in.read()) != -1) {
                System.out.print((char)ch);
            }
        } catch (IOException e) {
             System.out.println("IOException on CMD executing statement");
             e.printStackTrace();
        }

It was working successfully, but I modified the batch file and added some argument, so I have to pass a name to the batch file so i tried this: (I send "Name1" as argument)

final String cmd = "cmd /c C: && dir && cd C:\MyApp\Maxi && dir && C:\MayApp\Maxi\deploy.bat Name1";

try {


            Process process = Runtime.getRuntime().exec(cmd);

            final InputStream in = process.getInputStream();

            int ch;

            while((ch = in.read()) != -1) {
                System.out.print((char)ch);
            }
        } catch (IOException e) {
             System.out.println("IOException on CMD executing statement");
             e.printStackTrace();
        }

But its not working now and the command is not executed. I only get the "dir" command output.

Can anyone help ?

Note: The commands run successfully on CMD, but its not working from java.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Why do you want do multiple tasks in one command? e.g. Change to C:\, dir and then execute?

You can easily club all these tasks to one batch file.

That would help you "not" compiling you code again in case some directory structure changes.

share|improve this answer
    
because the directory name is parameterized with a variable in my code, and i need to move the cursor to a specific directory before calling my batch file. Do you think its going to work this way ? –  Şahin Usif Jun 12 '13 at 19:00
    
First, you definitely do not need DIR. Secondly, provide double quotes to you command. e.g. && "C:\\MayApp\\Maxi\\deploy.bat" "Name1" –  Sandeep Jindal Jun 12 '13 at 19:11
    
I only use "DIR" to check if the cursor is moving and if there's an output. And thank you its working now :) –  Şahin Usif Jun 12 '13 at 19:21
    
how do i run commands that are present in a config file through the same java code? I want to run command line scripts through the same java code, I could run it when i hardcoded it but i want to run these commands from a config file, please let know how to do? –  user1795999 May 8 '14 at 5:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.