VirtualBox does not send Control key events to guest OS.

January 23rd, 2009
Now that's a strange one.

This is the case: you run Windows XP as a guest operating system in VirtualBox in your Ubuntu box. However, VirtualBox refuses to send events for the Control key (for example, if you press Ctrl-N Windows XP will just receive 'N').

It is possible that the problem is caused by a feature of Ubuntu rather than a bug in VirtualBox. Specifically you have to make sure that you have unchecked the Show position of pointer when the Control key is pressed.

Go to System → Preferences → Mouse → General. Make it look like this:

locate-pointer.jpg



Showing the position of the pointer is neat feature but it seems that you have to live without it if you want Control key events to be passed in your VirtualBox guests...

Creating Symbolic Links with Java at Runtime

January 4th, 2009
For the time being it seems that there is no other way than calling an external program for doing the job:

Process process = Runtime.getRuntime().exec( new String[] { "ln", "-s", oldName, newname } );
process.waitFor();
process.destroy();


The disadvantage of this method is the overhead for creating the new process. This can be a serious downgrade in performance if you have to execute this code frequently.

Here is an alternative solution that can vastly improve speed.

First you write a small program in C, say it symlinkcreator.c. This is the actual worker:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int main()
{
char st[1024];
char oldpath[1024];
int count = 0;

while (fgets(st,sizeof(st),stdin) != NULL)
{
for (char *cp = st; *cp; cp++)
if (*cp == '\n' || *cp == '\r') *cp = '\0';

if (count++ % 2)
symlink(oldpath,st);
else
strcpy(oldpath,st);
}
}


You can compile the program by running this command:

# gcc symlinkcreator.c -o symlinkcreator -std=c99

This will give you the symlinkcreator executable file.

Then you going to need an elegant wrapper class that will call the C program:

import java.io.IOException;
import java.io.PrintWriter;

public class SymlinkCreator
{
private Process process;
private PrintWriter out;

public SymlinkCreator( String path ) throws IOException
{
process = Runtime.getRuntime().exec(path);
out = new PrintWriter(process.getOutputStream());
}

public void link( String oldpath, String newpath )
{
out.println(oldpath);
out.println(newpath);
out.flush();
}

public void terminate() throws InterruptedException
{
out.close();
process.waitFor();
process.destroy();
}
}


Have done these, you can then easily create symbolic links from you code, like this:

// You only need one single SymlinkCreator object (just a single unix process is created)
SymlinkCreator symlinkCreator = new SymlinkCreator("/path/to/symlinkcreator");
...
// Create as many symbolic links as you want using the same process
symlinkCreator.link(oldName,newName);
...
// Terminate the symbolic creator process and release resources when done.
symlinkCreator.terminate();


Downloads:
symlinkcreator.c

SymlinkCreator.java




Alternatively, you can use native methods to eliminate the hassle of inter-process communication between two separate processes.

First create a Symlink class that defines the signatures of the native mathod. This class will act as an interface to the actual C functions:

public class Symlink
{
public native static void create( String oldpath, String newpath );
}


Then you will need to generate a header file for this class using the javah utility:

# javah -classpath /path/to/your/classes Symlink

This will generate a header file that looks like this:

Symlink.h

#include <jni.h>
/* Header for class Symlink */

#ifndef _Included_Symlink
#define _Included_Symlink
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     Symlink
* Method:    create
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_Symlink_create
(JNIEnv *, jclass, jstring, jstring);

#ifdef __cplusplus
}
#endif
#endif


Then you will have to write a small C file that will define the implemtation of the  function:

Symlink.c

#include <unistd.h>

#include <jni.h>

#include "Symlink.h"

JNIEXPORT void JNICALL Java_Symlink_create
(JNIEnv *env, jclass obj, jstring oldpath, jstring newpath)
{
const char *nativeOldpath = (*env)→GetStringUTFChars(env, oldpath, 0);
const char *nativeNewpath = (*env)→GetStringUTFChars(env, newpath, 0);

symlink(nativeOldpath,nativeNewpath);

(*env)→ReleaseStringUTFChars(env, oldpath, nativeOldpath);
(*env)→ReleaseStringUTFChars(env, newpath, nativeNewpath);
}


Then you have to compile the C file as a shared library:

# gcc -shared -o Symlink.so Symlink.c -I /path/to/jdk/include -I /path/to/jdk/include/linux

You will have to load this shared library at the runtime of your java program.

Finally you can write a small test program to see that everything works:

public class TestSymlink
{
public static void main( String[] args )
{
System.load("/path/to/Symlink.so");
Symlink.create("/home/giannis/test1", "/home/giannis/test2");
}
}


zenity windows and dialogs "hidden"

January 3rd, 2009
I just discovered that zenity dialogs show up underneath all the other open windows. It was a matter of five seconds to google it and find out that this is a bug of zenity that causes this symptom if zenity is used in combination with Compiz or Metacity.

You can easily fix this problem by applying a small patch. Just run these two commands:

# wget http://launchpadlibrarian.net/18653141/zenity-2.24.0-focus.patch
# sudo patch -p0 /usr/share/zenity/zenity.glade < zenity-2.24.0-focus.patch


The original information was found in launchpad.

how to make bash display username, hostname and current working directory

December 23rd, 2008
Just add the following line:

PS1="\u@\h:\w$ "

in your .profile or .bashrc.

UNIX: how to find files that DO NOT match a pattern or other criteria

December 20th, 2008
Most people with some basic command line skills can readily issue the unix find command to find files of a specific type or matching a specific pattern.

You can do the exact opposite, that is, find files that do not fulfil a specific criterion. The "secret" is to precede an exclamation mark (!) in the argument list, just before the criterion that you want to negate.

