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 7 of 7

Thread: How to open and manipulate another program?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to open and manipulate another program?

    Hi,

    I'd like to know how to do the following things :

    1 - Open a program ; It works, but I don't know how to write in it. In other words, if I open notepad, and I'd like to write in it, how can i do it ? I know how to write into a text file and everything, so I think writing into another program should be something similar? (I hope so!)

        public static void main(String[] args) {
            // TODO code application logic here
                try{
                Runtime run = Runtime.getRuntime();
              Process pro = run.exec("C:\\Program Files\\ ... notepad.exe");
     
    // write this function should specify what to output in notepad
              writeToProgram();
     
     
                System.exit(1);
                int retcode = pro.waitFor();
            }
            catch(Exception e){
                System.out.println(e);
            }
        }
    // found this code somewhere on the web

    2 - Compiling a program : If I have a program (program1), that writes a Java program (program2) in netbeans, for example, and I want to compile and run what I wrote in program2, is there any function that allows us to "manipulate" another program using a Java program?

    Thanks!


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

    Default Re: How to open and manipulate another program?

    I'd like to write in it,
    Are you talking about entering character data into the window of the program like you would do from the keyboard?
    The Robot class can position the cursor and enter keystrokes.

    "manipulate" another program using a Java program?
    Can you explain what you mean by manipulate?
    The Runtime and Process classes can start and interact with other programs.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: How to open and manipulate another program?

    As Norm states you can do some things with Process, ProcessBuilder and Robot, but not everything. To have tight control over another program, you need to get close to the operating system, something that Java does not do well. Possible solutions include using JNI (I've not used), JNA (I've used quite a bit), and interfacing Java to the other program using an OS-specific scripting program such as AutoIt if this is in Windows (which I've used extensively). I've done quite a bit where I communicate with AutoIt scripts using Processes and the Sockets which can be obtained from them, and have AutoIt run the outside non-Java program.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to open and manipulate another program?

    Quote Originally Posted by Norm View Post
    Are you talking about entering character data into the window of the program like you would do from the keyboard?
    The Robot class can position the cursor and enter keystrokes.


    Can you explain what you mean by manipulate?
    The Runtime and Process classes can start and interact with other programs.
    1 - Yes, i was talking about entering character data (text)

    2 - by manipulate, i meant : a) save a file b) compile a file c) run a file

    Thanks for the answer; I'll take a look in the Robot Class

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

    Default Re: How to open and manipulate another program?

    2 - by manipulate, i meant : a) save a file b) compile a file c) run a file
    Yes, a java program can do those things itself. Sending instructions to another program to do them could possibly be done with the Robot class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to open and manipulate another program?

    Cool!

    I worked a bit with the Robot class and managed to do some stuff, thanks again!

    Another question : I assume that to type text using the Robot class may not be the simplest way; since every key is not that trivial (ie. h = KeyEvent.VK_H) So I thought about 2 simple ways

    1 - Create a method that converts a character to its KeyEvent (a = KeyEvent.VK_A)

    2 - type text in a notepad, open that notepad, CTRL + A (select all) , CTRL + C (Copy) , CTRL + V (paste) in the target document

    Any other ideas?

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

    Default Re: How to open and manipulate another program?

    converts a character to its KeyEvent
    1)A Map can associate one value with another.
    2) The Robot class should be able to do the keystrokes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Note pad source code open instead of executing java program?
    By clalrama in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 26th, 2012, 06:11 PM
  2. Replies: 1
    Last Post: August 3rd, 2012, 11:46 AM
  3. Open PDF
    By adec in forum Java Servlet
    Replies: 1
    Last Post: March 17th, 2012, 10:02 AM
  4. could not open `C:\Program Files\Java\jre6\lib\amd64\jvm.cfg
    By miaaa00 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 29th, 2011, 12:45 AM
  5. How Manipulate HTTP response?
    By nikos in forum Java Networking
    Replies: 1
    Last Post: October 7th, 2010, 12:22 PM