Passing vector of vectors to Java from C++ using JNI

While using JNI, it becomes a little bit of burden whenever you want to pass something more complex than simple types back to Java.


How to find files that have certain string in last line

Let’s say you have few files

string
|-- 1
|-- 2
|-- 3
|-- 4
|-- 5
|-- 6
|-- 7
|-- 8
`-- 9

And they may end with two different endings: aaa or bbb.

Finding list off files that have given string at the end can be done following way

> find string -name "*" -exec tail -v -n1 {} \; | grep "bbb" -B1

on macOS you have to go slightly different way

> find string -name "*" | xargs tail -1 | grep "bbb" -B1

Running Java code in R

Let’s say you have a Java class StringTest

package somepackage;

public class StringTest {
    public String changeString(String str) {
        return "I have changed the string: " + str;
    }
}

inside file

src
`-- somepackage
    `-- StringTest.java

and you want to run it in R. All you have to do is to compile the code.

> javac -d target src/somepackage/StringTest.java

You will get compiled version of your class – it will have class extension.

target
`-- somepackage
    `-- StringTest.class

This is the class you want to run. Let’s take a look at R.

> R
R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
...
...
> # if you don't have rJava installed yet, install it
> install.packages("rJava")
...
...
> library(rJava)
> # initialize Java environment
> .jinit()
>
> # we have to tell rJava where to look for our class
> .jaddClassPath(paste(getwd(),"/target", sep = ""))
>
> # we can create instance of our class
> obj <- .jnew("somepackage/StringTest")
>
> # and call the method with arguments
> result <- .jcall(obj, returnSig="Ljava/lang/String;", method="changeString", "Hello world!")
> result
[1] "I have changed the string: Hello world!"
>

Sometimes, it’s hard to say what is the signature of methods (e.g. it may have lots of arguments, strange return type). You can easily find the signature using javap.

> javap -cp target somepackage.StringTest
Compiled from "StringTest.java"
public class somepackage.StringTest {
  public somepackage.StringTest();
  public java.lang.String changeString(java.lang.String);
}

In case you have issues while installing rJava on macOS, make sure to take a look here: R 3.4, rJava, macOS and even more mess ;) and here: R, Java, rJava and macOS adventures.


And Now for Something Completely Different – Interwencje 2


Zbiory felietonów – one zawsze niosą ze sobą pewne ryzyka :(


Welcome back my friends :)


And Now for Something Completely Different – Król


Odczarowuje


Be careful with JVM library location while building JNI based code

In case you want to compile JNI based code on macOS, be careful. Pay attention to @rpath.

If you compile the code like this (it will work in Linux)

ld -L${JAVA_HOME}/lib/server -ljvm ...

you will end up with reference to /usr/local/lib/libjvm.dylib. You will end up with lots of issues as code might be running to some extent.

To make sure your code is properly linked with version you really want to use, make sure to use -rpath

ld -rpath ${JAVA_HOME}/lib/server -ljvm ...

This time, you will use libjvm.dylib that is coming from the location you are interested in.


And Now for Something Completely Different – Królestwo


Do kompletu to chyba brakuje jeszcze tylko Królewicza.


And Now for Something Completely Different – Wieczny Grunwald


Potrafi wciągnąć.


And Now for Something Completely Different – Modern Java in Action


If you have stuck with pre 1.8 Java but you have to go back to the future, it’s definitely worth it!


And Now for Something Completely Different – Serotonina


Po taniości :(


VNC – The first attempt to start Xvnc failed

At some point I was struggling with the strange issue. It came out of nowhere. VNC simply stopped working. Eventually, it turned out that reason was completely awkward. Missing directory on LD_LIBRARY_PATH. Yes, I know how it sounds, but it’s true. You can try it by yourself

> setenv LD_LIBRARY_PATH /some_fake_location
> vncserver -geometry 800x600

WARNING: The first attempt to start Xvnc failed, possibly because the font
catalog is not properly configured.  Attempting to determine an appropriate
font path for this system and restart Xvnc using that font path ...
Could not start Xvnc.


Xvnc TigerVNC 1.3.1 - built Nov 16 2016 13:38:44
Copyright (C) 1999-2011 TigerVNC Team and many others (see README.txt)
See http://www.tigervnc.org for information on TigerVNC.
Underlying X server release 11702000, The X.Org Foundation


Wed May  8 23:59:59 2019
 vncext:      VNC extension running!
 vncext:      Listening for VNC connections on all interface(s), port 5902
 vncext:      Listening for HTTP connections on all interface(s), port 5802
 vncext:      created VNC server for screen 0
(EE)
(EE) Backtrace:
(EE) 0: /usr/bin/Xvnc (xorg_backtrace+0x55) [0x5bb935]
(EE) 1: /usr/bin/Xvnc (0x400000+0x1bf7a9) [0x5bf7a9]
(EE) 2: /usr/lib64/libpthread.so.0 (0x2b8c5b8ad000+0xf370) [0x2b8c5b8bc370]
(EE) 3: /lib64/ld-linux-x86-64.so.2 (0x2b8c59945000+0x5f79) [0x2b8c5994af79]
(EE) 4: /lib64/ld-linux-x86-64.so.2 (0x2b8c59945000+0x86db) [0x2b8c5994d6db]
...
...
(EE) Segmentation fault at address 0x2b8c59b65de0
(EE)
Fatal server error:
(EE) Caught signal 11 (Segmentation fault). Server aborting
(EE)

Stopping node.js – an elegant way

If you want to stop node.js you have basically two ways:

– brute force (⌃ – C),
– gentle way, via socket inside your application.

I will show you how to do that using simple code (https://nodejs.org/en/about/).

/* app.js */
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Brute force

This is the simplest way, just kill your app inside console using Ctrl-C (macOS: ^-C). All we have to do is to start node (node app.js) and terminate it.

Gentle way

Here, we are using socket to listen for messages from client. We have to modify server a little bit, we have to add socket.

/* app.js */
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

const io = require('socket.io')(server);
io.on('connection', (socketServer) => {
  socketServer.on('stop', () => {
    process.exit(0);
  });
});

Make sure to install socket packages as well

> npm install socket.io
> npm install socket.io-client

Once we have server part, we can focus on client

/* app.stop.js */
const io = require('socket.io-client');
const socketClient = io.connect('http://127.0.0.1:3000/');

socketClient.on('connect', () => {
  socketClient.emit('stop');
  setTimeout(() => {
    process.exit(0);
  }, 1000);
});


What kind of programmer are you…
take this simple C based ‘loop’ test

You still remember good old days with your Atari/Commodore machine. You like to annotate the code and you miss line numbers so much

void main() {

loop:
  printf("Hello\n");
  goto loop;

}

You like it ‘low level’ style. In high school you were the master of assembler. There is nothing wrong about working with bare metal, close to the chip.

#include <setjmp.h>

jmp_buf ljenv;

void main() {

    setjmp(ljenv);
    printf("Hello\n");
    longjmp(ljenv, 0);

}

There is also nothing wrong with procedural languages. Do you recall ‘the father’ of clean procedural code – mister Pascal?

#define true 1

void main() {

  while( true ) {
    printf("Hello\n");
  }

}

You are the Z-generation coder. Scala is the language to rule them all. Recursion, recursion everywhere.

void loop() {
  printf("Hello\n");
  loop();
}

void main() {

  loop();

}

Invalid CSS after “body”: expected 1 selector or at-rule, was “{ “

This error drove me crazy for a while.

Find one difference :)

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css'

body { 
  padding: 0; 
}
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

body { 
  padding: 0; 
}

find without .git and .svn

> find . -not -iwholename '*.svn*' -type f -name "*"
> find . -not -iwholename '*.git*' -type f -name "*"

Apple keyboard symbols – how to type them

– U+2318
– U+2325
– U+21E7
– U+2303
– U+232B

If you don’t know how to type Unicode characters in mac OS, take a look here: Unicode frames in macOS.


Capture whole HTML page into PNG

1. Start Chrome
2. Open the page
3. Open DevTools (⌥⌘I)
4. Open the Command Menu (⇧⌘P)
5. Choose Capture full size screenshot


Python 3 Programming Specialization – Coursera


If you want to learn Python 3, I definitely suggest this specialization: Python 3 Programming Specialization. It consists of five courses, each focusing on different aspects of programming. It will guide you through the basics of Python 3 programming and will let you learn lost of various aspects of Python 3 based development: Python basics (primitive types, variables, loops), dealing with arrays and dictionaries, writing functions. You will learn mapping and filtering of data. During the course you will have to work with REST and JSON APIs – these are the basis for the web projects. There are sections devoted to file operations, CSV files, parsing JSON data, accessing web content from Python code – summarizing, it covers lots and lots of stuff related to Python.

Even though I work with Python on daily basis, I have decided to go through all these courses to refresh few things, to learn concepts I have never used, and to systemize my knowledge.

You can find my notes here: and . These notes will give you the felling of how many topics are covered during the course. It’s really well prepared material, I must say.

When it comes to the delivery, I think it’s a top notch lecture. Teachers go through all the concepts explaining them in details. Material is divided into short, encapsulated sections. It helps to learn if you can’t devote too much time at one go. Apart from the lectures there are exercises, practice materials, and projects that you are obliged to finish before passing each course.

There is one thing, that might be a little bit of a struggle for complete newcomers to Python 3. If you have no experience at all, I think that material complexity elevates a little bit at the very end. It might be challenging for people who are not familiar with coding. However, spending a little bit of time here and there should be enough to finish the course.

When it comes to summarizing Python 3 Programming Specialization in once sentence – definitely worth the time and money.

There are many options to write Python 3 code. You can use regular text editor (e.g. Sublime Text, TextMate or even Vim). However, I strongly suggest PyCharm (Community Edition is more than enough).


List all shared libraries loaded by the process

> cat /proc/PROC_ID/maps | sed 's/\s\s*/ /g' | cut -f6 -d' ' | grep '\.so' | sort -u

or

> lsof -p PROC_ID | grep "\.so" | sed 's/\s\s*/ /g' | cut -f9 -d' ' | sort -u

←Older