Last active
August 29, 2015 13:57
-
-
Save w25r/9536507 to your computer and use it in GitHub Desktop.
Gradle Windows Process Execution Hanging Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'groovy' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile 'org.codehaus.groovy:groovy-all:1.8.8' | |
testCompile gradleApi() | |
} | |
task runExec(type: Exec) { | |
description 'Demonstrate the problem from the standard Exec class' | |
commandLine 'cmd', '/c', 'java' | |
args '-cp' | |
def classpath = '' | |
sourceSets.main.runtimeClasspath.each { | |
println it | |
classpath += "$it;" | |
} | |
args classpath | |
args 'JavaFromJava' | |
dependsOn classes | |
} | |
task runJavaExec(type: JavaExec) { | |
description 'Demonstrate the crux of the problem' | |
classpath sourceSets.main.runtimeClasspath | |
main 'JavaFromJava' | |
dependsOn classes | |
} | |
task runNotepad(type: Exec) { | |
description 'Demonstrate the problem independent of java' | |
commandLine 'cmd', '/c', 'cmd /c start notepad.exe' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.OutputStream; | |
import javax.swing.JOptionPane; | |
import org.codehaus.groovy.runtime.ProcessGroovyMethods; | |
public class JavaFromJava | |
{ | |
public static void main(String... args) throws IOException, InterruptedException | |
{ | |
int level = 0; | |
if (args != null && args.length == 1) | |
{ | |
level = Integer.valueOf(args[0]); | |
} | |
if (level == 0) | |
{ | |
// create a JVM and wait for it to die | |
ProcessBuilder builder = new ProcessBuilder("java", "-cp", System.getProperty("java.class.path"), "JavaFromJava", "1"); | |
Process process = builder.start(); | |
ProcessGroovyMethods.consumeProcessOutput(process, (OutputStream) System.out, System.err); | |
int exitValue = process.waitFor(); | |
System.out.println("Process exit value:: " + exitValue); | |
System.out.println("Exiting from LEVEL 0"); | |
System.exit(exitValue); | |
} | |
else if (level == 1) | |
{ | |
// create a JVM, and exit | |
ProcessBuilder builder = new ProcessBuilder("java", "-cp", System.getProperty("java.class.path"), "JavaFromJava", "2"); | |
Process process = builder.start(); | |
ProcessGroovyMethods.consumeProcessOutput(process, (OutputStream) System.out, System.err); | |
// sleep to let the process get up and running | |
Thread.sleep(1000); | |
System.out.println("Exiting from LEVEL 1"); | |
System.exit(0); | |
} | |
else if (level == 2) | |
{ | |
System.out.println("Process LEVEL 2 started, launching modal dialog"); | |
// doing this DOES fix the problem... but I don't control the grandchild process | |
// System.out.close(); | |
// System.err.close(); | |
// System.in.close(); | |
JOptionPane.showMessageDialog(null, "Process should not be tied to this window!"); | |
System.out.println("Exiting from LEVEL 2"); | |
System.exit(0); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import org.gradle.api.Action; | |
import org.gradle.api.Project; | |
import org.gradle.api.Task; | |
import org.gradle.api.plugins.JavaPlugin; | |
import org.gradle.api.plugins.JavaPluginConvention; | |
import org.gradle.api.tasks.JavaExec; | |
import org.gradle.api.tasks.compile.JavaCompile; | |
import org.gradle.testfixtures.ProjectBuilder; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class JavaFromJavaFromGradleTest | |
{ | |
Project project; | |
@Before | |
public void setup() | |
{ | |
project = ProjectBuilder.builder().withName(getClass().getSimpleName()).withProjectDir(new File("build/tmp/debugDemo")).build(); | |
project.getPlugins().apply(JavaPlugin.class); | |
JavaPluginConvention convention = project.getConvention().getPlugin(JavaPluginConvention.class); | |
convention.getSourceSets().getByName("main").getJava().srcDir("../../../src/main/java"); | |
project.getRepositories().mavenCentral(); | |
project.getDependencies().add("compile", "org.codehaus.groovy:groovy-all:1.8.8"); | |
JavaCompile compileJava = (JavaCompile) project.getTasks().getByName("compileJava"); | |
compileJava.execute(); | |
Assert.assertTrue(compileJava.getDidWork()); | |
} | |
@Test | |
public void testJavaExec() | |
{ | |
JavaExec javaExec = project.getTasks().create("javaExec", JavaExec.class, new Action<Task>() | |
{ | |
public void execute(Task t) | |
{ | |
JavaExec exec = (JavaExec) t; | |
JavaPluginConvention convention = project.getConvention().getPlugin(JavaPluginConvention.class); | |
exec.setClasspath(convention.getSourceSets().getByName("main").getRuntimeClasspath()); | |
exec.setMain("JavaFromJava"); | |
} | |
}); | |
javaExec.execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related post on gradle forums:
http://forums.gradle.org/gradle/topics/process_execution_hangs_on_windows_if_children_processes_are_still_alive?rfm=1