简体   繁体   English

从C#自动化旧版Java应用程序

[英]Automate a legacy Java application from C#

I have a very old legacy Java application that I'd like to automate from C#. 我有一个非常古老的遗留Java应用程序,我想从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. 我可能'反编译'.jar文件,但我也不是java程序员,我不认为有权访问源代码(没有注释)会让我更进一步。

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): 到目前为止,我设法(有点成功)使用以下代码自动化应用程序的登录对话框(我在循环中轮询,因为可能有多个Java应用程序实例):

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. 现在的问题是我无法抓取Java窗口的内容。 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. 所以我不想在Java窗口中查看登录失败消息。

Is there a way to access the actual content (text) of the Java window? 有没有办法访问Java窗口的实际内容(文本)?

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. 此外,Java应用程序的所有实例共享相同的PID,因为实际上它只是一个运行所有应用程序的java.exe进程。 Is there a way to differentiate between the applications? 有没有办法区分应用程序? It obviously can't be done using the PID ... 显然无法使用PID完成...

It is helluva easy to decompile the jar file. 很容易反编译jar文件。 Just download the (currently) most widely used java decompiler , click File -> Open and open your jar file. 只需下载(当前)使用最广泛的java反编译器 ,单击文件 - >打开并打开您的jar文件。 You can then browse the code in the main window, and copy and paste it to actual files. 然后,您可以在主窗口中浏览代码,并将其复制并粘贴到实际文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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