Andre Angelo
Andre Angelo

Reputation: 281

Robot Class - Unhandled exception type AWTException

So, I was was tinkering around with the Robot Class in Java. I am a very new Java Programmer, but I have deeper roots in other languages. Here's my code:

import java.awt.*;

public class Main {

    public static void main(String[] args) {
        Robot bot = new Robot();
        bot.mouseMove(50, 50);  
    }
}

All I was trying to do was see if I can control the mouse, as in, moving it to 50, 50. However, in Eclipse it puts a red X next to

Robot bot = new Robot();

..saying..

Unhandled exception type AWTException

And won't let me run it. Can anyone help me figure out why this is happening?

Upvotes: 2

Views: 10551

Answers (2)

Justin McDonald
Justin McDonald

Reputation: 2166

you need to try/catch exceptions:

import java.awt.*;
public class Main{

public static void main(String[] args) {

    try
    {
    Robot bot = new Robot();
    bot.mouseMove(50, 50);  
    }
    catch (AWTException e)
    {
    e.printStackTrace();
    }
}
}

or throw the exception:

import java.awt.*;
public class Main throws AWTException{

public static void main(String[] args) {
    Robot bot = new Robot();
    bot.mouseMove(50, 50);  
}
}

Upvotes: 7

Eliasz Kubala
Eliasz Kubala

Reputation: 3896

import java.awt.*;

public class remote{

     public static void main(String[] args) {


         try
            {
            Robot bot = new Robot();
            bot.mouseMove(50, 50);
            trace("działam");
            }
            catch (AWTException e)
            {
            e.printStackTrace();
            }
        }

     public static void trace(String s){
         System.out.print(s.toString());
     }

}

It's work for me not before i added bot.mouseMove(50, 50) to TRY instruction.

Upvotes: 2

Related Questions