Wednesday, May 9, 2018

Making mouse pointer movement smooth with touchpad in Kubuntu 18.04

I have noticed that my mouse pointer (rather touchpoint pointer) moves in a jumpy manner while trying Kubuntu 18.04 Bionic Beaver on my Acer laptop.
After some research I've managed to fix this. Here's how:
1. Open Konsole
2. Run this command:
sudo apt install xserver-xorg-input-synaptics
(don't forget to enter your password :)
3. Reboot

That's basically it. After this you can open System Settings, go to Input Devices / Touchpad settings and further adjust Noise Cancellation if you really want to.

Here's a little video I made about it in Kdenlive (it was an excuse to spend some time learning the basics of Kdenlive, really :).


Makes me wonder why it isn't installed out of the box :|

Thursday, December 28, 2017

Command line classpath is ignored when using -jar

I was a little shocked today by my discovery of -classpath (-cp) parameter being ignored silently by Java Virtual Machine, when -jar is provided. Look at this (my jar file prints out its working directory and classpath):
java -cp /home:./ -jar ./build/jarclassscanning.jar

Working Directory = /home/me/projects/jarclassscanning
----------------
Classpath elements:
/home/me/projects/jarclassscanning/build/jarclassscanning.jar
----------------

However, if I run the same jar without -jar parameter, here's what it prints out (I've added a couple of directories to -cp just to show that it works):
java -cp /home:./:./build/jarclassscanning.jar my.package.jarclassscanning.BaseClass

Working Directory = /home/me/projects/jarclassscanning
----------------
Classpath elements:
/home/
/home/me/projects/jarclassscanning/
/home/me/projects/jarclassscanning/build/jarclassscanning.jar
----------------

So if your jar uses classes from other jars (or just plain old .class files) and you have to specify classpath, one solution is to add classpath elements to the manifest file - https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html. Keep in mind, however, that you've got to use spaces to separate them, not colons.

Also I've ran into another thing - when specifying jar files as classpath elements using Class-Path field of the manifest file using relative paths, they had to be relative to the location of my jar (the one that I give to JVM using -jar).

So, since my folder structure looked like this:
build
    myjar.jar
jdbc
    postgresql-42.1.4.jar
amqp
    amqp-client-4.2.0.jar
sl4j
    slf4j-simple-1.7.25.jar
    slf4j-api-1.7.25.jar

