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
}
}
Question
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...
Solution
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
}
}
OTHER TIPS
public static void main(String args[])
{
//remaining code write here;
}
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);
}
}