Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: passing input to a process

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default passing input to a process

    I'm working on a biology project that requires me to run little apps from java. The code below was borrowed and works 99% of the time. I send it a command which it executes like typing it into command line. The problem here is that this program requires text input. In linux, if i type in "wine protdist.exe < protdist" where protdist is a text file with the input (in this case, only "y" then carriage returns), the program runs and I get output. If I use the code below, it will execute wine protdist.exe, but I cant for the life of me pass the program the few characters that I need for it to move on. Any suggestions on how I can execute this program. send it a character and a carriage return and more on? TIA

    File dirFile = new File(tempDir);
     
     
            String[] cmd = new String[1];
            cmd = "wine protdist.exe < protdist";
     
            deleteFile(tempDir + "outfile");
            Process coffee = null;
            Runtime runtime = null;
            try {
            coffee = runtime.getRuntime().exec(cmd, null, dirFile);
            }
            catch (IOException e) {
                e.printStackTrace();
                System.out.println("OMG FAIL!");
            }
     
     
            InputStream is = coffee.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            deleteFile(tempDir + "testOuput.txt");
            TextIO.writeFile(tempDir + "testOutput.txt");
            try{
            while ((line = br.readLine()) != null) {
              TextIO.putln(line);
            }
            }
            catch (IOException e) {System.out.println("fail");}
            //System.out.println("Program terminated!");


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: passing input to a process

    What does deleteFile do?

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: passing input to a process

    If the file that is to capture the output from the program already exists, it just deletes it

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: passing input to a process

    Have you printed out the error Stream to see if the called program is complaining about anything?

  5. #5
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: passing input to a process

    Could someone explain to me why this is working:
    Runtime runtime = null;
            try {
            coffee = runtime.getRuntime().exec(cmd, null, dirFile);
            }
            catch (IOException e) {
                e.printStackTrace();
                System.out.println("OMG FAIL!");
            }
    Because what I see is that a var called "runtime" is initialized and we know its of type Runtime, but its null, so no object. How can he execute the command runtime.getRuntime().exec(cmd, null, dirFile) if the runtime is null?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: passing input to a process

    public static Runtime getRuntime()
    When in doubt read the API doc. It says that getRuntime() is static. That means you don't need an object to call it.

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: passing input to a process

    After trying a thousand variations on buffered writers, this seems to work. Don't ask me why, but I thought write should output arguments to the program, but it never did until I added the flush. So, for today, I give up figuring this thing out b/c I have something that works.

     
            try{
     
                String arguments = "y" + "\n" + "\n";
                coffee.getOutputStream().write(arguments.getBytes());
                coffee.getOutputStream().flush();
            }
            catch (IOException e){System.out.println("fail");}

  8. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: passing input to a process

    Quote Originally Posted by Norm View Post
    Have you printed out the error Stream to see if the called program is complaining about anything?
    Btw, I hadn't tried this, but it was clear from the program output that it was just hanging b/c it wasnt getting the input.

  9. #9
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: passing input to a process

    Quote Originally Posted by Norm View Post
    When in doubt read the API doc. It says that getRuntime() is static. That means you don't need an object to call it.
    I know about static, that was also my first though(but forgot to check the doc) but then again, why initialize the var? if its static it doesn't need that right?

    Something along the next code would also work right?
    coffee = Runtime.getRuntime().exec(cmd, null, dirFile);

Similar Threads

  1. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  2. Process Builder Issue
    By javameanslife in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 20th, 2010, 09:13 PM
  3. Kill a process
    By subhvi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 14th, 2010, 09:11 PM
  4. process control
    By ttsdinesh in forum Java Native Interface
    Replies: 6
    Last Post: October 27th, 2009, 06:29 PM
  5. Patch Process
    By Drakenmul in forum Java Theory & Questions
    Replies: 3
    Last Post: August 2nd, 2009, 03:09 AM