Tuesday, September 23, 2014

The installer is unable to instantiate the file KEY_XE.reg

I got this error when installing Oracle Database Express Edition 11g 32 bit on my Windows 7 64-bit machine.

---------------------------
Error
---------------------------
The installer is unable to instantiate the file C:\Users\<your user name>\AppData\Local\Temp\{60712028-B7B0-4EC3-9C28-663111EC954A}\KEY_XE.reg.  The file does not appear to exist.
---------------------------
OK
---------------------------

I was using Oracle Database Express Edition 11g Release 2 for Windows x32 because at the time there was no 64 bit version for Windows 7.

The answer I found was in a response to an Oracle forum post: XE11: KEY_XE.reg cannot be loaded on WIN7 prof 64b and I have integrated that process into the instructions below.

  1. Unzip the downloaded installer OracleXE112_Win32.zip somewhere.
    unzip /C/Users/<your user name>/Downloads/OracleXE112_Win32.zip -d /C/Users/<your user name>/Temp/OracleXe
    chmod -R 777 /C/Users/<your user name>/Temp/OracleXe
    
  2. Run the exe: C:\Users\<your user name>\Temp\OracleXe\DISK1\setup.exe.
  3. Click NEXT > click I accept.. > STOP!
  4. If you continued, you would see the error this post is about. Did you ignore these instructions and continue anyway? That's OK.
    1. Accept the error (click OK).
    2. Let the install finish.
    3. Un-install Oracle Express.
    4. Delete C:\oraclexe
    5. Start again.
  5. You should now be on the screen: Choose Destination location. DO NOT PRESS NEXT YET.
    1. Open Windows Explorer to C:\Users\<your user name>\AppData\Local\Temp and look for a folder that the install just created. It will have a name like this: {60712028-B7B0-4EC3-9C28-663111EC954A} and will be the same as what was reported in the error dialog.
    2. Inside that folder, find the file OracleMTSRecoveryService.reg and make a copy of it. Rename the copy to KEY_XE.reg.
    3. Now go back to the installer and press NEXT.
  6. Specify Database Passowords: someSecurePassword.
  7. Verify that the Current Installation settings are OK:
       Destination Folder: C:\oraclexe\
       Oracle Home: C:\oraclexe\app\oracle\product\11.2.0\server\
       Oracle Base:C:\oraclexe\
       Port for 'Oracle Database Listener': 1521
       Port for 'Oracle Services for Microsoft Transaction Server': 2030
       Port for 'Oracle HTTP Listener': 8080
       
  8. Install finished.

This worked for me on my previous install on a Windows 7 box.

This week I had to set myself up on a new Windows 7 64-bit install. I went through the steps I outlined above and the install seemed to work but I encountered another problem: Connected to an idle instance; trouble creating DB. The fix for this issue was to un-install the 32-bit Oracle Express and install Oracle Database Express Edition 11g Release 2 for Windows x64 - the 64-bit installer. I did not get the same error (The installer is unable to instantiate the file) when installing the 64-bit version.

Plugin execution not covered by lifecycle configuration

Eclipse is showing this error

Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-jar-plugin:2.4:jar (execution: make-jar, phase: compile)

It shows the error against the <execution> line in one of our POMs, an extract of which is below.

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <build>
  ...
    <plugins>
    ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <!-- Create shared ViewController JAR file which is used by MYPROJECT-API-V1 -->
          <execution>
            <id>make-jar</id>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
    ...

I only notice this error in Ecliipse Luna - it was not showing in Eclipse Kepler. Further, I can still build OK from the command line and even Eclipse still builds the project.

The fix was surprisingly easy. I select the quick fix: Discover new m2e connectors. Nothing came up, but when I clicked on FINISH, I found that Eclipse was indeed updating the m2e Connectors. I let it happen, re-started Eclipse, and the error disappeared.

Monday, September 15, 2014

Java process that can be stopped via sockets

While writing the code for Flip a coin and get the same results 100 times in a row, one thing that troubled me was finding a nice way to stop the process. The easiest (but ugliest) thing I could do was press control+c on the console to force-close (kill) the app. This is terrible because it doesn't allow me to clean up anything. I couldn't create a closing report on results. Worse, as I worked towards the next step - writing results to a database - I had no way to close the database. If you kill an app, it prevents finally clauses or try-with-resources blocks from doing their jobs.

