Sign in to follow this  
Followers 0
J_Y_C

Communicating with Java via Stdin/Stdout

11 posts in this topic

#1 ·  Posted (edited)

I don't know if I'm in way over my head here, but I am trying to use stdin and stdout to talk back and forth to a Java console output.

Obviously, you may need to know some basic java to help me here.

Let's say you have a compiled Java class called TestClass:

import java.io.*;

public class TestClass {
    public static void main(String args[]){
        String s;
        InputStreamReader ir = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(ir);
        System.out.println("Hello AutoIt");
        try{
            s = in.readLine();
            while(s!=null){
                if (s.equalsIgnoreCase("Hello Java")){
                    System.out.println("How are you?");
                }
                if (s.equalsIgnoreCase("I am fine")){
                    System.out.println("Yippee Skippee");
                }
                //System.out.println(s);
                s = in.readLine();
            }
            in.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

And you have the following AutoIt Script:

#include <Constants.au3>
$dir = "C:\javafiles\"
$cmd="java TestClass"
FileChangeDir($dir)
$foo = Run(@ComSpec & " /c "&$cmd, $dir, 1, $STDIN_CHILD + $STDOUT_CHILD)

While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0,"Java says :",$line)
    if $line = "Hello AutoIt" Then
        StdinWrite($foo,"Hello Java")
    EndIf
    if $line = "How are you?" Then
        StdinWrite($foo,"I am fine")
    EndIf   
    if $line = "Yippee Skippee" Then
        StdinWrite($foo,"^Z")
        MsgBox(0,"qc","it worked!")
        Exit
    EndIf
Wend

;;(I realize this is sloppy code, and there are better ways than this, but this particular code isn't really
;;my goal, it's just proof of concept....

My hopes were that I could send strings back and forth to my Java process, but it doesn't seem to work. Autoit sees Java saying "Hello AutoIt", but doesn't seem to reply right. If I could just send and receive strings from Java, my world would be rocked. I tried the same thing but with consolewrite, but that didn't work either.

And Java folks able to help me on this one?

Edited by J_Y_C

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

if you dont understand the question darkshadow i would recommend you dont post ;)

As i understand the question but I am unable to explain why it isn't sending anything back on the line

Edited by thatsgreat2345

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Not a java programmer, but a suggestion to try to iso your issue...

Have you got your java to work w/ another compiled console mode app?

Should be easy enough to build a testcase with, for example, the ms built in ftp client; or any other console mode utility.

Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

I really don't know java, but by knowing some other languages i think i may have found your problem:

The while loop you have set up as:

while(s!=null)

This may be a problem since the while loop probably rechecks the stream much faster than autoit responds. If that is the case, then it may be exiting the loop and closing the stream before autoit has a chance to respond. I recommend you don't use while(s!=null). Instead i recommend you use a while(1) and when you receive a certain command from autoit, such as "K,done", then have the loop break;. This may be more foolproof then your current setup.

So maybe something like this:

import java.io.*;

public class TestClass {
    public static void main(String args[]){
        String s;
        InputStreamReader ir = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(ir);
        System.out.println("Hello AutoIt");
        try{
            s = in.readLine();
            while(1){
                if (s.equalsIgnoreCase("Hello Java")){
                    System.out.println("How are you?");
                }
                if (s.equalsIgnoreCase("I am fine")){
                    System.out.println("Yippee Skippee");
                }
                if (s.equalsIgnoreCase("k, done!")){
                    System.out.println("Exiting...");
                    break;
                }
                //System.out.println(s);
                s = in.readLine();
            }
            in.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

That's a good stab at it Kandie Man, but it is not the case. You can compile and run that Java class, and it doesn't time out at all. You must ctrl-z to get out of it. And, furthermore, if you look at the code you will see I set up a little "conversation" dialogue to test with. Running it, and doing the question/answer works just fine. Otherwise, it just echoes the typed text back. But the point of the q/a on this isn't really just to get the communication working, my end goal is so that I can use my Java code in conjunction with AutoIt. There are some things in AutoIt that aren't nearly as fast as efficient as Java, and vice versa, so I am hoping to get the best of both worlds here.

Thanks for you interest in this, I am hoping that I can get the attention of some of the Java gurus on this site. I'm new to Java, but I really like the OO nature of it, and if I could find a good way to get the two to talk I could totally make my AutoIt code WAY more efficient.

I suppose that I could just use text files to communicate, but opening and closing text files could be a rather messy and slow affair. I KNOW that will work, I just don't think it's going to be the best method. I am not in a hurry for this, and I am actually swamped this week (even though it's thanksgiving, sucks), so I may not get to work on this right away. But, I think for Java and other OO programmers, something like this may help us use AutoIt is a more efficient manner.

But still, AutoIt does rock!!!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Actually darkshadow791, you may be on to something.

Figures that I didn't try the simplest thing. Now I just need to read text from that DOS window, and I will be ready to roll. Certainly that should be pretty easy, right?

(Not so easy. After Java sends it's initial message to the console, autoit sees it, but then locks up, so it never gets to send the response, and can't read from the console anymore after that.)

But you're right in that plain old send() did work!!

(But now I can't read from the console...)

If there's any Java folks out there that do this, or know of a better way, please LMK!

Edited by J_Y_C

Share this post


Link to post
Share on other sites

I don't know if I'm in way over my head here, but I am trying to use stdin and stdout to talk back and forth to a Java console output.

have a look at this thread: http://www.autoitscript.com/forum/index.php?showtopic=22598. You can do the same with java and AutoIT.


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

have a look at this thread: http://www.autoitscript.com/forum/index.php?showtopic=22598. You can do the same with java and AutoIT.

Thanks for the link. It would suit my purposes, but it presents a completely different set of problems in Java, and that is working with DLL files. While Java is highly flexible, the tradeoff is that is can come at the cost of a lot of code. So, if I am implementing a structure to work with DLL files, then I may as well do it with COM. Working with COM in Java entails SOOOO much code that it's like writing a novel when all you need is a poem.

Since writing to the standard output in Java is super duper easy, I thought it might be a good place to bridge the gap. As I said before, I could just communicate by writing to a file from one language, and reading that file from another, but I'd like something a little more direct, like the console.

Share this post


Link to post
Share on other sites

Bump...

Anyone, anyone, Bueller, Bueller...

Share this post


Link to post
Share on other sites

I know this is an old post but perhaps you could just have the java app listen on a tcp socket and send any commands or data through that via auto it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0