Java

Mint 12 (/Ubuntu): Set up Java alternatives

Saturday, February 18th, 2012 | Java, Linux, Tech-savvy | 2 Comments

Your modern Linux distro will provide you with a more or less decent Java setup by default. Mint 12 comes with a Sun JDK 1.6.0.26 pre-installed. To switch between installed versions of your Java runtime use the

update-java-alternatives --list

command to list installed alternatives and

update-java-alternatives --set alternative

to switch them. If you still want to have different versions of the JDK you can always download them manually, untar and use them directly. So far so good.

I wanted to be able to switch my downloaded JDKs as easy as the pre-installed ones, so I was looking for a way to add my JDKs to my system’s repository. So here is how it’s done (it’s basically a copy of a nice post I found, I just wanted to spread the knowledge) – it’s a bit painful but made me feel quite good when I was done πŸ™‚ › Continue reading

Tags: ,

eclipse essentials: Grep Console plug-in

Friday, February 17th, 2012 | IDE, Java, Tech-savvy | No Comments

Every time I set up a mint eclipse I am wondering why an IMHO essential feature is missing: a configurable coloured console.

I like stacktraces to be red and “JBoss started” messages to be bold. I like the important log entry you are looking for to be highlighted with a yellow background.

All this bliss is provided by a very nice plug-in I’ve been using for years: Grep Console.

Using it is very easy and intuitive. Just select the desired text part in your standard console output, right-click and choose “Add Grep Expression”.

You can add colour and format information for background & font of the matching expression or the whole line.

That’s basically it, there is not much more to say, yet sometimes it’s the simple things that make us happy, right? πŸ™‚

Go get your Grep Console using this URL as your eclipse update site: http://eclipse.musgit.com

Tags: , , , , ,

Monitoring JBoss AS start with JMX

Friday, September 3rd, 2010 | Java, Tech-savvy | 2 Comments

Today a fellow colleague asked for a better way to check whether an app server is really up and running than grepping the log file. Well, for we are using a JEE compliant server (JBoss) I suggested looking into JMX for it is the designated monitoring interface, right? Unfortunately I didn’t get replied to even after suggesting it twice. Whatever, apart from proving brilliant communication skills I may have missed something that didn’t make JMX an option (like a firewall issue or something).

Still, I wanted to look into it anyway, because it seemed a fun and pretty simple task to write a little JMX client that probes the desired MBean. But it is even easier. JBoss offers a little helper out of the box that is up to the task in no time. It’s a little tool called twiddle. Usage is self explanatory:

A JMX client to 'twiddle' with a remote JBoss server.
 
usage: twiddle [options] <command> [command_arguments]
 
options:
    -h, --help                Show this help message
        --help-commands       Show a list of commands
    -H=<command>              Show command specific help
    -c=command.properties     Specify the command.properties file to use
    -D<name>[=<value>]        Set a system property
    --                        Stop processing options
    -s, --server=<url>        The JNDI URL of the remote server
    -a, --adapter=<name>      The JNDI name of the RMI adapter to use
    -u, --user=<name>         Specify the username for authentication
    -p, --password=<name>     Specify the password for authentication
    -q, --quiet               Be somewhat more quiet

Now you only have to decide which MBean to probe. As “are u there” is one of the most basic thinks to ask for this isn’t too hard either:

twiddle.sh get jboss.system:type=Server Started

As soon as this returns true the server is up and running.

So, just put this command in your shell script and you can easily probe your JBoss AS if it is ready to go app serving.

Tags: , , ,

eclipse shortcuts

Thursday, August 12th, 2010 | IDE, Java, Quirks | 2 Comments

Kids, after a hard day’s labour I Crtl-Alt-S’ed myself into the team sync perspective to commit my stuff. After doing so this conversation happened in our team room today:

What was the shortcut for getting back to the Java perspective again?
Hmm, dunno, hit Ctrl-Shift-L
What’s that?
That’s the shortcut for the shortcuts.

True Story. 😎