One easy way to stop a long running process is to listen for keyboard input. The code below will do this.

public void run() {
   boolean keepGoing = true;
   try (Scanner keyboard = new Scanner(System.in)) {
      while (keepGoing) {
         System.out.println("Doing work in a loop. Enter STOP to stop.");
         if (keyboard.hasNextLine()) {
            final String next = keyboard.nextLine();
            System.out.println("You entered [" + next + "].");
            if (next.equals("STOP")) {
               keepGoing = false;
            }
         }
      }
   }
   System.out.println("Process stopped.");
}

The problem with this is that my process uses the console to output ongoing results. I write to a file as well, but I find it very useful to write to the console too (standard out) and I don't want to interrupt console reporting. It was really awkward listening for keyboard input from a console that I am busy writing to. It might say "Enter STOP" at the top of the console, but after a few hours, 100s of lines will be written out. So I needed another way.

The approach I decided upon was to use sockets. I based it on the Echo Client/Server from the Java Trail, Reading from and Writing to a Socket. It meant that I had to make my application multi-threaded. Here is quasi Sequence Diagram representing the interactions in this pattern.

My main class (RandomInARow - running in a thread) starts up the StopListener thread.

// Spin up different thread - socket to listen for STOP signal.
stopListener = new StopListener();
stopListener.start();

And the RandomInARow's run() method does the work, and with every iteration, it will first check if the stopListener thread has received the STOP signal. If it has, then the application should clean up and finish.

public void run() {
   // Now do our own work in this thread.
   int target = INCREMENT_BY;
   while (stopListener.isActive()) {
      repeatUntilWeGetCount(target);
      target += INCREMENT_BY;
   }
   System.out.println("See results in log file [" + logFile.toAbsolutePath()
         + "].\n");
}

StopListener is a thread that will open up a socket and listen to it. As soon as it reads anything, it sets the boolean active to false and exits.

public void run() {
   try (ServerSocket serverSocket = new ServerSocket(port);
         Socket clientSocket = serverSocket.accept();
         PrintWriter out =
               new PrintWriter(clientSocket.getOutputStream(), true);
         BufferedReader in =
               new BufferedReader(new InputStreamReader(
                     clientSocket.getInputStream()));) {
      String inputLine;
      while ((inputLine = in.readLine()) != null) {
         message.append(inputLine);
         out.println("Stop signal received.");
         active = false;
         break;
      }
   } catch (IOException e) {
      System.out.println("Exception caught when trying to listen on port ["
            + port + "] or listening for a connection");
      System.out.println(e.getMessage());
   }
}

It is doing a few other things of course.

  • Note the use of try-with-resources to open up several Autocloseable objects: a ServerSocket, Socket, a PrintWriter and a BufferedReader. From the Java Trail, The try-with-resources Statement: note that the close methods of resources are called in the opposite order of their creation.
  • ServerSocket and Socket are two objects that talk to each other across a network via a given port. A Socket is a client: it sends requests to a server and reads a response. A ServerSocket is a server: it listens for requests from a client and sends a response. Note that "across a network" can still mean a client and server sitting on the same machine - they will open a port and send messages via that port in the same way they would if they were on different machines. On a Windows machine, when running a program that attempts to send/receive data via sockets, your Firewall may show a pop-up asking if you give permission for the network communication to happen.
  • Although ServerSocket and Socket objects communicate to each other, they don't know how read/write strings, integers etc - that job goes to a PrintWriter (which does the writing) and a BufferedReader (which does the reading).
  • The while loop will wait for something to be read, acknowledge the signal, store a message and set a flag to outside code that processing should stop.
    1. The while loop will wait for something to be read (in.readLine()).
    2. When something is read, it is stored (with message.append(inputLine)).
    3. An acknowledgment is then written back to the client Socket (out.println("Stop signal received.")).
    4. The boolean called active is set to false.
    5. The loop is stopped via break.

StopSender is a thread that will open up a socket on the same port that StopListener listens to. It sends a message via that port - either a default message or one supplied by the user. It reads a response which it outputs to console, and then finishes.

