• «PHP» logo
  • «Python» logo
  • «Java» logo
  • «C#» logo
  • «Ruby» logo
  • «Golang» logo
  • «JavaScript» logo
Bypass captcha in JAVA

Bypass captcha in JAVA

Implement Java library for fast captcha verification bypass. Simple API for captcha solve.

Java package for easy integration with the API of 2Captcha captcha solving service to bypass reCAPTCHA, hCaptcha, Arkose captcha, Geetest and solve any other captchas

Quick start

Installation

The script package can be installed using the package installer or manually

Maven Central Repository

2captcha-java artifact is available in Maven Central

We invite you to explore our GitHub repository where you can find libraries and modules for easy integration with our API.

Configuration

Description of all the necessary parameters to configure the installed package

An instance of the TwoCaptcha class can be created like this:

TwoCaptcha solver = new TwoCaptcha('YOUR_API_KEY');

You also have the ability to customize some of the options for the created instance:

solver.setSoftId(123);
solver.setCallback("https://your.site/result-receiver");
solver.setDefaultTimeout(120);
solver.setRecaptchaTimeout(600);
solver.setPollingInterval(10);
TwoCaptcha instance options
OptionDefault valueDescription
softId-your software ID obtained after publishing in 2Captcha software catalog
callback-URL of your web-sever that receives the captcha recognition result. The URL should be first registered in pingback settings of your account
defaultTimeout120Polling timeout in seconds for all captcha types except reCAPTCHA. Defines how long the module tries to get the answer from res.php API endpoint
recaptchaTimeout600Polling timeout for reCAPTCHA in seconds. Defines how long the module tries to get the answer from res.php API endpoint
pollingInterval10Interval in seconds between requests to res.php API endpoint, setting values less than 5 seconds is not recommended
IMPORTANT: once callback is defined for TwoCaptcha instance, all methods return only the captcha ID and DO NOT poll the API to get the result. The result will be sent to the callback URL. To get the answer manually use getResult method

Solve captcha

When you submit any image-based captcha use can provide additional options to help 2Captcha workers to solve it properly

Captcha options
OptionDefault valueDescription
numeric0Defines if captcha contains numeric or other symbols see more info in the API docs
minLength0minimal answer length
maxLength0maximum answer length
phrase0defines if the answer contains multiple words or not
caseSensitive0defines if the answer is case sensitive
calc0defines captcha requires calculation
lang-defines the captcha language, see the list of supported languages
hintImg-an image with hint shown to workers with the captcha
hintText-hint or task text shown to workers with the captcha

Basic example

Example below shows a basic solver call example with error handling.

Normal captcha = new Normal();
captcha.setFile("path/to/captcha.jpg");
captcha.setMinLen(4);
captcha.setMaxLen(20);
captcha.setCaseSensitive(true);
captcha.setLang("en");

try {
    solver.solve(captcha);
    System.out.println("Captcha solved: " + captcha.getCode());
} catch (Exception e) {
    System.out.println("Error occurred: " + e.getMessage());
}
simpleCaptcha

To bypass a normal captcha (distorted text on image) use the following method. This method also can be used to recognize any text on the image.

Normal captcha = new Normal();
captcha.setFile("path/to/captcha.jpg");
captcha.setNumeric(4);
captcha.setMinLen(4);
captcha.setMaxLen(20);
captcha.setPhrase(true);
captcha.setCaseSensitive(true);
captcha.setCalc(false);
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Type red symbols only");
textCaptcha

This method can be used to bypass a captcha that requires to answer a question provided in clear text.

Text captcha = new Text();
captcha.setText("If tomorrow is Saturday, what day is today?");
captcha.setLang("en");
recaptchaV2

Use this method to solve reCAPTCHA V2 and obtain a token to bypass the protection.

ReCaptcha captcha = new ReCaptcha();
captcha.setSiteKey("6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-");
captcha.setUrl("https://mysite.com/page/with/recaptcha");
captcha.setInvisible(true);
captcha.setAction("verify");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
recaptchaV3

This method provides reCAPTCHA V3 solver and returns a token.

ReCaptcha captcha = new ReCaptcha();
captcha.setSiteKey("6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-");
captcha.setUrl("https://mysite.com/page/with/recaptcha");
captcha.setVersion("v3");
captcha.setAction("verify");
captcha.setScore(0.3);
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
funCaptcha

