LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting with pipes and Java (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-with-pipes-and-java-486150/)

BlueRaja 09-22-2006 08:51 PM

Scripting with pipes and Java
 
I have a directory of folders, each containing a number of .class (Java) files. I would like to write a script to run each one and test it on a number of given inputs.
In otherwords, when I normally type "java myClass" in one of the folders, a console menu will appear and ask for some input. I might choose option "1" and hit enter; then it will ask me, say, what my name is - I'll type in my name, and it will bring the menu back up; and so on and so forth.
I can probably figure out how to traverse all the folders (I just began scripting today), but I can't seem to figure out how to run the test cases on the java files. I tried scripts as simple as these:
Code:

echo -e "1\nBlueRaja" | java myClass
Code:

(echo 1; echo BlueRaja) | java myClass
and more complex code (using a named pipe) like this:
Code:

#!/bin/csh
mkfifo pipe
java myClass <pipe >output.out &
echo -e "a\n" > pipe
echo -e "BlueRaja\n" > pipe

exit 0

However, nothing seems to work. If anyone could lend me some of their insight, it would be greatly appreciated.

Thank you.

BlueRaja 09-23-2006 02:02 AM

The Java program reads input using the following code, if this helps:
Code:

InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader    stdin    = new BufferedReader(inStream);
String            input    = stdin.readLine();


elsheikhmh 09-23-2006 03:15 AM

hi,
try test.sh
Code:

java myClass <<DELIMITER
7
DELIMITER

replace 7 by whatever you want to enter if the program were running interactively.

bye,
mustafa

BlueRaja 09-23-2006 11:50 AM

both of these work:
Code:

echo 1 | java myClass
Code:

java myClass <<DELIMITER
1
DELIMITER

However, I would like to input more than one thing after another using a script - the following does not work:
Code:

java myClass <<DELIMITER
1
BlueRaja
DELIMITER


BlueRaja 09-23-2006 02:16 PM

Also: According to everything I've read, this should work:
Code:

input.txt:
1
BlueRaja

Code:

user%  java myClass < input.txt
This works in Windows, but not Linux (in two separate Linux machines) - I've tried everything I can think of, and am just about to give up with Linux and just do it in a batch file...

jlliagre 09-24-2006 05:12 AM

The problem must be in your java code, I believe the first bufferedReader is flushed somewhere there.

A simple Java program using your sample code works fine under Unix:
Code:

import java.io.*;

public class Test
{
  public static void main(String args[]) throws Exception
  {
    try
    {
      InputStreamReader inStream = new InputStreamReader(System.in);
      BufferedReader    stdin    = new BufferedReader(inStream);
      String            input    = stdin.readLine();
      System.out.println("input="+input);
      input    = stdin.readLine();
      System.out.println("input="+input);
    }
    catch(Exception e)
    {
      throw(e);
    }
  }
}

- = - = - = - = -

$ java Test <<%EOF%
one
two
%EOF%
input=one
input=two
$


BlueRaja 09-26-2006 03:52 AM

Wait...I'm *not* supposed to flush the BufferedReader? o_O
Wow thanks, I think I can get it working now... but why does flushing BufferedReader screw it up?

jlliagre 09-26-2006 05:53 AM

Because you discard the remaining lines before giving a chance your application to read them.


All times are GMT -5. The time now is 12:52 PM.