-2

I've been trying to add this code to my project :

public static void Save() throws HeadlessException, AWTException, IOException
{
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension Dim = tk.getScreenSize();
    Rectangle screenRect = new Rectangle(Dim);
    Robot r = new Robot();
    BufferedImage screenCapturedImage = r.createScreenCapture(screenRect);
    DateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
    Date date = new Date();
    String FileName = dateFormat.format(date);
    ImageIO.write(screenCapturedImage, "png", new File("/home/caio/Desktop/"+FileName+".png"));
}

it gives me this error :

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: RoboCore.Print.Save
    at Core.Start.main(Start.java:23)
Java Result: 1

I want to call this method to print the results on my screen, but this code won't work because it's not a main method, it seems very strange to me...

Caio Petrelli
  • 37
  • 1
  • 7
  • Are you getting any errors when you attempt to call the method from anywhere other than main? – Matthew Champion Mar 04 '14 at 10:35
  • how are you calling this method is not working? – Rahul Mar 04 '14 at 10:35
  • 1
    what do you mean by "wont work"? – nikis Mar 04 '14 at 10:36
  • `throws Exception`: not good. Please replace with specific exceptions. – Bathsheba Mar 04 '14 at 10:38
  • it gives me an error unless its a main method : Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: RoboCore.Print.Save at Core.Start.main(Start.java:23) Java Result: 1 – Caio Petrelli Mar 04 '14 at 10:39
  • @Bathsheba which Exception should I use? – Caio Petrelli Mar 04 '14 at 10:40
  • @CaioPetrelli `throws Exception` is not relevant to your immediate problem - it doesn't actually break anything, but it might make it harder to understand your program later. About your error: "uncompilable source code" means you have compile errors and your IDE lets you run programs with compile errors. How are you calling Save? – user253751 Mar 04 '14 at 10:42
  • @immibis im calling like : Save(); – Caio Petrelli Mar 04 '14 at 10:43
  • possible duplicate of [this] (http://stackoverflow.com/questions/16767950/uncompilable-source-code-erroneous-sym-type-java-io-file-getsamplev) – Sagar Gandhi Mar 04 '14 at 10:55

3 Answers3

1

public static void main(String args[])

{

 //remaining code write here;

}

user2273146
  • 1,602
  • 2
  • 14
  • 27
1

I've tried compiling and running your code, I don't get any issues when calling it from main:

public static void main(String[] args) {
   try {
      Save();
   } catch (Exception e) {
      // Handle the exception
   }
}
xlm
  • 6,854
  • 14
  • 53
  • 55
  • I've called from main just for a test but the method its actually called later on with user input from a UDP Server – Caio Petrelli Mar 04 '14 at 10:51
  • @CaioPetrelli interesting, please share more/relevant code where it is being called. – xlm Mar 04 '14 at 10:52
  • Does it save the image? because it seams like the try catch clause stoped the error but does not save the image – Caio Petrelli Mar 04 '14 at 10:54
  • @CaioPetrelli yes it does take screenshot correctly. I'm not sure of the greater context in which you are calling `Save()` so it makes it hard for us to help further without updating your Q. – xlm Mar 04 '14 at 10:55
  • so it gets a server UDP packet and returns that to a class witch checks if the server recived the string "TakeScreenShot" than calls the method – Caio Petrelli Mar 04 '14 at 10:58
  • it worked now god knows why... but it was missing some catch{} bits – Caio Petrelli Mar 04 '14 at 11:05
  • @CaioPetrelli glad it's working for you now. `catch` is necessary as `Save()` may throw various exceptions which you need to handle. – xlm Mar 04 '14 at 11:07
0
public static void save() throws AWTException, IOException
{
        Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension Dim = tk.getScreenSize();
    Rectangle screenRect = new Rectangle(Dim);
    Robot r = new Robot();
    BufferedImage screenCapturedImage = r.createScreenCapture(screenRect);
    Format dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
    Date date = new Date();
    String FileName = dateFormat.format(date);

    ImageIO.write(screenCapturedImage, "png", new File("/home/caio/Desktop/" + FileName + ".png"));
    }

try that code it worked for me.

your problem was in the ImageIO.write method, you added " where unneeded

all i changed was "/home/caio/Desktop/"+FileName+".png" to "/home/caio/Desktop/" + FileName + ".png"

and the date format

then call it by

public static void main(String[] args)
{
   try
   {
       save();
   }
   catch(Exception e)
   {
       System.out.println(e);
   }
}
Luke Melaia
  • 1,470
  • 14
  • 22