Tuesday, June 29, 2021

Java ldd


For a C++ library, we can use command ldd to list the dependencies. There isn't such a official tool for the Java. However, if the source code is available, we can build the JAR using javac with the verbose mode turned on. The location of all the dependent JARs will be listed during the build.

Wednesday, June 16, 2021

Android 10: Enable the built-in screen recorder


 

Note: This may NOT be supported by all phones.

 

1. Turn on the Developer Options on the phone. (Google it to see how to...)

2. Connection the phone to the computer.

3. On the computer, install the "adb" application.

4. On the computer, open a terminal, run command "adb devices" to verify that the phone is connected correctly.

5. On the computer, run command "adb shell settings put global settings_screenrecord_long_press true" to enable the screen recorder on the phone.

6. On the phone, long press the power butter to bring up the menu: Power off/Restart/Screenshot

7. Long press the "Screenshot" item in the above menu. A "Start Recording" dialog is popped up.

8. Click on the "Start Recording" button to start.


Friday, May 7, 2021

MySQL DB: mixed charset problem


The problem:

Client application A writes character ¢ into MySQL DB.

Client application B reads it from DB and gets ¢.

If we look inside the DB to see how it is stored using function hex() -- select hex(fieldname) from tablename, we can see it is stored as \xC2\xA2 in hex form.

 

The reason:

The MySQL DB server is using ISO-8859-1 (latin1).

Client A writes characters to MySQL DB server in UTF-8. But the server incorrectly thinks the connection is using latin1. So one character \xA2 is stored as \xC2\xA2 (UTF-encoding).

Client B reads characters from the server in UTF-8. And the server correctly identifies the connection is using UTF-8. So the sever will encode the 2 hex characters into \xC3\x82\xC2\xA2 and send back 4 characters as the UTF-2 encoding for the 2 characters stored in the table.

 

To fix:

Make sure the charset settings of the connection and the client match the what the client actually uses.

The SQL command to check the charsets is:

SHOW SESSION VARIABLES LIKE 'character\_set\_%';

The SQL command to change the charsets to UTF-8 is:

SET NAMES utf8


Wednesday, May 5, 2021

Spring Boot: escape HTML and non-ASCII characters in Json response


Create an ObjectMapper object and let Spring Boot use our ObjectMapper object with the @Bean and @Primary annotations.

In our configuration bean:

@Configuration
public class MyConfiguration {
 
  @Bean
  @Primary
  public ObjectMapper objectMapper() {
    // create our own ObjectMapper object.
    ObjectMapper objMapper = new ObjectMapper();
 
    // escape all non-ASCII characters
    objMapper.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);

    // definition of the HTML characters to escape
    final class HtmlCharsToEscape extends CharacterEscapes
    {
      private final int[] asciiEscapes;
    
      public
HtmlCharsToEscape()
      {
        // get the set of default escaped characters (double-quote, backslash etc)
        int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
        // and force escaping of HTML special characters:
        esc['<'] = CharacterEscapes.ESCAPE_STANDARD;
        esc['>'] = CharacterEscapes.ESCAPE_STANDARD;
        esc['&'] = CharacterEscapes.ESCAPE_STANDARD;
        esc['\''] = CharacterEscapes.ESCAPE_STANDARD;
        asciiEscapes = esc;
      }

      @Override public int[] getEscapeCodesForAscii() {
        return asciiEscapes;
      }

      @Override public SerializableString getEscapeSequence(int ch) {
        // no CUSTOM escaping needed:
        return null;
      }
    }

    // Escape the HTML special characters.
    objMapper.getFactory().setCharacterEscapes(new
HtmlCharsToEscape());
 
    // This will be picked up the Spring Boot.
    return objMapper; 
  }
}



Tuesday, May 4, 2021

Windows 10 shortcut: move a window to another monitor


If the computer connects to multiple monitors, use this shortcut combination to move the selected window to the left or right monitor:

Windows-Key + Shift + Left/Right-Arrow


Monday, May 3, 2021

Elipse: debug Maven project with JUnit


1. Set the breakpoints.

2. Right click on the JUnit test Java class. On the popup menu, select Debug as... -> JUnit.

Sunday, May 2, 2021

Linux boot into Busybox - Gave up waiting for root device


One possible fix is to add the rootwait to the boot parameters.

 

1. Add parameter rootwait to GRUB_CMDLINE_LINUX_DEFAULT in file /etc/default/grub:

 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash rootwait"

 

2. Run command to update grub:

$ sudo update-grub

 

(Note: Another related parameter is rootdelay.)

 

 


Wednesday, April 28, 2021

Windows: install msi as non Administrator user


 Run command:

> msiexec /a ThePackage.msi


When the installation program asks for the destination location, pick a fold you can write into.


Thursday, April 22, 2021

HTML dialog Tag


dialog.html:

<html>

<body>

<button onclick="openDialog()">Open Dialog</button>

<dialog id="myDialog"><iframe src="dialogbox.html"></iframe></dialog>

<script>

function openDialog() {

  document.getElementById("myDialog").showModal();

}

</script>

</body>

</html>


dialogbox.html:

<html>

<body>

<button onclick="parent.document.getElementById('myDialog').close()">

Click to close

</button>

</body>

</html>

 

Wednesday, March 24, 2021

Docker commands and Dockerfile examples


To install docker on fedora

$ sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo

$ sudo dnf install docker-ce docker-ce-cli containerd.io 

$ sudo systemctl start docker

$ sudo docker run hello-world


To build and run an image

First, create a Dockerfile under the current directory (see below).

$ sudo docker build -t myimagename:myimageversion .

$ sudo docker  run -p 8888:8080 myimagename:myimageversion

or 

$ sudo docker run --net=host myimagename:myimageversion


Other commands

$ sudo docker image ls

$ sudo docker rmi image-hash

$ sudo docker ps -as

$ sudo docker rm container-hash

$ sudo docker run -it myimagename:myimageversion bash


Dockerfile of adding self CA and Bouncy Castle jar

FROM tomcat:9.0.44-jdk8

# Add myCA certificate

ADD myCA.crt /usr/local/share/ca-certificates/

RUN chmod 644 /usr/local/share/ca-certificates/myCA.crt && update-ca-certificates

# Add Bouncy Castle provider

RUN echo '' >> "/usr/local/openjdk-8/jre/lib/security/java.security"

RUN echo 'security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider' >> "/usr/local/openjdk-8/jre/lib/security/java.security"

ADD bcprov-jdk15on.jar /usr/local/openjdk-8/jre/lib/ext/

# Add web app

ADD myapp.war  /usr/local/tomcat/webapps/

EXPOSE 8080

CMD ["catalina.sh", "run"]



 
Get This <