August 20, 2013

Mutable variable capture in anonymous Java classes

by Jay Jonas

The Java compiler requires local variables of enclosing contexts referenced in anonymous classes (so-called captured variables) to be final. What if the anonymous class wants to alter the value of the variable, i.e. requires the variable to be mutable? This post shows different ways how to achieve that.

Mutability with an array

The most common way to solve this problem is to put the variable into an array of length one, make the array final and capture the array:

<br /><%%KEEPWHITESPACE%%>   JButton button = new JButton("Press me!");<br /><%%KEEPWHITESPACE%%>   final String[] message = new String[]{"Never been pressed"};<br /><%%KEEPWHITESPACE%%>   button.addActionListener(new ActionListener() {<br /><br /><%%KEEPWHITESPACE%%>      @Override<br /><%%KEEPWHITESPACE%%>      public void actionPerformed(ActionEvent e) {<br /><%%KEEPWHITESPACE%%>         message[0] = "Pressed";<br /><%%KEEPWHITESPACE%%>      }<br /><br /><%%KEEPWHITESPACE%%>   });<br />

The value is stored in the first slot of the array. The reference to the array is final but its element stays mutable.

While this approach works fine, it looks strange and will certainly raise questions for developers in your team which have never seen this construct before.

Mutability with a holder

The better way to make the message mutable is to put it into a holder class

<br /><%%KEEPWHITESPACE%%>    class Holder {<br /><%%KEEPWHITESPACE%%>        private T value;<br /><br /><%%KEEPWHITESPACE%%>        Holder(T value) {<br /><%%KEEPWHITESPACE%%>            setValue(value);<br /><%%KEEPWHITESPACE%%>        }<br /><br /><%%KEEPWHITESPACE%%>        T getValue() {<br /><%%KEEPWHITESPACE%%>            return value;<br /><%%KEEPWHITESPACE%%>        }<br /><br /><%%KEEPWHITESPACE%%>        void setValue(T value) {<br /><%%KEEPWHITESPACE%%>            this.value = value;<br /><%%KEEPWHITESPACE%%>        }<br /><%%KEEPWHITESPACE%%>    }<br />

and pass the holder into the inner class:

<br /><%%KEEPWHITESPACE%%>   JButton button = new JButton("Press me!");<br /><%%KEEPWHITESPACE%%>   final Holder mutableMessage = new Holder("Never been pressed");<br /><%%KEEPWHITESPACE%%>   button.addActionListener(new ActionListener() {<br /><br /><%%KEEPWHITESPACE%%>      @Override<br /><%%KEEPWHITESPACE%%>      public void actionPerformed(ActionEvent e) {<br /><%%KEEPWHITESPACE%%>         mutableMessage.setValue("Pressed");<br /><%%KEEPWHITESPACE%%>      }<br /><br /><%%KEEPWHITESPACE%%>   });<br />

This solution states more clearly why the message has been wrapped. If the holder is implemented as a generic utility class, this solution is not more verbose than the one with the array. In case you don’t want to implement the Holder class yourself, you can also reuse the MutableObject from Apache Commons or the Holder from Google Guava. One could argue that the solution with the array is faster (creating an array is usually faster than instantiating a class), but in most cases the performance loss will be negligible.

// from at

August 5, 2013

Running javah tool from Eclipse

by Jay Jonas

The javah produces C header files and C source files from a Java class that are needed to implement native methods. The javah -jni is used to generate a C header file containing the function prototype for the native method implementation.

The javah can be configured as an external tool like this: click Run |External Tools |External Tools Configurations. This will invoke External Tools Configurations wizard.

Select Program Type and create a new launch configuration.

For Location, click on Browse File System and browse to the path of javah tool (typically <java_home>\bin).

For Working Directory, click on Browse Workspace and select upto project output directory (directory where class files reside – typically ${workspace_loc:/your.jni.project/bin})

Various options of javah command may be added here which is then followed by ${java_type_name} workspace variable which returns the fully qualified Java type name of the primary type in the selected resource.

The argument list instructs the tool to keep the generated header file in a directory named jni under the project folder. If -d option is not specified, the header files will be stored in bin folder itself.

"${project_loc}${system_property:file.separator}jni" ${java_type_name}
External Tools Configuration

External Tools Configuration

// That’s all, folks!

February 10, 2013

Access forbidden! New XAMPP security concept fix

by Jay Jonas

XAMPP is a great tool for developers running Windows. But if you are using virtual hosts and maybe you are stuck on  this error message:

