简体   繁体   中英

How to execute windows commands with java program in windows OS

I want to gradle build on a huge set of projects to import into the eclipse. I know that, we can include the projects which we want to build under settings.gradle file. But there is a limitation with this in my working area.

So what I thought is, instead of doing the gradle build by going to each and every project. I want to automate this with the java program.

I tried to execute the basic windows command execution with the following java program:

import java.io.BufferedReader;
import java.io.InputStreamReader;      
public class Windows {    

    public static void main(String[] args) {

        Windows obj = new Windows();    

        String output = obj.executeCommand("ipconfig");

        System.out.println(output);

    }

    private String executeCommand(String command) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();    
    }
}

So, In the above program if I give the command as gradle build it is not working Can you please help me on this.

Since you are already using Gradle, why not solve this in gradle?

Option 1: Put everything in a multi-project build

Option 2: Create a composite build from multiple separate builds

Option 3: Create an "uber" gradle build which invokes the others using a GradleBuild task

apply plugin: 'base'

def buildDirs = ['dir1', 'dir2', 'dir3']

buildDirs.each { buildDir ->
   Task task = tasks.create(name: "build${buildDir.capitialize()}", type: GradleBuild) {
      dir = buildDir
      tasks = ['build']
   }
   build.dependsOn task
}

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