简体   繁体   中英

How do I automate a login of a client using Java?

So I've been trying to automatically login through a WinSCP client. I have all the of credentials saved in the client for when I open it. If I manually press Enter on my keyboard, the client will log in. I tried using the robot method credited to @Slanec

Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);

However, when I checked Java API this just seems to check whether or not a key is pressed by a user?

Anyways... this is the code I currently have. The code opens the command prompt and enters a specified file path to open up a specific application WinSCP. Once the application is open I want the Enter Key to be pressed so that I can log in. If I manually press the enter key during this process it will work fine. SO is it possible to have the java code press the enter for me so that the process is fully automatic? I added the program to terminate in 5 seconds simply for testing purposes. Any input would be greatly appreciated.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class runADP 
{

    public static void main(String[] args) 
    {
            try 
            {
                System.out.println("Opening WinSCP");
                Runtime runTime = Runtime.getRuntime();
                Process process = runTime.exec("C:\\Program Files (x86)\\WinSCP\\WinSCP.exe");
                try 
                {
                        Thread.sleep(5000);
                        Robot r = null;
                            try 
                            {
                                r = new Robot();
                                r.keyPress(KeyEvent.VK_ENTER);
                                r.keyRelease(KeyEvent.VK_ENTER);

                            } 
                            catch (AWTException e) 
                            {
                                e.printStackTrace();
                            }

                } 
                catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }
            System.out.println("Closing WinSCP");
            process.destroy();
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
 }

}

I figured out what was going on. The two keypress lines were being executed before the client could open. I simply added a thread.sleep command surrounded by a try and catch block and now the code works perfectly.

    public void go()
    {
        try 
        {
            System.out.println("Opening WinSCP");
            Runtime runTime = Runtime.getRuntime();
            Process process = runTime.exec("C:\\Program Files (x86)\\WinSCP\\WinSCP.exe");
            Robot r = null;
            try 
            {
                Thread.sleep(2000);
                r = new Robot();
                r.keyPress(KeyEvent.VK_ENTER);
                r.keyRelease(KeyEvent.VK_ENTER);

            } 
            catch (AWTException | InterruptedException e) 
            {
                e.printStackTrace();
            } 
        } 
        catch (IOException e)
        {
        e.printStackTrace();
        }
}

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