简体   繁体   中英

Execute command prompt commands in java

I want to write a Java program , in which that need to execute a batch file to convert mp3 to wav file,am having command and its working fine in a command prompt,so to convert the file from wave to mp3 the first step is to have to change the directory to the where that batch file presents and then i need to execute mu-law-mp3.bat muic command in command prompt now my question is how i can achieve this through Java?

In my java program

  1. I need to execute cd command to the location of batch file
  2. I need to execute above command .

Here is one way of doing it.You can also specify the location of the file in the command itself.

  ProcessBuilder pb=new ProcessBuilder(command);
    pb.redirectErrorStream(true);
    Process process=pb.start();
    BufferedReader inStreamReader = new BufferedReader(
        new InputStreamReader(process.getInputStream())); 

    while(inStreamReader.readLine() != null){
        //do something with commandline output.
    }

You can use of java.lang.Process.

Process process = Runtime.getRuntime().exec(commands, null,workingDirFile);

commands is String[] containing command and arguments required in string format envP is second variable, set it to null workingDirFile is object of File object of working directory

Thanks Gaurav

Check this post out. I created a program in Java that called MATLAB that you can see there.

Can I call an external process in a Java Web Application?

You use ProcessBuilder to do so!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM