Java tutorial
/* --------- BEGIN COPYRIGHT NOTICE --------- * Copyright 2013 Technologic contributors * * This file is part of the Minecraft Forge Maven Plugin. * * The Minecraft Forge Maven Plugin is free software: you can * redistribute it and/or modify it under the terms of the GNU * Lesser General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) * any later version. * * The Minecraft Forge Maven Plugin is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with the Minecraft Forge Maven Plugin. If not, see * <http://www.gnu.org/licenses/>. * ---------- END COPYRIGHT NOTICE ---------- */ package com.maltera.technologic.maven.mcforge; import java.io.File; import java.io.IOException; import org.apache.maven.plugin.logging.Log; import org.codehaus.plexus.util.cli.StreamConsumer; import org.codehaus.plexus.util.cli.StreamPumper; /** Simple wrapper for executing Python scripts. */ public class PythonLauncher { private static final String[] PYTHON_EXES = new String[] { "fml/python/python_fml.exe", "runtime/bin/python/python_mcp.exe", }; private final StreamConsumer logger; private final File dir; private final String python; public PythonLauncher(final Log log, File dir) { logger = new StreamConsumer() { public void consumeLine(String line) { log.info(line); } }; this.dir = dir; if (System.getProperty("os.name").toLowerCase().contains("win")) { String result = null; for (String candidate : PYTHON_EXES) { final File file = new File(dir, candidate); if (file.isFile()) { result = file.getPath(); break; } } if (null == result) { throw new IllegalArgumentException("no supported Python executable found"); } else { python = result; } } else { python = "python"; } } public int execute(File file) throws IOException { final ProcessBuilder builder = new ProcessBuilder(); builder.command(python, file.getPath()); builder.directory(dir); builder.redirectErrorStream(true); final Process proc = builder.start(); final StreamPumper pump = new StreamPumper(proc.getInputStream(), logger); pump.start(); while (true) { try { int result = proc.waitFor(); pump.close(); return result; } catch (InterruptedException caught) { // we don't care, just wait again } } } }