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.

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.

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">
          <!-- Create shared ViewController JAR file which is used by MYPROJECT-API-V1 -->

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();

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()) {
      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) {
         out.println("Stop signal received.");
         active = false;
   } catch (IOException e) {
      System.out.println("Exception caught when trying to listen on port ["
            + port + "] or listening for a connection");

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()));) {
      final String received = in.readLine();
   } catch (UnknownHostException e) {
      System.err.println("Don't know about host [" + HOST + "].");
   } catch (IOException e) {
      System.err.println("Couldn't get I/O for the connection to [" + HOST
            + "].");

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) {
   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);
      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.