testing - Determine if a java execution from another java application was done successfully -
i have application calls java test class in specified location of pc. path hard-coded now, , checked worked executing command line (in case want see it: java -cp c:\users\user\documents\workspace\test\build\test.jar org.junit.runner.junitcore us.test.dynamicwebserviceinvocationtest), know command works fine.
the thing is, when runtime.getruntime().exec(command), if try log resulting inputstream , errorstream of resulting process, program stucks. tried exitvalue() , waitfor(), first throws incompletition error , second gets stuck. weird thing if don't touch of (the streams, or using functions), program has no problem ending.
so question is: why be? next step build command given parameters, if can't see resulting inputs can't sure if tests running or not.
the code, in case want see it:
runtime runtime=runtime.getruntime(); logger.debug("attempting execute test {} @ path {}",classpath,applicationlocation); string command="java -cp c:\\users\\user\\documents\\workspace\\test\\build\\test.jar org.junit.runner.junitcore us.test.dynamicwebserviceinvocationtest"; process process=runtime.exec(command); bufferedreader stdinput = new bufferedreader(new inputstreamreader(process.getinputstream())); bufferedreader stderror = new bufferedreader(new inputstreamreader(process.geterrorstream())); system.out.println("here standard output of command:\n"); string s = null; while ((s = stdinput.readline()) != null) { system.out.println(s); } system.out.println("here standard error of command (if any):\n"); while ((s = stderror.readline()) != null) { system.out.println(s); }
you absolutely must read both streams in separate threads. (read javadoc of process class). if wait end, or read first 1 stream before other can happen output buffer of command fills , block (on stdout or stderr, depending on read first). use waitfor() in current thread , have background thread draining outputs (this allows detecting end of child process without polling).
if want use 1 (additional) thread can redirect stderr stdout. if want avoid read streams @ all, can set processbuilder features inheritio(). allows stream written existing output , not need thread read independently.
there btw various libararies offer exec tools (for example apache commons exec has streams) offer active stream draining, logging or pumping process.
in addition might idea first close stdin, in case command waits input: p.getoutputstream().close();
Comments
Post a Comment