Lubos Programming Blog

Lubos Blog about Programming (mainly Java) and Computers

Archive for the ‘java’ Category

HTML5 Programming

Posted by lubosp on March 8, 2012

Advertisements

Posted in java | Leave a Comment »

Generating Java Properties String retrieval class

Posted by lubosp on March 4, 2012

Resource file frequency – Source File

Retrieval Class Package Bundle name Properties file
no package, no retrieval class <source file name> + "ResourceStringManager" source file package from properties file <source file name> <source file name>.properties
package, no retrieval class <source file name> + "ResourceStringManager" package from properties file <source file name> <source file name>.properties
no package, retrieval class retrieval class source file package from properties file <source file name> <source file name>.properties
package, retrieval class retrieval class package from properties file <source file name> <source file name>.properties
Notes
  • Generated Retrieval class use bundle with the name of the source file when generate. If the same retrieval class is used for multiple source classes (row 3 and 4), the retrieval class has to be manually extended to use multiple bundles. The retrieval method has to pass the source file name and the method has to extract the bundle name from it. See how to create multiple resource files bundle below.
  • If no retrieval class is specified (row 1 and 2) the retrieval class is generated for each source file/properties file.

Resource file frequency – Project

Retrieval Class Package Bundle name Properties file
no package, no retrieval class capitalized scan name (spaces removed) + "ResourceStringManager" source file package from properties file specified properties file
package, no retrieval class capitalized scan name (spaces removed) + "ResourceStringManager" package from properties file specified properties file
no package, retrieval class retrieval class source file package from properties file specified properties file
package, retrieval class retrieval class package from properties file specified properties file
Notes
  • If no package is specified (row 1 and 3) the source file package is used, which means that Retrieval class will be created in each package.

Notes

How to create Resource file per package

Use Frequency project, externalize one package at the time and after each package modify package and retrieval class.

If "Do not use Retrieval Class" selected (default)

Uses only String retrieval method (getString(key)). This will always generate Java error if the source class definition does not contain getString(key).

How to create multiple resource files bundle

ResourceBundle.Control() controls the list of files for the ResourceBundle. You can overwrite getCandidateLocales and toBundleName. toBundleName converts locale to the "file name" and the list of locales you can control in getCandidateLocales. For example like (It works only in Java 1.6+):

 final String[] variants = new String[]{"your names"};
ResourceBundle.getBundle(baseName, locale,
            new ResourceBundle.Control() {
                public List<Locale> getCandidateLocales(String baseName, Locale locale) {

                        List<Locale> out = new ArrayList<Locale>();
                        String language = locale.getLanguage();
                        String country = locale.getCountry();

                        for (String variant : variants) {
                            out.add(new Locale(language, country, variant));
                        }
                        out.addAll(super.getCandidateLocales(baseName, locale));
                        return out;
                }

                public String toBundleName(String baseName, Locale locale) {
                        Locale l = new Locale(locale.getLanguage(), locale.getCountry());
                        return locale.getVariant() + "." + super.toBundleName(baseName, l);
                }
            });

Links

Posted in java | Leave a Comment »

Using IzPack Installer

Posted by lubosp on February 8, 2012

IzPack installer

IzPack is the best open source Java installer. Documentation is available here.

Tutorials

Debugging

Following links describe how to debug IzPack installer:

Posted in java | Leave a Comment »

Introduction to Quartz Scheduled Jobs

Posted by lubosp on February 5, 2010

 

Excellent introduction to Quartz Scheduled Jobs. Quartz is Java framework for sophisticated jobs scheduling.

Posted in java | Tagged: , | Leave a Comment »

Developing Blackberry application

Posted by lubosp on January 7, 2010

This blog entry has been moved as a page to my Mobile Development blog.

Posted in blackberry, java | Tagged: , | Leave a Comment »

Linux, Ubuntu is still not good enough to replace Windows or Mac OSX

Posted by lubosp on December 2, 2009

I had two old desktops (2GHz single core) and I decided to gut both and create one desktop that would run new Ubuntu 9.10.

I re-build the computer hardware after some experimenting with hard drive and DVD drive jumpers and made it bootable.

I then installed Ubuntu 9.10, install itself was a pleasant experiance comparable to Windows 7 install experience. Everything went smoothly.

After reboot I started exploring Ubuntu OS (I am running Ubuntu 9.10 in VMWare Player on my laptop). To my surprise Ubuntu did not recognize by name any other computers on my intranet (mix of Windows Vista and Windows 7 machines).  I can ping the other machines by IP but not by name. Network explorer did see the other machines by name and could access their shared hard drives, so Samba was working OK, but networking name resolution wasn’t working (kind of big deal when you are using DHCP).