Access forbidden! New XAMPP security concept

Open httpd-xampp.conf which is at  <xampp_path>/apache/conf/extra/

Comment this section:

 # New XAMPP security concept
 #<LocationMatch "^/(?i:(?:xampp|security|licenses|phpmyadmin|webalizer|server-status|server-info))">
 # Order deny,allow
 # Deny from all
 # Allow from ::1 \
 # fc00::/7 \
 # fe80::/10
 # ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var

And insert these sections as  following:

# New XAMPP security concept

 # Close XAMPP security section here
 <LocationMatch "^/(?i:(?:security))">
     Order deny,allow
     Deny from all
     Allow from ::1
     ErrorDocument 403 /error/HTTP_XAMPP_FORBIDDEN.html.var

# Close XAMPP sites here
 <LocationMatch "^/(?i:(?:xampp|licenses|phpmyadmin|webalizer|server-status|server-info))">
     Order deny,allow
     Deny from all
     Allow from ::1
     ErrorDocument 403 /error/HTTP_XAMPP_FORBIDDEN.html.var

# My VHosts security concept
# if you are using virtual hosts
# stored outside of xampp folders
<DirectoryMatch "D:/dev/wwwroots/(.*)/wwwroot/*">
    Order deny,allow
    Deny from all
    Allow from

    ErrorDocument 403 /error/HTTP_XAMPP_FORBIDDEN.html.var

Then now open http-vhosts.conf that is into the same location.

Add this section:

<VirtualHost *:80>
     DocumentRoot "D:/path/to/xampp/htdocs"

     ServerName localhost:80
     <Directory "D:/path/to/xampp/htdocs">
         IndexOptions +FancyIndexing NameWidth=*
         Options Includes FollowSymLinks Indexes ExecCGI
         AllowOverride All
         Require all granted

Finally a sample for setting a virtual host stored in a different folder:

<VirtualHost *:80>
     DocumentRoot D:/dev/wwwroots/

     ServerAlias *

     ErrorLog D:/dev/wwwroots/
     CustomLog D:/dev/wwwroots/ common

     <Directory "D:/dev/wwwroots/">
         IndexOptions +FancyIndexing NameWidth=*
         Options Includes FollowSymLinks Indexes ExecCGI
         AllowOverride All
         Require all granted
September 22, 2012

Eclipse Task Tags Meaning

by Jay Jonas

TODO, along with FIXME and XXX, is known in Eclipse as a task tag, and is indexed by the IDE to let you find the spots marked with those tags easily. You can edit such tags in the Eclipse Preferences -> Java -> Compiler -> Task Tags.

This is the list for default Eclipse Task Tags and their meaning:


Comments that mark something for later work, later revision or at least later reconsideration. ODO comments should be considered a very useful technique, although like all good things on Earth, there’s certainly potential for abuse.


A standard put in comments near a piece of code that is broken and needs work.


