Return-Path: X-Original-To: apmail-commons-user-archive@www.apache.org Delivered-To: apmail-commons-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3139379E2 for ; Wed, 28 Sep 2011 20:15:18 +0000 (UTC) Received: (qmail 30013 invoked by uid 500); 28 Sep 2011 20:15:16 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 29924 invoked by uid 500); 28 Sep 2011 20:15:16 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 29916 invoked by uid 99); 28 Sep 2011 20:15:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Sep 2011 20:15:16 +0000 X-ASF-Spam-Status: No, hits=1.6 required=5.0 tests=FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of ethanhuang1991@gmail.com designates 74.125.82.49 as permitted sender) Received: from [74.125.82.49] (HELO mail-ww0-f49.google.com) (74.125.82.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Sep 2011 20:15:09 +0000 Received: by wwp14 with SMTP id 14so7849977wwp.6 for ; Wed, 28 Sep 2011 13:14:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=C/5T/RoPoGCSTpEdFCi56Z95UpdiJaWHPOMjiMbpg7g=; b=NulTMtg4bQNuT7J68g66+tlPuzHN/+eYMRQGtx3jcx9VvWKwip5N1L4OoGupjnk/Fb HXWxS79cmza5mZTw220ykcEc6xNsIWud+b5PizkAyQ+BIjJ73qIPd3smqRnmKUqCwVEe ig04idOzuCmubnhOkHBL2cq2goVSRDqquIHsg= MIME-Version: 1.0 Received: by 10.227.167.1 with SMTP id o1mr10987449wby.6.1317240889590; Wed, 28 Sep 2011 13:14:49 -0700 (PDT) Received: by 10.227.128.134 with HTTP; Wed, 28 Sep 2011 13:14:49 -0700 (PDT) In-Reply-To: <4E8370A1.6080700@it20one.at> References: <4E8370A1.6080700@it20one.at> Date: Wed, 28 Sep 2011 13:14:49 -0700 Message-ID: Subject: Re: [exec]How could I use exec to execute a java program with keyboard input in it? From: Yi Huang To: Commons Users List , "siegfried.goeschl@it20one.at" Content-Type: multipart/alternative; boundary=20cf300259444ec3e204ae060b69 X-Virus-Checked: Checked by ClamAV on apache.org --20cf300259444ec3e204ae060b69 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable Thank you very much! That was exactly what I want. =D4=DA 2011=C4=EA9=D4=C228=C8=D5=D0=C7=C6=DA=C8=FD=A3=ACSiegfried Goeschl <= siegfried.goeschl@it20one.at> =D0=B4=B5=C0=A3=BA > Hi, > > if I understand that correctly you would like to get some keyboard input for your child process - I have never done that but I think the following snippet from DefaultExecutorTest might show you the way > > Cheers, > > Siegfried Goeschl > > /** > * The test script reads an argument from stdin and prints > * the result to stdout. To make things slightly more interesting > * we are using an asynchronous process. > * > * @throws Exception the test failed > */ > public void testStdInHandling() throws Exception { > > ByteArrayInputStream bais =3D new ByteArrayInputStream("Foo".getBytes()); // newline not needed; causes problems for VMS > CommandLine cl =3D new CommandLine(this.stdinSript); > PumpStreamHandler pumpStreamHandler =3D new PumpStreamHandler( this.baos, System.err, bais); > DefaultExecuteResultHandler resultHandler =3D new DefaultExecuteResultHandler(); > Executor executor =3D new DefaultExecutor(); > executor.setStreamHandler(pumpStreamHandler); > executor.execute(cl, resultHandler); > > resultHandler.waitFor(WAITFOR_TIMEOUT); > assertTrue("ResultHandler received a result", resultHandler.hasResult()); > > assertFalse(exec.isFailure(resultHandler.getExitValue())); > String result =3D baos.toString(); > assertTrue("Result '"+result+"' should contain 'Hello Foo!'", result.indexOf("Hello Foo!") >=3D 0); > } > > > On 27.09.11 09:41, Yi Huang wrote: >> >> For example: >> >> import java.util.Scanner; >> >> public class ToBeCalled { >> >> public static void main(String[] args) { >> Scanner keyboard =3D new Scanner(System.in); >> int t =3D keyboard.nextInt(); >> System.out.println(t); >> } >> } >> >> How could I use exec to let it run properly? >> I have tried this: >> >> // compile >> cmdline =3D CommandLine.parse("javac"); >> executor =3D new DefaultExecutor(); >> cmdline.addArgument("ToBeCalled.java"); >> exitValue =3D executor.execute(cmdline); >> System.out.println("compile: " + exitValue); >> >> // run >> cmdline =3D CommandLine.parse("java"); >> cmdline.addArgument("ToBeCalled"); >> cmdline.addArgument("abc"); >> cmdline.addArgument("def"); >> executor =3D new DefaultExecutor(); >> exitValue =3D executor.execute(cmdline); >> System.out.println("run: " + exitValue); >> >> It did compile, but crashed and said that there is no argument in nextInt() >> How could I fix that? >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org > For additional commands, e-mail: user-help@commons.apache.org > > --20cf300259444ec3e204ae060b69--