I spent few hours poking around and googling but was unable to resolve the issue. I was also not able to add my networked printer, I did not find any way by using UI to add a printer (must be well hidden). To give Ubuntu some credit, my NAS MyWorldBook was recognized by Network explorer/Samba, that was not the case in Ubuntu 8.x and 9.04.

After about a day of playing with and tweaking Ubuntu, I cut my loses and installed Windows XP pro. The install was without any problems, networking works as expected, and I can add my network printer.

Conclusion

Installing and running Ubuntu was major disappointment. While I am sure with enough googling and editing some config files, I would be able to make Ubuntu (networking, printer) work properly, I cannot imagine that person without computer and system administration experience can install and use Ubuntu. I am part time system administrator for small business, and I wouldn’t introduce Ubuntu based machines into the network after my last experience. It looks like Windows are save for now and there is not real thread from Ubuntu to displays Windows any time soon. I am disappointed also because I was and still am big fan of Linux, and I thought Ubuntu is slowly improving to the level, where it is going to be able to compete with Windows and Mac OSX.

I think that main reason of Linux still has problems to catch up with Windows and Mac OSX is the huge fragmentation of the Linux world (so many distributions, so much work duplicated), it remind me Unix situation 15-20 years ago.

Posted in java | Leave a Comment »

Upgrading to Windows 7

Posted by lubosp on November 15, 2009

I am part time system administrator for small health-care provider, so I know that Windows 7 are in my future with purchases of the new computers (it is harder and harder to get new computer with Windows XP).

I already tried to install Windows 7 on my 6 year old HP laptop, and the experience wasn’t quite positive, see my blog here. But most of the technical publications rates Windows 7 quite well, so I decided to upgrade my main work/development laptop, Thinkpad R61 to Windows 7 Pro. At the same time I purchased Asus Eee PC netbook with Windows 7 Starter Edition.

Installation

My Thinkpad R61 had Windows Vista Home edition (32 bit), so the OS did not use all of the 4Gb memory in the laptop. Because I couldn’t do Windows 7 update (update doesn’t work going from Vista Home to Windows 7 Pro), I decided to bite the bullet and do the clean install of Windows 7 Pro 64bit to make use of full 4Gb of memory. I performed full backup and ran Easy Transfer Wizard (to Windows 7). Then ran Windows 7 Upgrade Advisor and uninstalled and/or upgraded a few applications advisor recommended to uninstall or upgrade (mainly ThinkVantage, Thinkpad applications, see here).

Installation went smoothly and it took about an hour. I then ran Windows update a few times to get the latest and greatest. Everything was running well NVidia drivers and other drivers (camera, audio) loaded well, networking was working, I could easily connect to my MyWorldBook NAS, and to my HP network printer (this might be the first Windows OS where the networking is working without glitches ;-). I then run Windows Easy Transfer to restore my settings.

As always, while some applications are running out of the box like Firefox, Eclipse, Notepad++ and others, some Windows applications have nasty habit to depend on registry entries, .dll libraries in Windows\system32 etc, and they had to be reinstalled. This is one of the major pains of upgrading or re-installing Windows OS. IMHO Microsoft should offer better application model to application developers so they can build applications that are easy to migrate between Windows OS installation, and application developers should do better job writing applications that do not depend on resources that disappear during Windows OS install/re-install.
BTW, I am using 2 disk partitions, C: and D: drive, and I do install most of the applications on D: drive, which makes it easier to install/re-install Windows OS. Even with that, it took me about a day to get my laptop in shape.

Windows 7 install creates Windows.old folder with core of the old (in my case Windows Vista) OS, so you can use it to recover applications and data missed by transfer wizard. Biggest installation disappointment was that Start Menu\Programs disappeared and they were also missing in Windows.old folders, so I had to rebuild them from scratch. That in itself is not trivial because I couldn’t find a way to access Start Menu folder (Permission Denied) with Windows Explorer even with administrator privileges. Fortunately I found this article, and downloaded and used SurF Explorer.

I do not use Programs menu that much, I instead maintain and share between my computers custom toolbars (folders in C:\&D (for development), C:\&U for utilities etc) which show up as D >> toolbar and that popup applications (folder content) when clicked on >>  in the toolbar. I use these toolbars for easy access to applications. These custom toolbars saved me from some more work chasing down all the applications and creating their links.

Windows XP mode is not installed with Windows 7 by default, here is the Microsoft documentation how to install Windows XP mode.