A marker that attention is needed. Commonly used in program comments to indicate areas that are kluged up or need to be. Some hackers like `XXX’ to the notional heavy-porn movie rating. Use it to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.

ZZZ   /* This tag is mine!  ; ) */

Sometimes I like to use this marker for a stuff that was commented as deprecated just to remind me what happened there. However, when I’m sure, I delete it along with the code.

// TODO Need to optimize this before the end of the world.
// FIXME This won't work if the programmer is missing. 
// XXX This method badly needs refactoring: should be decaffeinated.
// ZZZ This was useful before the Y2K Millennium Bug.

From Sun/Oracle's Java code conventions, section 10.5.4:

September 3, 2012

How to delete a print job that is stuck in the print queue

by Jay Jonas

Printing problems are some of the most frustrating problems that we experience as computer users.

Last week I had some problems with stuck network printer jobs in Windows 2003 Server. Forget to press Cancel button. Forget to turn off and on your printer. Forget the Control Panel. Go straight forward to the core of the problem, be radical.

Start Notepad and write down the following command:

@echo off
pause Press ENTER to continue or CTRL+C to Cancel
net stop spooler
del %systemroot%\system32\spool\printers\*.shd
del %systemroot%\system32\spool\printers\*.spl
net start spooler
pause The Command Script has finished. Press ENTER to exit.

Save this to a command script text file and named it as DeletePrintJobs.bat. Save it where you could easy find it. For example, save it in C:\.

Now that you have created the command script file, you will run it. To run it, you will write the name of the command script file in the Run box, including the path where you save it.

Click Start, and then click Run. In the Open box, write C:\DeletePrintJobs.bat and Click OK.

Notice that the Command Prompt window opens to run the command script file that you created. Notice also that this window will wait for a confirmation when starting and also when finishing. If you do not see the Command Prompt window open, check that you saved the command script file by using the correct name and that you entered the correct command script file name in the Run box.

Note 1 If this method does not work the first time, or if you cannot print anything after you use this method, restart your computer, double check for typo errors and then try again.

Note 2 To use this method, you must have Computer Administrator status

June 16, 2012

Youtube: An error occurred. Please try again later.

by Jay Jonas

Have you installed RealPlayer a couple of days?

Now, YouTube prompts their videos with the following error message:

An error occurred. Please try again later.

To fix this, just follow these simple steps:

  • Open RealPlayer.
  • From the upper left corner menu, choose Preferences option.
  • From the left hand panel, choose Download & Recording.
  • Uncheck “Enable Web Download & Recording for these installed browsers”.
  • Hit Ok to confirm and close RealPlayer.
  • Restart Mozilla Firefox.
June 1, 2012

15 client limit for Windows Server 2003 for Small Business Server

by Jay Jonas

That’s a horror history!
Technically, we have a 75 Client Access License (CAL) limit in a Microsoft Windows Small Business Server 2003 R2 (Part Number T75-01355). It comes with a 5 CALs in box.

So, we purchased additional CALs described as Microsoft Windows Server CAL 2008 sngl Open No Level User CAL.

We can perfectly add 2 packets of 5 CAL’s, but when we try to add the third packet of 5 CAL’s we get the following error:

“The specified license code cannot be added because it will exceed the
15 client limit for Windows Server 2003 for Small Business Server”

The client now has 15 licenses in total, but has still the remain CAL’s that
need to be added.

Note, I repeat,  “we have a Windows Small Business Server 2003 R2. Technically, it’s a 75 Client Access License (CAL)”. The error message is about a Windows Server 2003 for Small Business Server.

The Microsoft support says that all this problem is because we cannot dowgrade from Microsoft Windows Server CAL 2008 to a Microsoft Windows Server CAL 2003 for SBS.

Not a surprise when after buy Microsoft Windows Small Business Server Premium CAL Ste 2008 Single Open  20 No Level User CAL (packed of 20 CALs SKU 6VA-02189) and Microsoft Windows Small Business Server Premium CAL Ste 2008 Single Open  5 No Level User CAL (packed of 5 CALs SKU 6VA-01644) we got the same error message:

“The specified license code cannot be added because it will exceed the
15 client limit for Windows Server 2003 for Small Business Server”

We called Microsoft Support and they asked us if we have a support contract. “No, we don’t” was the answer. So, they said if we pay a support they will fix it!

If it were not tragic, it would be comic!

I will tell the rest of the story in the next post. Stay tune!

December 31, 2011

2011 in review

by Jay Jonas

The stats helper monkeys prepared a 2011 annual report for our blog.

Happy New Year and here’s an excerpt:

A San Francisco cable car holds 60 people. This blog was viewed about 3,100 times in 2011. If it were a cable car, it would take about 52 trips to carry that many people.

Click here to see the complete report.

December 19, 2011

Permanent workaround

by Jay Jonas

Anonymous guy wrote elsewhere:

So a few months ago while converting the PHX platform from 3 to 4 I found a comment:

// RM 6-22-2007 Temporary workaround

Ha ha, it’s still there. I will go revise it today to:

// RM 6-22-2007 Temporary workaround
// SB 1-13-2011 Permanent workaround

October 16, 2011 Address already in use: JVM_Bind

by Jay Jonas

Just have some fun with Java sockets API in a Windows enviroment? So, what I could I say, just happens! Here is a solution.

Open the Command Prompt and follows these steps:

  • Find out if tcp port is really in use:
C:> netstat -na
  • Find out which process is binded on that por, in case it’s in use:
C:> netstat -abnovp tcp
This command may be a litte slow to return their results
  • To kill the process, take the PID (Process ID) in the lastest column and run this command:
C:> taskkill /pid <process_id_goes_here> /f

Finally, you can download the TCPView, a freebie from Microsoft.

TCPView is a Windows program that will show you detailed listings of all TCP and UDP endpoints on your system, including the local and remote addresses and state of TCP connections.


A more little tip: fix your sofware!

Tags: ,