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!)
Code :
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!
Re: How to open and manipulate another program?
Quote:
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.
Quote:
"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.
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.
Re: How to open and manipulate another program?
Quote:
Originally Posted by
Norm
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
Re: How to open and manipulate another program?
Quote:
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.
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?
Re: How to open and manipulate another program?
Quote:
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.