Few Problems

  • Desktop icons suddenly disappeared after reboot, it took me few minutes to find Desktop popup menu View/Show desktop icons that got unchecked for some reason
  • Power management is biggest problem ad disappointment so far. At night I just close the lid of my laptop and then come in the morning and open it. In two of the three days using Windows 7 after opening the lid in the morning the display was dark, there was no laptop activity (even if OS was not shut down), and I couldn’t wake up the laptop no matter what I did. I have to do hard shutdown holding I/O button and restart Windows 7. This worked seamlessly in Windows Vista. I am trying to set all Power options to Never, but so far laptop wake up function is failing!
  • Creating file associations is worse than it was in Vista or XP, the UI doesn’t allow you to specify details of the command, and executing java .jar files requires -jar option to execute a .jar file (double-click). I had to search and edit registry to set .jar file association with -jar option. This was actually problem with me installing JDK 32bit instead of JDK 64bit. After I uninstalled 32bit and installed 64bit, everything was working well, and .jar associations were set properly.

Applications I had to re-install

  • VMWare player
  • Snagit
  • PDANet – used http://junefabrics.com/pdanet/x64.php to install 64b driver, and used old PDANet 1.2 for Blackberry
  • HAVA
  • FileZilla site manager, here is how to find and restore site manager items
  • Set environment variables, I forgot to save env. variables (why there is no Import/Export for env. variables on Windows?), so I had to recreate quite a few of them (JAVA_HOME, GROOVY_HOME etc.)
  • Cygwin
  • VNC
  • Blackberry Desktop
  • Logmein
  • Lastpass
  • VLC Player
  • Java

Conclusion

So far I do agree with experts, that Windows 7 is the best Windows OS so far, yes even better than Windows XP. After disabling UAC (which was much easier than in Vista), I am happy Windows 7 user (with exception of power management mentioned above). Also Windows 7 Starter on our Asus Eee PC is running well with no glitches (on that laptop power management works as it should, have it set to sleep on close lid, wake up on open).

 

Posted in java | Leave a Comment »

Stopping or interrupting Java hanging thread

Posted by lubosp on November 15, 2009

As most of Java developers know and experienced, some Java frameworks including core Java libraries do have nasty habit to run for very, very loooong time or hang indefinitely. Classic example is Java regex library which can cause stack overflow, or it can hang indefinitely.

In order to prevent the whole application from hanging and to allow to stop or interrupt the offending hanging thread, I am using timeout thread that checks if the watched thread finished in allotted time, and if it did not the timeout thread interrupts the watched, hanging thread.

Abruptly stopping or interrupting a thread from different thread is not well supported in Java especially when Thread.stop() method was deprecated, see How to stop a thread article.

But the problem is that Java offers nothing better to forcefully interrupt the offending hanging thread. Following is an example code of the TimeoutThread:

package com.lingoport.scanner.util;

import java.util.Date;

/** Stop a thread after a given timeout has elapsed
* <P>
* A simple timeout class.  You give it a thread to watch and a timeout
* in milliseconds.  After the timeout has elapsed, the thread is killed
* with a Thread.stop().  If the thread finishes successfully before then,
* you can cancel the timeout with a done() call; you can also re-use the
* timeout on the same thread with the reset() call.
* <P>
*
*/