public void sendStopSignal(final String message) {
   try (Socket socket = new Socket(HOST, StopListener.DEFAULT_PORT);
         PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
         BufferedReader in =
               new BufferedReader(new InputStreamReader(
                     socket.getInputStream()));) {
      out.println(message);
      final String received = in.readLine();
      System.out.println(received);
   } catch (UnknownHostException e) {
      System.err.println("Don't know about host [" + HOST + "].");
      System.exit(1);
   } catch (IOException e) {
      System.err.println("Couldn't get I/O for the connection to [" + HOST
            + "].");
      System.exit(1);
   }
}

Note that HOST is defined to be localhost. This means that StopSender must be run on the same machine that StopListener is being run on! I have defined this in a variable to make it just a bit easier if I want to expand this to run across machines in the future.

public static final String HOST = "localhost";

Would you like to try out this code? You can download the jar containing all the class and java files at https://app.box.com/s/x6rxzaw7v8jpt52lrkej. It was compiled under JDK 7. Save the jar file somewhere, and in the console, run it with the command below. The program will output to the console and to a text file.

java -jar randomInARow.jar

You can control where the text file is written to by supplying a path argument as below (the path/folder must already exist).

java -jar randomInARow.jar "dir/to/write/log/to"

The program will run forever, but you can stop it by running StopSender in another console.

java -cp randomInARow.jar com.rmb.randomnodatabase.taskmanagement.StopSender

This uses the default STOP message. You can supply your own message by adding an argument to the commaned, as below.

java -cp randomInARow.jar com.rmb.randomnodatabase.taskmanagement.StopSender "Because I want this to STOP NOW!"

Here is an extract from the log created when I used the above command to stop the application.

New count [ 24] at [14 Sep 2014, 11:36:16.946 PM] after 1 seconds and 538 milliseconds.
New count [ 22] at [14 Sep 2014, 11:36:17.371 PM] after 1 seconds and 963 milliseconds.
New count [ 26] at [14 Sep 2014, 11:36:17.459 PM] after 2 seconds and 51 milliseconds.
Finished at [14 Sep 2014, 11:36:18.454 PM] with target [30]
-----
User cancelled operation. It took 3 seconds and 46 milliseconds to get [26] results in a row - with target [30].
Reason for stopping: Because I want this to STOP NOW
-----
How often we flipped the same result [   1] times in a row: 13737650.
How often we flipped the same result [   2] times in a row: 6866089.
How often we flipped the same result [   3] times in a row: 3430324.

If you just want to see the code without downloading the jar, you can see the three classes I talk about there in separate pastebin pages.

Monday, September 08, 2014

Flip a coin and get the same results 100 times in a row

This morning, I heard someone say on a podcast that it was possible to flip a coin and get the same result 1000 times - it would just take a while. Then I thought, "if I could flip a coin as fast as random.nextInt(2), how long would it take?"

The full code I wrote in a pastebin. See the results of one continous run, in another pastebin.

The core code is very simple. Keep flipping a coin (random.nextInt(2)) for as long as you get the same result over and over - returning the number of times you got the same result.

private int countContiguousOccurences(final Random random) {
   int count = 1;
   final int first = random.nextInt(2);
   while (random.nextInt(2) == first) {
      count++;
   }
   return count;
}

Next, record how many times you flipped and got the same result in a row. Store this in a map of (key = int, value = long). The key int is how many times you got the same result in a row. The value long is how many times you have flipped the coin and got the same result this many times in a row. So for example, if we flipped and got heads five times in a row, and this was the tenth time we did it, we would be storing (key = 5, value = 10).

private void rememberCountOfContiguousOccurences(final Random random,
      final Map<Integer, Long> counts) {
   Integer count = countContiguousOccurences(random);
   if (counts.containsKey(count)) {
      Long countOccurences = counts.get(count);
      countOccurences++;
      counts.put(count, countOccurences);
   } else {
      counts.put(count, 1l);
      Date currentTime = new Date();
      message(String.format("New count [%3d] at [" //
         + FORMAT_TIMESTAMP.get().format(currentTime)
         + "] after %s.\n", count, reportTime(currentTime.getTime()
         - start)));
   }
}

I control the code with a thread in such a way that we try for ten of the same results in a row, then twenty, thirty, forty etc, incrementing the target by ten each time. Whenever the target is hit, output the results like this:

It took 168 milliseconds to get [20] results in a row.
How often we flipped the same result [   1] times in a row: 154382.
How often we flipped the same result [   2] times in a row: 77054.
How often we flipped the same result [   3] times in a row: 38763.
How often we flipped the same result [   4] times in a row: 19426.
How often we flipped the same result [   5] times in a row: 9608.
How often we flipped the same result [   6] times in a row: 4883.
How often we flipped the same result [   7] times in a row: 2359.
How often we flipped the same result [   8] times in a row: 1202.
How often we flipped the same result [   9] times in a row: 587.
How often we flipped the same result [  10] times in a row: 316.
How often we flipped the same result [  11] times in a row: 150.
How often we flipped the same result [  12] times in a row: 70.
How often we flipped the same result [  13] times in a row: 41.
How often we flipped the same result [  14] times in a row: 22.
How often we flipped the same result [  15] times in a row: 7.
How often we flipped the same result [  16] times in a row: 9.
How often we flipped the same result [  17] times in a row: 1.
How often we flipped the same result [  18] times in a row: 1.
How often we flipped the same result [  20] times in a row: 1.

Note that I report at the end of each run how many times we achieved each count in a row. So in this run we got the same result twice in a row 77054 times, and we got the same result eighteen times in a row just once. In this run, we didn't hit nineteen times in a row at all.

Each time I get a new count, it is reported as it happened. In this way, I can see that I don't always new results in order. See below for a sample of the latest run.

Initial results show that getting 30 of the same result in a row is easy - it happens within a couple of minutes.. but getting to 40 is a jump to hours, and 50: days.

New count [ 30] at [10 Sep 2014, 10:02:55.254 PM] after 22 seconds and 711 milliseconds.
New count [ 29] at [10 Sep 2014, 10:04:20.712 PM] after 1 minutes, 48 seconds and 169 milliseconds.
New count [ 32] at [10 Sep 2014, 10:06:57.301 PM] after 4 minutes, 24 seconds and 758 milliseconds.
New count [ 33] at [10 Sep 2014, 10:08:40.588 PM] after 6 minutes, 8 seconds and 45 milliseconds.
New count [ 31] at [10 Sep 2014, 10:09:11.408 PM] after 6 minutes, 38 seconds and 865 milliseconds.
New count [ 34] at [10 Sep 2014, 10:12:58.718 PM] after 10 minutes, 26 seconds and 175 milliseconds.
New count [ 35] at [10 Sep 2014, 10:18:24.287 PM] after 15 minutes, 51 seconds and 744 milliseconds.
New count [ 37] at [10 Sep 2014, 10:47:36.084 PM] after 45 minutes, 3 seconds and 541 milliseconds.
New count [ 36] at [10 Sep 2014, 11:20:35.756 PM] after 1 hours, 18 minutes, 3 seconds and 213 milliseconds.
New count [ 38] at [11 Sep 2014, 01:30:12.367 AM] after 3 hours, 27 minutes, 39 seconds and 824 milliseconds.
New count [ 40] at [11 Sep 2014, 03:36:59.302 AM] after 5 hours, 34 minutes, 26 seconds and 759 milliseconds.
New count [ 39] at [11 Sep 2014, 04:18:59.431 AM] after 6 hours, 16 minutes, 26 seconds and 888 milliseconds.
New count [ 41] at [12 Sep 2014, 07:38:47.966 PM] after 1 days, 21 hours, 36 minutes, 15 seconds and 423 milliseconds.
New count [ 45] at [13 Sep 2014, 01:09:16.112 AM] after 2 days, 3 hours, 6 minutes, 43 seconds and 569 milliseconds.
New count [ 43] at [14 Sep 2014, 12:25:38.984 PM] after 3 days, 14 hours, 23 minutes, 6 seconds and 441 milliseconds.

Thursday, May 29, 2014

Regex to replace upper case with lower case in UltraEdit

Regex in UltraEdit to replace someStringWithCamelCase with "some string with camel case".

This is using regex to find upper case characters and replace them with lower case by finding ([A-Z]) and replacing with \L\1\E.

Thanks to this StackOverflow post: Convert a char to upper case using regular expressions (EditPad Pro).

Saturday, March 22, 2014

Listary, Directory Opus and AutoHotkey - a match made in Geek Heaven

Listary is a very good tool for finding files fast. It indexes all drives on your machine and integrates into Windows Explorer or Directory Opus (my explorer of choice) so that when you start typing any file name, it will straight away show you all matches, highlighting the ones in your current directory.