For example:

# find . -type f ! -iname "*.mp3"

The above command will find all files that have not an .mp3 extension.

How to create Shortcuts in Windows from the command line

December 3rd, 2008
It seems that there is not a direct way to create a shortcut from the command line in Windows. The solution presented here takes less than two minutes to set up and works pretty good. However, if you find a better one, I would like to know.

First you have to create a small text file by the name mkshortcut.vbs. Use your favorite text editor to edit the file, even notepad will do. Then copy the following text and paste it into the file:

set WshShell = WScript.CreateObject("WScript.Shell" )
set oShellLink = WshShell.CreateShortcut(Wscript.Arguments.Named("shortcut") & ".lnk")
oShellLink.TargetPath = Wscript.Arguments.Named("target")
oShellLink.WindowStyle = 1
oShellLink.Save


Then save the file and exit the editor. Make sure that you move the file in a directory in your PATH (usually C:\WINDOWS\System32 is fine). Now, from the command line you can create shortcuts this way:

mkshortcut /target:TargetName /shortcut:ShortcutName

You will have to replace TargetName with the name of the target file and ShortcutName with the name of the shortcut to be created (do not include a .lnk extension!). For example:

C:\>mkshortcut /target:"c:/documents and settings/giannis/desktop" /shortcut:"My Desktop"

C:\>dir *.lnk
Volume in drive C has no label.
Volume Serial Number is 70FC-EBB4

Directory of C:\

12/03/2008  11:12 AM               453 My Desktop.lnk
1 File(s)            453 bytes
0 Dir(s)  46,659,989,504 bytes free

C:\>


Make sure that in target you include the full path of the target file name, starting with the drive letter and going down. For some reason Windows seem unable to create shortcuts with a relative path. Always use absolute paths for target.

search files that contain a specific string

November 9th, 2008
Many times you find yourself at the top of complex directory tree, seeking for a file that contains some specific string value. Nautilus is not very helpful now, as it can only search on file names, not content. It seems that the command line is the only way.

Of course, you can always visit every single subdirectory in the tree and grep all files each time. But this is very inefficient and time consuming as the number of subdirectories grow larger, not to mention the probability of skipping some subdirectories by mistake.

"There must be a better way", you may think. And you are right. This is the command that will do the job for you:

find /path/to/top/level/directory -type f -exec grep -F -H [needle] {} \;

This command will output all the files under /path/to/top/level/directory that contain the stringl [neede]. The ; at the end is necessary to denote the end of the command. Make sure that you use appropriate escaping (\) according to your shell's fads.

If you are a Windows user and you cannot search within your files' contents (that's ok, it's not your fault, it's just that bloody windows explorer that never functioned...), you can also use the solution described here, at the cost of installing GNU utilities for Win32.

Windows refuse to eject volume for no obvious reason

September 18th, 2008
I believe everyone who uses Windows on a daily basis faces that problem quite often.

The case is: you are done with your removable hard drive or flash memory and then you are trying to Safely Remove it. However, Windows may tell you that "The device cannot be stopped right now. Try stopping the device again later." even though you are absolutely sure that you have closed all programs that may be using the removable volume.

Most of the times, this is caused by the Windows Explorer, or explorer.exe (not to be confused with the browser Internet Explorer). You have to kill the Explorer's running process and the start a new instance.

Open the task manager (Ctrl+Shift+Escape). Make sure you select the Processes tab. Sort the processs by name (by clicking on the Image Name header) and then look for a process with the name explorer.exe:

task-manager-1.JPG



Once you find it, click End Process. You may notice that your windows list and all the icons on the bottom of your screen will disappear, as well as the icons on your desktop, but don't worry they will soon come back as they were.

Then, select the Applications tab. There you have to click on the New Task... button:

task-manager-2.JPG



The Create New Task dialog will show up:

task-manager-3.JPG



Type "explorer" in the text box and press Enter. Your windows list and icons should show up again. Now, try again to stop the device. Hopefully there will be no process blocking you any more.

Aftetris "Micro Edition" Free Java Game

September 7th, 2008
aftetris-screenshot.pngAftetris "Micro Edition" is the Java ME version of Aftetris for mobile devices. This is a rather simplistic variant of Tetris, but it delivers a considerable dose of eye-candy, and it it highly customizable to the needs of the user. The game also takes advantage of the device's touchscreen, if there is one, and thereby can offer an enthralling playing experience.









Current version: 1.0.2

What's new in version 1.0.2:

  • Added "fast down", a useful feature for the slow levels.

  • Only the last row of new blocks inside the well. You really want this in these hard moments when the game is played in the top row of the well...




Here you can watch a small video demonstrating aftetris recorded directly from the emulator. My apologies for the watermark:



How to turn off autoplay in Windows XP

August 31st, 2008
Autoplay (or autorun) of Windows XP can be a very dangerous feature since it may execute (without even asking you) possible harmful software from a CD, flash memory stick, or other mass storage media. It can be an excellent way of spreading a virus or other malware. Here is what you have to do to disable autoplay in Windows XP:

Go to Start → Run... Type gpedit.msc and press Enter:

run-gpedit-msc.jpg



The Group Policy console window will show up. Go to Computer Configuration → Administrative Templates → System. Make sure you select the Standard tab on the bottom of the window. In the right panel, scroll down till you can see Turn off Autoplay. It is Not configured by default, like this:

group-policy-console.jpg



Double-click on that item. The Turn off Autoplay Properties dialog will show up.It should look like this:

turn-off-autoplay-disabled.jpg



Make sure you select the Enabled radio button, and that you want to Turn off Autoplay on: All drives. Like this:

turn-off-autoplay-enabled.jpg



Then click OK and you are done.