public class TimeoutThread implements Runnable {

private final Thread targetThread;
private long millis;
private final Thread watcherThread;
private boolean loop;
private boolean enabled;
private static final boolean TIMEOUT_DISABLED = System.getProperty("disable.timeout") != null;

/**
* Constructor. Give it a thread to watch, and a timeout in milliseconds.
* After the timeout has elapsed, the thread gets killed. If you want
* to cancel the kill, just call done().
*
* @param targetThread
* @param millis
*/
public TimeoutThread(Thread targetThread, long millis) {
this.targetThread = targetThread;
this.millis = millis;
if (TIMEOUT_DISABLED) {
watcherThread = null;
enabled = false;
} else {
watcherThread = new Thread(this);
enabled = true;
watcherThread.start();
// Hack - pause a bit to let the watcher thread get started.
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}

/**
* Constructor, current thread.
*
* @param millis
*/
public TimeoutThread(long millis) {
this(Thread.currentThread(), millis);
}

/**
* Call this when the target thread has finished.
*/
public synchronized void done() {
loop = false;
enabled = false;
notify();
}

/**
* Call this to restart the wait from zero.
*/
public synchronized void reset() {
loop = true;
notify();
}

/**
* Call this to restart the wait from zero with a different timeout value.
*
* @param millis
*/
public synchronized void reset(long millis) {
this.millis = millis;
reset();
}

/*
* The watcher thread - from the Runnable interface.
* This has to be pretty anal to avoid monitor lockup, lost threads, etc.
*
*  (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public synchronized void run() {
if (TIMEOUT_DISABLED) return;
Thread me = Thread.currentThread();
me.setPriority(Thread.MAX_PRIORITY);
if (enabled) {
do {
loop = false;
try {
wait(millis);
} catch (InterruptedException e) {
}
} while (enabled && loop);
}
// The call stop() is deprecated, but Java doesn't offer anything better
if (enabled && targetThread.isAlive()) {
targetThread.stop();
done();
}
}

// Test main
public static void main(String[] args) {
System.out.println((new Date()) + "  Setting ten-second timeout...");
TimeoutThread tk = new TimeoutThread(10000);
try {
double f = 1.;
System.out.println((new Date()) + "  Starting execution of long loop...");
for(double i = 0; i < 1.0E99; i++) f = f * i;
System.out.println((new Date()) + "  Another execution of long loop...");
for(double i = 0; i < 1.0E99; i++) f = f * i;
tk.done();
} catch (Exception e) {
System.out.println((new Date()) + "  Caught Exception");
} catch (ThreadDeath td) {
System.out.println((new Date()) + "  Caught ThreadDeath");
}
System.out.println((new Date()) + "  Finished!");

}

}

This solution seems to be working quite well, and the offending hanging thread (caused for example by Java regex) can catch ThreadDeath exception, recover appropriately and continue work.

The only problem I encountered so far is that it doesn’t play well with Eclipse debugging, that is why I disable the TimoutThread while debugging the application using disable.timeout system property.

I know, that this is not great solution, but it works, and I couldn’t find any better solution (BTW, Thread.interrupt() does not work in this case). Let me know if you have better solution.

 

Posted in java | Leave a Comment »

Why I am not Mac user

Posted by lubosp on September 18, 2009

I have small one man software development consulting company, High Country Computing. Sometimes I need to deliver product(s) that needs to be tested on Apple hardware.
I used to be Apple fan while starting with personal computing in 1988 or so with AppleII. But I switched to Windows and Linux based computers in early 1990ies and never looked back (who would pay $500-$1500 more for inferior hardware ;-).

Because of need to test software applications on Apple and Mac OSX hardware, I finally caved in and decided to buy Mini Mac. Unfortunately, the experience was terrible and I will not buy any Apple made product any time soon.

I bought Mini Mac from reseller and the Mac didn’t even boot. The only solution I was offered by Apple customer support was to bring it to authorized repair shop, which is quite inconvenient considering I live high in the mountains and I would have to drive over 100 miles (Lenovo, Dell, HP let you ship the computer for repair in their expense). It was my first experience in 15 years with Apple, and I regret buying it, and I will not recommend to my friends or at work (I am part time system administrator for small healthcare provider) to even consider Apple. I contacted Apple customer support for help, but the customer support guy was very, very unhelpful, and he was quite rude. My impression was that Apple support is terrible compared to Lenovo, HP, Dell (have hands on experience with all of them). Fortunately the Mac reseller, where I purchased the Mini Mac, was much more customer friendly than Apple and took the broken Mac back.

See you Apple!

Posted in java | Leave a Comment »

Installing and Uninstalling Windows7

Posted by lubosp on August 15, 2009

I have tried to install Windows7 RC on my HP Pavillion zd7000 laptop (1.25Gb memory, 2×3.0GHz processor) with Windows XP SP3. The installation went OK, the only problem was running Windows7 compatibility check application which hanged and never finished.

The problems came after the install, and there was enough of them to force me to uninstall Windows7 and restore good old Windows XP.
Here is the list of problems:

  • Windows XP cannot be upgraded, it requires full OS install and the install partition has to have at least 6GB free
  • I had hard time to redirect video from laptop to external monitor
  • The Windows7 migration application failed to install, so I was forced to moved the Documents and Setting and Program Files from Windows.old manually (painful)
  • I couldn’t find NVIDIA driver, so the screen resolution was only 1024×768 (XP is 1600×1200)
  • Networking had problems connecting to printer (HP J6400), and to my NAS (WD MyWorldBook), sometimes it worked, sometimes not

Fortunately there is relatively easy way to uninstall Windows7 and restore Windows XP, see this article for details.

Honestly, I am not sure why all the “expert” reviews are so positive, I have Windows XP laptop, another laptop with Windows Vista, and also desktop with Windows Vista (64b). Base on my one day experience I do not see Windows7 being worth upgrading and they are definitely NOT dramatically better than Windows XP or Windows Vista.
I still like Windows XP (they just work), but for last year plus, I mainly use Vista and after disabling UAC, I became Vista fan. It seems to me that the improvements in Windows7 are mainly cosmetic and there are quite a few things (and remind you, it is RC, so maybe they will have time to fix some issues ;-).

I am system administrator for small business (20+ desktops, 2 servers), and I will stay with Windows XP for as long as I can. No plan for me to upgrade to Windows7 any time soon! I was planning to spend $150 to get the Windows7 family pack for my home computers, but looks like I can now spend the money at Starbucks ;-).

What is your experience with Windows7?

Posted in java | 1 Comment »