Skip to content

Commit

Permalink
Fixed non-cmd console issue in Java example + added more build info (t…
Browse files Browse the repository at this point in the history
…dlib#146)

* Changed Console to BufferedReader in Java example, fixes tdlib#141. Added more info about building Java example.
  • Loading branch information
hekkup authored and levlam committed Mar 20, 2018
1 parent e0924ea commit 4acf428
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
8 changes: 6 additions & 2 deletions example/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
31 changes: 21 additions & 10 deletions example/java/org/drinkless/tdlib/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
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;
Expand Down Expand Up @@ -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): ";

Expand Down Expand Up @@ -106,17 +106,17 @@ private static void onAuthorizationStateUpdated(TdApi.AuthorizationState authori
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;
}
Expand Down Expand Up @@ -165,9 +165,24 @@ private static long getChatId(String arg) {
}
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]) {
Expand Down Expand Up @@ -267,10 +282,6 @@ private static void sendMessage(long chatId, String message) {
}

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);
Expand Down

0 comments on commit 4acf428

Please sign in to comment.