Fixed non-cmd console issue in Java example + added more build info (#146)

* Changed Console to BufferedReader in Java example, fixes https://github.com/tdlib/td/issues/141. Added more info about building Java example.
This commit is contained in:
hekkup 2018-03-20 16:48:47 +02:00 committed by Aliaksei Levin
parent e0924ea333
commit 4acf428c9b
2 changed files with 27 additions and 12 deletions

View File

@ -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 <path to TDLib sources>
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=<VCPKG_DIR>/scripts/buildsystems/vcpkg.cmake
Then you can build this example:
```
cd <path to TDLib sources>/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.

View File

@ -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<Integer, TdApi.BasicGroupFullInfo> basicGroupsFullInfo = new ConcurrentHashMap<Integer, TdApi.BasicGroupFullInfo>();
private static final ConcurrentMap<Integer, TdApi.SupergroupFullInfo> supergroupsFullInfo = new ConcurrentHashMap<Integer, TdApi.SupergroupFullInfo>();
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 <chatId> - GetChat, me - GetMe, sm <chatId> <message> - 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);