diff --git a/example/java/README.md b/example/java/README.md index b3185fb6..1267da1b 100644 --- a/example/java/README.md +++ b/example/java/README.md @@ -3,7 +3,7 @@ To run this example, you will need installed JDK >= 1.6. For Javadoc documentation generation PHP is needed. -TDLib should be prebuilt for using with Java and installed to local subdirectory `td/`: +TDLib should be prebuilt for using with Java and installed to local subdirectory `td/` as follows: ``` cd mkdir jnibuild @@ -13,6 +13,8 @@ cmake --build . --target install ``` If you want to compile TDLib for 64-bit Java on Windows using MSVC, you will also need to add `-A x64` option to CMake. +In Windows, use Vcpkg toolchain file by adding parameter -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake + Then you can build this example: ``` cd /example/java @@ -32,4 +34,6 @@ java '-Djava.library.path=.' org/drinkless/tdlib/example/Example If you get "Could NOT find JNI ..." error from CMake, you need to specify to CMake path to the installed JDK, for example, "-DJAVA_HOME=/usr/lib/jvm/java-8-oracle/". -If you get java.lang.UnsatisfiedLinkError with "Can't find dependent libraries", you may also need to copy some dependent shared libraries to `bin/`. +If you get java.lang.UnsatisfiedLinkError with "Can't find dependent libraries", you may also need to copy some dependent shared libraries to bin/. + +In case you compiled the example as 32-bit version, you may need to give -d32 parameter to Java. diff --git a/example/java/org/drinkless/tdlib/example/Example.java b/example/java/org/drinkless/tdlib/example/Example.java index c9af2821..cdd5d874 100644 --- a/example/java/org/drinkless/tdlib/example/Example.java +++ b/example/java/org/drinkless/tdlib/example/Example.java @@ -10,9 +10,10 @@ import org.drinkless.tdlib.Client; import org.drinkless.tdlib.Log; import org.drinkless.tdlib.TdApi; -import java.io.Console; import java.io.IOError; import java.io.IOException; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.util.NavigableSet; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; @@ -49,7 +50,6 @@ public final class Example { private static final ConcurrentMap basicGroupsFullInfo = new ConcurrentHashMap(); private static final ConcurrentMap supergroupsFullInfo = new ConcurrentHashMap(); - private static final Console console = System.console(); private static final String newLine = System.getProperty("line.separator"); private static final String commandsLine = "Enter command (gcs - GetChats, gc - GetChat, me - GetMe, sm - SendMessage, lo - LogOut, q - Quit): "; @@ -106,17 +106,17 @@ public final class Example { client.send(new TdApi.CheckDatabaseEncryptionKey(), new AuthorizationRequestHandler()); break; case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: { - String phoneNumber = console.readLine("Please enter phone number: "); + String phoneNumber = promptString("Please enter phone number: ", false); client.send(new TdApi.SetAuthenticationPhoneNumber(phoneNumber, false, false), new AuthorizationRequestHandler()); break; } case TdApi.AuthorizationStateWaitCode.CONSTRUCTOR: { - String code = console.readLine("Please enter authentication code: "); + String code = promptString("Please enter authentication code: ", false); client.send(new TdApi.CheckAuthenticationCode(code, "", ""), new AuthorizationRequestHandler()); break; } case TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR: { - String password = console.readLine("Please enter password: "); + String password = promptString("Please enter password: ", false); client.send(new TdApi.CheckAuthenticationPassword(password), new AuthorizationRequestHandler()); break; } @@ -165,9 +165,24 @@ public final class Example { } return chatId; } + + private static String promptString(String prompt, boolean printNewline) { + System.out.print(prompt); + if (printNewline) { + System.out.print(newLine); + } + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String str = ""; + try { + str = reader.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return str; + } private static void getCommand() { - String command = console.readLine(commandsLine); + String command = promptString(commandsLine, true); String[] commands = command.split(" ", 2); try { switch (commands[0]) { @@ -267,10 +282,6 @@ public final class Example { } public static void main(String[] args) throws InterruptedException { - if (console == null) { - System.err.println("This example requires Console for user interaction. On Windows cmd or PowerShell should be used"); - System.exit(1); - } // disable TDLib log Log.setVerbosityLevel(0);