P.S. Technically Ctrl-Alt-S doesn’t mean “switch to the team sync perspective” but “sync with repo” implicating a switch to the team sync perspective.
P.P.S. Don’t tell me to use Ctrl-Shift-F8 – first, it’s not “switch to Java perspective” and second that’s the dumbest non-accessible shortcut ever. I always reassign it to Ctrl-Tab thus being far more convenient IMHO.
P.P.P.S. Yes, I know you can assign a shortcut to the not bound by default Java perspective.
P.P.P.P.S. If you are still reading this you didn’t get the post’s joke. It’s not about the Java perspective at all. πŸ˜€

Tags: , ,

Windows 7 ClearType WTF?

Sunday, March 28th, 2010 | IDE, Misc, Tech-savvy | 2 Comments

I recently had to switch to Windoze 7. I am not the kind of guy who likes migrating to new OSs – the biggest challenge was the switch from an all tuned Win98 SE to Windows XP almost a decade ago. Well, times change, and I don’t have to be on a first name basis with every file on my system partition any more. Things should just run, stable, fast and smart (oh, it does all that by itself?). Something that Linux Mint does for me every time I need a free OS for a random project (HTPC, friend’s netbook, arcade emulation machine, you name it). Windows 7 has been a great piece of software so far, it’s as fast as XP, has a good look and feel and does smart things like getting drivers from M$ so I don’t have to dl them from third party websites (btw: Dell, I pretty much hate you for your “support” section… it’s such a mess!).

All right, back to topic. I want to share some experience I gathered with Microsoft ClearType technology. This is a short description taken from Wikipedia:

ClearType is a trademark for Microsoft‘s implementation of subpixel rendering technology. ClearType attempts to improve the appearance of text on certain types of computer display screens by sacrificing color fidelity for additional intensity variation. This trade-off is asserted to work well on LCD flat panel monitors.

With ClearType enabled I have the constant feeling that my display has a focus problem. I’m really into pixels πŸ˜‰ and I want to see ’em, not some blurry optimized mud. I understand the need for soft-focused cutting/editing using image manipulation software like Photoshop or Gimp, but fonts should be plain (black) pixels on plain (white) background. This is just my opinion, others may like the new style better.Β  I also agree that ClearType makes sense on a certain display size / resolution / dpi ratio, say a 20″ full-hd display with 120 DPI could be a good choice for subpixel rendering. I prefer the old style ratios like 20″ 1680×1050 75 DPI or 24″ full-hd 75 DPI.

Anyway, here are some examples of bad “optimization” and how I got rid of them, well, almost.

The first program that hurt my eyes was the eclipse IDE. Being a software developer I work with the eclipse workbench on a daily basis. What I did was copy my eclipse workspaces from my old XP box to the new machine. (Something I really like about development tools (eclipse, Java, MySQL, JBoss, …) – they don’t have to be installed, just unzip/copy ’em and you are ready to go. )

I launched eclipse and something felt different. I blinked my eyes. Still, something odd was displayed on my screen. I thought “OMG, ClearType”, so I disabled this feature as I did every time before I ever came across it (mostly Internet Explorer). The font rendering in the text editor was fine now, but suddenly the explorer tree looked horrible. See for yourself (click to enlarge, browse the enlarged pictures to see the difference):

I was stuck with either a nice representation of the explorer tree or a feel good editor. This sucked big time! But I found a solution to this problem, which included the enabling of the ClearType feature. Yikes! After fiddling with eclipse’s font settings I found the bugger! “Courier New” (the font I had used for source code for years) is getting rendered blurry, “Courier” doesn’t (@see font dialogue on the right hand side).

Switching from “Courier New” to “Courier” solved the rendering issue, but I did not like the line-height of “Courier”, the code seemed to be squeezed together, but this is another issue πŸ™‚

The rendering “bug” was found and “Courier New” is history to me – today I use “Segoe UI” and I am pretty fond of the font. (Plz forgive me for that bad joke, it was just too tempting… πŸ™‚ )

Conclusion: What I find annoying about the font rendering settings is the fact, that you cannot choose between an old sk00l font rendering style and the new ClearType way, meaning 1:1 pixels vs. subpixel rendering. You can only choose between bad (ClearType on) and worse (ClearType off) font rendering using Windows 7. While I am typing this my eyes really feel stressed reading on my Win7 machine and it’s always a pleasure to go back to my private ole WinXP desktop. *Sigh* It seems to me M$ foobared the non-ClearType rendering that bad you just have to use ClearType. So they can say: “Look, it all looks better with ClearType”. Yeah, right!

But M$ does not have to dictate how fonts are getting rendered. Software can give you the opportunity to choose the font rendering style. Read on for a good example on how a piece of software lets you choose how fonts are displayed.

Another tool I use on a regular basis is PuTTY. Again, “Courier New” used to be my weapon of choice for shells and as soon as I started using PuTTY on Windows 7 with ClearType enabled my eyes were screaming “Focus! Fooocus!” – I opened the font preferences dialogue and found some pretty interesting settings.

PuTTY gives you the opportunity to choose a font “quality” from Antialiased, Non-Antialiased, ClearType and Default. Default is enabled by, well yes, default, which is ClearType on systems that have ClearType enabled. I simply chose Non-Antialiased and all my font problems were solved. I can stick to “Courier New” with a proper line-heigt (in contrast to “Courier”) and crisp font rendering. Finully! Now I just have to wait for other tool developers to offer similar options to end users or the world falling into subpixel rendering apathy. Either way, until 200+DPI displays are released to the open public I will miss the good ole “a pixel is just a pixel” days and try to eliminate bad display “optimization”.

Tags: , , , , , ,

Generation of MD5/SHA File Hashes in Java

Sunday, July 26th, 2009 | Java, Tech-savvy | 2 Comments

This post is about generating file hashes in Java. I came across the need to generate file hashes for a media application that I am working on and I wanted to implement a way to identify dupes. The best way IMHO is a hash code of the file, which has a constant size (even for large files) and can be easily compared to other hash codes thus making the identification of dupes a breeze.

Java provides a .hashcode()method for all objects, inherited by java.lang.Object – but this is not what we are looking for as this excerpt of the Java SE6 API Doc states:

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

To be perfectly honest it would be quite silly to believe that the .hashcode() method of java.lang.Object would be sufficient for generating file hashes. We might be lucky enough, that the .hashcode() method of java.io.File overrides the default behaviour of Object to something more suitable for files. Well, it does indeed, but this is still not what we want (API Doc excerpt):

Computes a hash code for this abstract pathname.

Well, java.io.File.hashCode() computes a hash based on the pathname. Again, not suitable.

What we really need is a method, that reads all the bytes of a file and computes a hash of the file contents, not some meta data. This is how we do it (not my work, just the first snippet Google provided):

public static String generateHash(File file)
		throws NoSuchAlgorithmException,
		FileNotFoundException, IOException {
 
	MessageDigest md = MessageDigest.getInstance("SHA"); // SHA or MD5
	String hash = "";
 
	byte[] data = new byte[(int)file.length()];
	FileInputStream fis = new FileInputStream(file);
	fis.read(data);
	fis.close();
 
	// Reads it all at one go. Might be better to chunk it.
	md.update(data);
 
	byte[] digest = md.digest();
 
	for (int i = 0; i < digest.length; i++) {
		String hex = Integer.toHexString(digest[i]);
		if (hex.length() == 1)
			hex = "0" + hex;
		hex = hex.substring(hex.length() - 2);
		hash += hex;
	}
 
	return hash;
}

This worked for me, but there are (at least) two things I don’t like about this solution. First, as the comment already states, this method reads the whole file at once – this will give you an java.lang.OutOfMemoryError: Java heap space exception quite fast. Second, the for loop tinkers the String representation of the hash – this is error prone and not easily maintainable.

So I looked further an came across this solution:

public static String generateBufferedHash(File file)
	throws NoSuchAlgorithmException,
	FileNotFoundException, IOException {
 
	MessageDigest md = MessageDigest.getInstance("MD5");
 
	InputStream is= new FileInputStream(file);
 
	byte[] buffer=new byte[8192];
        int read=0;
 
        while( (read = is.read(buffer)) > 0)
                md.update(buffer, 0, read);
 
        byte[] md5 = md.digest();
        BigInteger bi=new BigInteger(1, md5);
 
        return bi.toString(16);
}

Wow, just a small helper method, a buffered reader that hashes large files without taking too much memory and a provided toSting() method. This is just what I was looking for. I hope some people out there save some time trying to implement their file hash solution reading this post. Happy coding!

P.S.: If you care about the hash algorithm used (e.g. MD5 or SHA) have a look at java.security.Security.getProviders() and the .getInfo() of each given Provider.

Tags: , , , ,

SpringSource Certified Spring Professional

Friday, July 24th, 2009 | Certification, Java | No Comments

cert_spring_proAs of today I am a SpringSource Certified Spring Professional. I am totally happy that I made it, this was one of the most challenging certifications I ever achieved (okay, the Certified JBoss Developer was even harder, but it is “open book”).

I can’t go into the details right now (and I am not allowed to) because I have some friends over for a beer. Just one thing:Β  JavaBlackBelt provides a great summary of the topics that will be in the exam. The Spring Reference Documentation is just awesome and will give you all the information that you need. It is a lot of information, but it’s worth the effort. Okay, gotta go partying πŸ™‚

Tags: ,

eclipse IDE Subversion integration – Galileo still foobared

Sunday, July 19th, 2009 | IDE, Java, Tech-savvy | No Comments

eclipseAnother post on getting your subversion running in eclipse – this time it’s the brand new Galileo release.

To cut a long story short: SVN integration is still a manual tedious process. I don’t want to complain too much this time, now that I know it’s because of legal issues eclipse is not coming with an out of the box SVN support – even though the Subversive team provider is part of eclipse, the connectors cannot be published together with Subversive because the eclipse legal rules don’t support the connectors’ license, just Google it for more information)

So here is how you do it:

Help ->Β  Install New Software… -> Galileo -> Collaboration -> Subversive SVN Team Provider (Incubation)

Help ->Β  Install New Software… -> Add -> http://community.polarion.com/projects/subversive/download/eclipse/2.0/galileo-site/ -> SVN Connectors

Install all or just the connector you really need, my favourite one is SVN Kit, because it works fine and has svn+ssh:// support.

If you chose to install all connectors you can change the implementation in Window -> Preferences -> Team -> SVN

Tags: , , ,

meet the experts @ codecentric

Sunday, June 28th, 2009 | Java, Reviews, Tech-savvy | No Comments

“meet the experts” – that is the title of a great series of small highly specialized conferences organized by my company codecentric. The goal is to bring together experts to share their knowledge and experience, followed by a so called open space, where everybody can start discussion groups about a related topic – experts included. Share your thoughts with the guys who are the top notch players – you don’t get a chance like that at a “normal” conference.

The first event (focus on performance) is over and it was great! My colleague Rob was faster than me in writing his review and I basically share his experiences (Kirk Pepperdine talks on performance and his history lessons on “community driven” eclipse as well as Toplink vs. Hibernate, Heinz Kabutz talks about performance as well, Crete and why he refused to play Guitar Hero, Alois Reitbauer talks about the Titanic from a software development POV and assures the audience a few times “This is not a sales presentation”). So go on and read Rob’s blog post about the event. More are to come, so make sure you keep any eye on the meet the experts web site and join in for one of the next sessions, they sure will be great experiences as well. The next event will be about agile software development, which will be a topic with highly esoteric open space sessions to come. I am really looking forward to this event and share my experience with other pros.

Last but not least: catering was first class and the event went on till late with not everybody going home sober. A great event off the beaten track! Thank you, codecentric.

Check out the slideshow at flickr!

Tags: , , ,

Speedtesting my a$$

Thursday, June 25th, 2009 | IDE, Tech-savvy | No Comments

I just moved into a new flat and as soon as my provider got me hooked up again I was keen on measuring the DSL speed I pay for. I tried a few so called speed tests, but they gave me awkward results.

There is a way to test the speed of your connection that worked for me. Download a recent release of eclipse (Galileo was just released) using a BitTorrent client. Whoooha, full throttle at the expected download rate. Now I am assured that I really get what I pay for.

I’ll never try to use some speed testing websites again… what a waste!

Tags: , , ,

Search

Categories