简体   繁体   中英

Automate a legacy Java application from C#

I have a very old legacy Java application that I'd like to automate from C#. The problem is we don't have the source code of the application and the programmer has long since left the company. I could probably 'decompile' the .jar file but I'm no java programmer either, and I don't think that having access to the source code (without comments) will get me any further.

So far i managed to (kinda successfully) automate the application's login dialog, using the following code (I'm polling in a loop because there might be more than one instance of the Java app):

while (true) {
            var processes = Process.GetProcesses().Where(p => p.MainWindowTitle.Equals("Title of Java Login Window"));
            foreach (var process in processes) {
                var handle = process.MainWindowHandle;
                SetForegroundWindow(handle);

                await Task.Delay(500);

                SendKeys.SendWait("the password");
                SendKeys.SendWait("{TAB}");
                SendKeys.SendWait("the username");
                SendKeys.SendWait("{ENTER}");

                await Task.Delay(3000);
            }
            await Task.Delay(500);
        }

Now the problem is I'm unable to grab the contents of the Java window. For example, there might be a problem with the login, like a wrong user or password. So I wan't to check the Java window for login failure messages.

Is there a way to access the actual content (text) of the Java window?

Also, all instances of the Java Application share the same PID, because in reality it's just one java.exe process running all the applications. Is there a way to differentiate between the applications? It obviously can't be done using the PID ...

It is helluva easy to decompile the jar file. Just download the (currently) most widely used java decompiler , click File -> Open and open your jar file. You can then browse the code in the main window, and copy and paste it to actual files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM