user4514869
user4514869

Reputation:

Incompatible types error Java

So, I have this line of code

ScreenCapture.main(String[].class);

in file "1" and it is linking to this file "2"

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;

class ScreenCapture
{
   public static void main (String args[]) throws
        AWTException, IOException
   {
      System.out.print(".");
      BufferedImage screencapture = new Robot().createScreenCapture(
            new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
      Expo.delay(2000);
      System.out.print(".");
      File file = new File("Agreement.jpg");
      ImageIO.write(screencapture, "jpg", file);
      Expo.delay(4000);
      System.out.print(".");
   }
}

and this is the error I get in file 1

WarandPeace.java:21: error: incompatible types: Class<String[]> cannot be
converted to String[]

My end goal is to take a screenshot of the screen when the user completes a specific action in file 1. I have the screenshot file working (file 2), but no matter what I do, that error keeps being annoying (file 1). Any solutions?

Upvotes: 0

Views: 933

Answers (3)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your error is self-explanatory. A String array class is not the same as a String array itself. But more importantly, that code should not be in a static main method if you want other code to use it since the main method should be used for starting a program, not for utility methods. Learn proper OOPS concepts, create classes and call non-static methods of proper objects. If this were my code, I'd create a method to capture the screen and have it return a BufferedImage. Then other code can decide what to do with the BufferedImage.

Upvotes: 4

Elliott Frisch
Elliott Frisch

Reputation: 201409

Modify "file 1".

ScreenCapture.main(String[].class);

should be

String[] args = { "one" }; // <- for one.

or

String[] args = new String[0]; // <- for none.

and then

ScreenCapture.main(args);

main() takes an array of String(s) (not the Class<String[]> which is the type of a String[]).

Finally, on one line -

ScreenCapture.main(new String[] {"one", "two"});

Upvotes: 0

icyrock.com
icyrock.com

Reputation: 28588

The method in file 2 has this signature:

public static void main (String args[]) throws
        AWTException, IOException

You need to match it. You can do e.g. this:

ScreenCapture.main(new String[] {"param1", "param2"});

or, since you are not using the arguments at all, you can just:

ScreenCapture.main(new String[] {});

I agree with the other answer though...

Upvotes: 1

Related Questions