FunCaptcha (Arkoselabs) solving method. Returns a token.

FunCaptcha captcha = new FunCaptcha();
captcha.setSiteKey("69A21A01-CC7B-B9C6-0F9A-E7FA06677FFC");
captcha.setUrl("https://mysite.com/page/with/funcaptcha");
captcha.setSUrl("https://client-api.arkoselabs.com");
captcha.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");
captcha.setData("anyKey", "anyValue");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
geeTest

Method to solve GeeTest puzzle captcha. Returns a set of tokens as JSON.

GeeTest captcha = new GeeTest();
captcha.setGt("f2ae6cadcf7886856696502e1d55e00c");
captcha.setApiServer("api-na.geetest.com");
captcha.setChallenge("12345678abc90123d45678ef90123a456b");
captcha.setUrl("https://mysite.com/captcha.html");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
hCaptcha

Use this method to solve hCaptcha challenge. Returns a token to bypass captcha.

HCaptcha captcha = new HCaptcha();
captcha.setSiteKey("10000000-ffff-ffff-ffff-000000000001");
captcha.setUrl("https://www.site.com/page/");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
keyCaptcha

Token-based method to solve KeyCaptcha.

KeyCaptcha captcha = new KeyCaptcha();
captcha.setUserId(10);
captcha.setSessionId("493e52c37c10c2bcdf4a00cbc9ccd1e8");
captcha.setWebServerSign("9006dc725760858e4c0715b835472f22");
captcha.setWebServerSign2("2ca3abe86d90c6142d5571db98af6714");
captcha.setUrl("https://www.keycaptcha.ru/demo-magnetic/");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
capy

Token-based method to bypass Capy puzzle captcha.

Capy captcha = new Capy();
captcha.setSiteKey("PUZZLE_Abc1dEFghIJKLM2no34P56q7rStu8v");
captcha.setUrl("https://www.mysite.com/captcha/");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");
grid

Grid method is originally called Old reCAPTCHA V2 method. The method can be used to bypass any type of captcha where you can apply a grid on image and need to click specific grid boxes. Returns numbers of boxes.

Grid captcha = new Grid();
captcha.setFile("path/to/captcha.jpg");
captcha.setRows(3);
captcha.setCols(3);
captcha.setPreviousId(0);
captcha.setCanSkip(false);
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Select all images with an Orange");
canvas

Canvas method can be used when you need to draw a line around an object on image. Returns a set of points' coordinates to draw a polygon.

Canvas captcha = new Canvas();
captcha.setFile("path/to/captcha.jpg");
captcha.setPreviousId(0);
captcha.setCanSkip(false);
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Draw around apple");
clickCaptcha

ClickCaptcha method returns coordinates of points on captcha image. Can be used if you need to click on particular points on the image.

Coordinates captcha = new Coordinates();
captcha.setFile("path/to/captcha.jpg");
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Select all images with an Orange");
rotateCaptcha

This method can be used to solve a captcha that asks to rotate an object. Mostly used to bypass FunCaptcha. Returns the rotation angle.

Rotate captcha = new Rotate();
captcha.setFile("path/to/captcha.jpg");
captcha.setAngle(40);
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Put the images in the correct way up");

Other methods

Additional valid methods used during the work of the main scripts

send / getResult

These methods can be used for manual captcha submission and answer polling.

String captchaId = solver.send(captcha);

Thread.sleep(20 * 1000);

String code = solver.getResult(captchaId);

balance

Use this method to get your account's balance.

double balance = solver.balance();

report

Use this method to report good or bad captcha answer.

solver.report(captcha.getId(), true); // captcha solved correctly
solver.report(captcha.getId(), false); // captcha solved incorrectly

Error handling

Possible variants of standard errors returned by the service when processing requests

If case of an error captcha solver throws an exception. It's important to properly handle these cases. We recommend to use try/catch to handle exceptions.

try {
    solver.solve(captcha);
} catch (ValidationException e) {
    // invalid parameters passed
} catch (NetworkException e) {
    // network error occurred
} catch (ApiException e) {
    // api respond with error
} catch (TimeoutException e) {
    // captcha is not solved so far
}

Other languages for integration with the service API