You can also set a hotkey to summon a Listary toolbar if you are not in a file explorer to do the same thing - search for files. What is so cool about this is that once you find the file you want, press the right arrow key and you have the context menu displayed, so you can choose to open the file, do an SVN update or whatever - without having to go to your explorer program. There are other options added too - like copying the path of the file/directory to clipboard.

The free and pro versions are the same download - and you can access many of the pro features while using it for free (with a not very intrusive nag dialog). However, the pro version is well and truly worth it: $20 for lifetime upgrades.

As I mentioned, Directory Opus is my file explorer tool of choice, and I will often use the Listary toolbar to find and go to a directory when Directory Opus is open but not in focus (i.e. not the top window). So I might be writing in UltraEdit and want to look at files in D:\Temp, so I summon the Listary toolbar, type Temp and press enter on the correct entry. By default, Listary will open that directory in Directory Opus, but if Directory Opus was opened but not in focus, it won't manage to bring Directory Opus to the front (because in Directory Opus that is two commands - one to open a directory and one to bring Directory Opus to top).

So, a small fix is needed in this situation - Autohotkey script containing those two commands. Create an Autohotkey script:

Run c:\Program Files\GPSoftware\Directory Opus\dopusrt.exe /cmd Go "%1%"
Run c:\Program Files\GPSoftware\Directory Opus\dopusrt.exe /cmd Go LASTACTIVELISTER

Then open Listary Options > General tab and under Default File Manager, set Path to be the path to your AHK script (D:\Documents\apps\AHK\openDirectoryInDirectoryOpus.ahk in my case) and Parameter to %1. See the below screenshot.

Also, turn on Fuzzy Matching - that allows you type out non-contiguous parts of the file/directory name.

Monday, January 20, 2014

Testing synchronous vs asynchronous Dojo 1.9

I have this in a.js:
require(["dojo/_base/xhr"], function(){
  console.log("a");
});

I have this in b.js:
define(["dojo/_base/xhr"], function(){
  console.log("b");
  return {};
});

And I run this (in index.html):
console.log("TESTING Part One");
require([ "a" ], function() {
console.log("c");
});
console.log("TESTING Part Two");
require([ "b" ], function() {
console.log("c");
});

Output 1 - Asynchronous: here is my output with data-dojo-config="async: true"
TESTING Part One
TESTING Part Two
a
c
c
b

Output 2 - Synchronous: here is my output with data-dojo-config=""
TESTING Part One
a
c
TESTING Part Two
b
c

  1. With output 1, why are the "TESTING Part .." strings being output first?
    1. Is it because the two requires in index.html are creating anonymous blocks of code that are being asynchronously?
    2. Is this sneaky way of spinning off threads?
  2. With output 1, why is "b" even being output at all?
    1. From here: http://livedocs.dojotoolkit.org/loader/amd#the-amd-api: "Module creation is lazy and asynchronous, and does not occur immediately when define is called. This means that factory is not executed, and any dependencies of the module will not be resolved, until some running code actually requires the module."
    2. Nothing is actually calling on "b", so why is it executed at all?
  3. With output 2, does this mean that synchronous config turns off this AMD feature: "Module creation is lazy and asynchronous"?

Friday, October 11, 2013

CSS Custom Cursor that works in IE and Chrome

Frustration with CSS cursor.

This works in Chrome, Firefox and IE (8) (although I can't seem to get this snippet working in IE 8 on my blog)

<P style="cursor: url(/path/to/grab.png), url(/path/to/grab.cur),auto;">
   Text.
</P>

Problems I faced getting this to work.

  1. PNG file works fine on Chrome, not in IE.
  2. CUR file works find in IE, but not in Chrome.
  3. Had to download a program to make CUR files. RealWorld Cursor Editor worked.
  4. But Windows always re-sizes cursor files to 32 pixels x 32 pixels. So I had to resize the image in cur format to fit within the top left quadrant of a 32 pixels x 32 pixels image.
  5. Have to put reference to both file formats in the CSS property, and it wouldn't work in Chroms until I put the auto at the end. cursor: url(/path/to/grab.png), url(/path/to/grab.cur),auto;.

Page that helped me with this:

  1. W3Schools demo page for cursors.