The Class-Path field of the manifest in myjar.jar ended up looking like this (note the ".."'s):
Class-Path: ../jdbc/postgresql-42.1.4.jar ../amqp/amqp-client-4.2.0.jar ../slf4j/slf4j-simple-1.7.25.jar ../slf4j/slf4j-api-1.7.25.jar

Tuesday, December 26, 2017

Apache Ant modifies classpath

Just a quick note - I have discovered this thing while digging into Java class loading / reflection. When running my test program from command line, like so:
java -ea -cp ./build/jarclassscanning.jar my.package.jarclassscanning.BaseClass
(yes, I specify my main class directly and just add my jar file to the classpath)

my classpath contains what I expect it to contain:
/home/me/projects/jarclassscanning/build/jarclassscanning.jar

But if I use my simplistic ant build.xml, which contains target run:
<target name="run">
  <java classname="my.package.jarclassscanning.BaseClass">
    <arg value="-ea"/>
    <classpath>
      <pathelement location="./build/jarclassscanning.jar"/>
    </classpath>
  </java>
</target>

this is what my classpath becomes (printed out from the test program itself):
/home/me/apps/apache-ant-1.9.7/lib/ant-launcher.jar

However, if I add fork="true" to the java task (just as you should if you use jar attribute, which I don't):
<target name="run">
  <java classname="my.package.jarclassscanning.BaseClass" fork="true">
    <arg value="-ea"/>
    <classpath>
      <pathelement location="./build/jarclassscanning.jar"/>
    </classpath>
  </java>
</target>

everything's back to normal:
/home/me/projects/jarclassscanning/build/jarclassscanning.jar

Saturday, February 18, 2017

A C pitfall

Consider the following code:

#include ‹stdio.h›

int main(void)
{
 printf("This line ends with backslash (\\)\n"); // Print \
 printf("This is the second line\n");
 printf("This is the third line\n");
 return 0;
}

gcc 5.4.0 I have on my Xubuntu 16.04 box compiles this code without errors or warnings.
What do you think the code prints out when run?
This:

This line ends with backslash (\)
This is the third line

The second line is silently ignored by the compiler. The reason is that the line before it ends with backslash symbol (\). Even if the backslash belongs to the comment, gcc still considers the second line to be continuation of the line before it, and exactly because of this the second line is considered to be the continuation of the comment and is ignored.

Worst of all there's no warning or error, although ideone tells me that at least gcc 6.3 refuses to compile the code:
prog.c: In function ‘main’:
prog.c:5:50: error: multi-line comment [-Werror=comment]
  printf("This line ends with backslash (\\)\n"); // Print \
                                                  ^
cc1: all warnings being treated as errors

But anyway this is something to be aware of, especially if you're not sure about the way your compiler treats this case.

Wednesday, July 13, 2016

Enums in Java

If you're familiar with C or C++, you may tend to think of enums as just integers. Which they basically are - in those languages, but not in Java.
Java enum is a special kind of class. What's the difference for you as a user?

It you just want a simple enum and don't care about real values behind enum constants, you can go ahead and just do as in old good C (almost, as C syntax is slightly different):
enum Color {RED, GREEN, YELLOW}
 . . .
Colors col = RED;
But if you try to assign an integer value to col, as you could in C, like so:
col = 2;
- the code fails to compile, which is a good thing, as you obviously want your value to be one of the predefined constants.
Well, actually you can't compile this in C++ either, but you can still force the compiler to build it by using a cast like this one:
col = (Color)2;
But that's beside the point. If you want your constants to have specific values, in C you could achieve that like this (note that this is C syntax, which is slightly different from Java):
enum Color {RED=5, YELLOW=10, GREEN=15};
If you want this in Java, you have to take into account that your enum is going to be a special kind of Java class, extending java.lang.Enum. Its syntax is also different from both C / C++ enums and normal Java classes:
enum Color
{
    RED(5), GREEN(10), YELLOW(15);
    private int col;
    private Color(int col) { this.col = col; }
    private int getValue() { return col; }
}
And this is how you use it:
Color c = Color.RED;
System.out.println(c);
which prints out
RED
But you can't just go and assign an integer value to your enum object:
Color c = 5;
System.out.println(c);
because
error: incompatible types: int cannot be converted to Color

There's much more to Java enums, check out these links for more details:

Monday, July 11, 2016

java.time

java.time is a package for handling date / time values, introduced in Java 8. Before that you had to use classes like java.util.Date or java.sql.Date, if you were dealing with SQL databases.

These are still available in Java 8, but I would rather avoid them, as they're not thread safe, don't support the notion of time zones, etc. You can use a third-party solution like Joda-Time, but if you're writing Java 8 code, I see no reason not to use buiilt-in java.time.

Here's an article from Java Magazine, introducing java.time package.

I myself am most interested in ZonedDateTime class, as it represents timestamp in a certain time zone. ZonedDateTime (like OffsetDateTime class) contains at its heart an object of Instant class, and time zone information.

Instant is "an instantaneous point on the time-line", in other words it's just a number of seconds (and nanoseconds) passed since "epoch" (1970-01-01T00:00:00Z). This value doesn't depend on time zone, it is the same no matter where in the world you are (java.time doesn't take relativity into account, which is fine with me).

What this means, basically, is that you can't "convert" ZonedDateTime to another time zone, but what you can do instead is create another ZonedDateTime object with the same Instant value, but with different time zone setting. You can't change your existing ZonedDateTime objects, since they are immutable (thread safety!).

Using ZonedDateTime.withZoneSameInstant does just that:
import java.time.ZoneId;
import java.time.ZonedDateTime;

. . .

ZonedDateTime zdtHereNow = ZonedDateTime.now();
System.out.println("ZonedTimeDate here and now = " + zdtHereNow);

ZoneId utcTzId = ZoneId.of("UTC");
ZonedDateTime zdtUtcNow = zdtHereNow.withZoneSameInstant(utcTzId);
System.out.println("ZonedTimeDate \"here and now\" in UTC = " + zdtUtcNow);

Redis + Java = Jedis

"Jedis is a blazingly small and sane Redis java client."

Jedis wiki is here - https://github.com/xetorthio/jedis/wiki

Here's a tutorial: http://www.tutorialspoint.com/redis/redis_java.htm

If you want to download Jedis jar (compiled, not the source zip they offer on their Github page), use this link - http://repo1.maven.org/maven2/redis/clients/jedis/
Go to the folder with the greatest version number (currently 2.8.1, but it'll change) and download jedis-.jar (jedis-2.8.1.jar at the moment).

Don't forget to add this jar to your CLASSPATH both for javac (when you compile your project) and for java (when you run it).

Another option, besides Jedis, is Lettuce - see here for short introductions for both Lettuce and Jedis.

Finally, here's a small example of my own:

package kotodemo.jedis_test;

import java.util.Set;
import redis.clients.jedis.Jedis;

class JedisTest1
{
 public static void main(String[] args)
 {
  // Connecting to Redis server on localhost
  Jedis jedis = new Jedis("localhost", 6100); // Note the non-standard port 6100 I'm using
  // Check whether server is running or not
  System.out.println("Server is running: " + jedis.ping());

  // Get the names of all the keys print them
  Set keys = jedis.keys("*");
  for(String keyStr : keys)
   System.out.println(keyStr);
 }
}

Update: to connect to a non-zero Redis database, use Redis URI to initialize Jedis object, like so:
import java.net.URI;
. . .
Jedis jedis = new Jedis(new URI("redis://localhost:6100/7"));
where 6100 is Redis port I used to test this particular code snippet and 7 is Redis DB I wanted to connect to.

More information on Redis URIs (URLs):