Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By jim829

Thread: How to make Java open a program and write ONLY in that program

  1. #1
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default How to make Java open a program and write ONLY in that program

    Hello, I would like to know how can I make Java open an application and type ONLY in the application. I already made Java open an application like Notepad and type using a robot, but if I open Microsoft Word, Java will type in the Word document and not in Notepad. How can I make it so that Java types only on the Notepad document even though I use another application meanwhile Java is typing.

    Here is the code to open and type in the notepad document:

    try {
    Process p = Runtime.getRuntime().exec("C:\\Documents and Settings\\construccion\\Desktop\\notepad.txt");

    Robot robot = new Robot();

    robot.delay(1600);
    robot.keyPress(KeyEvent.VK_A);
    robot.delay(500);
    robot.keyPress(KeyEvent.VK_C);
    robot.delay(500);
    robot.keyPress(KeyEvent.VK_C);
    robot.delay(500);
    robot.keyPress(KeyEvent.VK_ENTER);

    }
    catch (Exception ex) {
    ex.printStackTrace();
    }





    THANKS FOR HELP :)

  2. #2
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    can't you just use an output stream to write in the .txt file?
    cause you're writing the text that needs to be written in the code anyway.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to make Java open a program and write ONLY in that program

    I tend not to provide assistance to these types of questions because the solutions can be used for hacking. On the other hand, if you explain what you are trying to accomplish then I or someone else may have an alternative approach we could recommend.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Hello, I currently am working for a company. My job is to monitor some relays and I use the program Putty to connect to the relays that we installed on other companies. I connect to the relays with Putty and get information of how is it doing and what is working and what is not. The program Putty is like terminal in mac or cmd in windows. My program in Java opens Putty, and types commands like you see acc gives you access to the relay. Then I programmed to say more commands to get information about the relay. However, I want to be able to work on my computer on other things like sending emails meanwhile the commands are being typed by the robot in Putty. However, if I open a word document meanwhile the robot is typing it types on the word document and not in Putty.


    I wanted to know how can I make it so that Java types only on the Putty document even though I use another application meanwhile Java (the robot) is typing.
    This will help me not waste time. For example, right now I run the program and can not touch my computer, and if I can do this I can send an email meanwhile the robot is typing in Putty.

    Would appreciate it a lot.
    Thanks.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to make Java open a program and write ONLY in that program

    I used to do this type of thing with Perl many years ago. I used a module called Expect. According to a quick search and the following link, it has been adapted to a variety of languages.

    https://en.wikipedia.org/wiki/Expect

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Sorry if I was not clear. I do not need help entering commands in Putty or retrieving information from Putty. What I need to know is how can I make Java open an application and type in ONLY that application even though I am working on something else.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to make Java open a program and write ONLY in that program

    Quote Originally Posted by Donddo View Post
    Sorry if I was not clear. I do not need help entering commands in Putty or retrieving information from Putty. What I need to know is how can I make Java open an application and type in ONLY that application even though I am working on something else.
    Do you mean that if you are surfing or editing a document, you want whatever you type to also go to that application? I guess I don't understand what you want.
    Normally, an application is opened and the standard I/O streams are remapped using what are called pipes. You can do that in Java to accomplish your objective. Expect just makes it easier for you. But the data that is sent to the application must come from a file, the program directly, or a console or window which has user focus.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Sorry Jim. Let me explain it in a different way. I have made a program in Java so that when I run the program it automatically opens a document, and then (the robot) types in it. For example it opens a notepad document and it types in it. However, while the program is running if I open another program like word or excel, the robot will type in the word or excel document and not in the notepad that is where I want the robot to type. How can I tell the robot to ONLY type in that notepad document. That way I can run the program and the robot will be typing in notepad meanwhile I can be writing something in word or excel document.

    THANKS.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: How to make Java open a program and write ONLY in that program

    The approach that Jim suggests doesn't use Robot.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    With or without robot, does something come to your mind on how can I do this?

  11. #11
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    They way you are describing the problem is not going to work for you. Your robot is mimicking the action of typing on you computer's keyboard, which can only type into the application that is currently in focus. There's nothing you can do about that.

    What you really want to do is to ssh into some other machine, run some commands on it, and observe the output, in an automated way.

    What the guys here are suggesting to you is that there is a better way to do it, than relying on your operating system GUI. One of those solutions is to use a scripting language such as Perl or Expect. I've had some experience with Expect and it is immensely powerful and was designed to perform exactly the type of task you are trying to achieve.

    I would strongly recommend looking into it. If you fancy getting a book on Expect, it's been around so long now that you can pick a second hand one up on amazon for literally pennies.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to make Java open a program and write ONLY in that program

    Ok. Let's forget about robot for a second. You have a java program that opens a notepad document. Then you can either simply write to that document yourself using file I/O, or you can use Expect or some similar approach to spawn a process (like notepad.exe) which can then write to the document via data sent to the notepad app. If this is not what you want then someone else may need to help you because I don't understand your requirement.

    Also, the behavior you seem to describe is a robot writing into a notepad editor window. Then you open up a word document and it overlaps the notepad window and starts writing in the word document. Robot has no idea what it is writing to. It presumes the programmer knows the desktop arrangement and programs the robot accordingly. At least, that has been my experience with it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Quote Originally Posted by jim829 View Post
    Ok. Let's forget about robot for a second. You have a java program that opens a notepad document. Then you can either simply write to that document yourself using file I/O, or you can use Expect or some similar approach to spawn a process (like notepad.exe) which can then write to the document via data sent to the notepad app. If this is not what you want then someone else may need to help you because I don't understand your requirement.

    Also, the behavior you seem to describe is a robot writing into a notepad editor window. Then you open up a word document and it overlaps the notepad window and starts writing in the word document. Robot has no idea what it is writing to. It presumes the programmer knows the desktop arrangement and programs the robot accordingly. At least, that has been my experience with it.

    Regards,
    Jim


    Ok.How can I spawn a process that can write to the document via data send to the app using Expect? Or any other program.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to make Java open a program and write ONLY in that program

    Go to the web site I posted about Expect. Install the software for Java. Read the documentation and experiment. That is exactly how I would do it if I needed this capability today.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Hello, you mentioned earlier that I could use the robot or expect to write to the document via data sent to the notepad app. It would help me a lot if you know or have an idea of how to do this. I have been wanting to do this for some time, but I have not had much time until now, but if you know how to do this it will help me a lot.

    Thanks :)

  16. #16
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Hello, I also can not figure out how to install expect. do you have the link in which you installed it? And how do I set it up? I have a folder with files that says expect but I do not know if that is the original expect, and if it is how do I set it up? The program that I use for java is BlueJ.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to make Java open a program and write ONLY in that program

    Quote Originally Posted by Donddo View Post
    Hello, I also can not figure out how to install expect. do you have the link in which you installed it? And how do I set it up? I have a folder with files that says expect but I do not know if that is the original expect, and if it is how do I set it up? The program that I use for java is BlueJ.
    I have not installed the Java version of Expect. I have not installed any version of Expect in over 10 years. When I did, it was for Perl and I installed it from CPAN. Nor can I tell you how to do this with robot without experimenting myself. It is not something I do everyday.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Hello Jim, another question I have had is how can I make Java read a single cell from a document in excel. For example cell F5. If I need any other libraries, how do I install them and put them with Java. I am using java on the program Bluej in windows.

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: How to make Java open a program and write ONLY in that program

    Quote Originally Posted by Donddo View Post
    how can I make Java read a single cell from a document in excel. For example cell F5.
    Take a look at Apache POI.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  20. #20
    Donddo is offline Member
    Join Date
    Jun 2015
    Posts
    10
    Rep Power
    0

    Default Re: How to make Java open a program and write ONLY in that program

    Where can I install it? I do not find it. Also how do I configure it with my Java application, I am using BlueJ

Similar Threads

  1. Write a java program
    By lnruther in forum New To Java
    Replies: 7
    Last Post: 08-30-2013, 04:07 PM
  2. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  3. Replies: 9
    Last Post: 06-17-2011, 05:10 AM
  4. Replies: 13
    Last Post: 03-22-2011, 06:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •