Aug
17
Shelling out in Java – beware, not as simple as it first appears!
Filed Under Computers & Tech on August 17, 2005 | 1 Comment
Yes, you should of course avoid ever shelling out in Java, but from time to time shelling out becomes the lesser of two evils (the greater usually being writing a native class) and when that happens you need to be aware of some extra complexity that is often over looked and can result in some very strange and hard to diagnose run-time errors!Many people think that all you have to do to shell out in Java is to do something like the following:
Runtime.getRuntime().exec("/usr/local/bin/convert x.png y.jpg");
This works fine if, and only if, you never assume in the rest of your program that this call completed. Or to put it another way, if your script tries to use the output of the program you shelled out to just launching it in that way could result in inconsistent behavior at run time.
Many programmers who are used to shelling out in Perl or the like assume that Java works in the same way and that the program shelling out will wait till the process it shelled out to completes before moving on. In Perl that is a correct assumption, in Java it is not! The shelled out process runs in a separate thread and Java just carries on execution regardless of what is happening with it’s baby process!
If you need Java to wait because you need to manipulate some outcome from the process you spawned or if you need to find out whether is succeeded or failed you need to do a little more. The following is a function I wrote to shell out properly in a project I am working on, it should be a good enough example for you to work out what to do!
static void executeCommand(String cmd) throws Exception{ try { Process p = Runtime.getRuntime().exec(cmd); if(p.waitFor() != 0) { throw new Exception("The command " + cmd + " returned with an error)"); } } catch(Exception e) { // do what ever error handeling you want } }
Aug
16
It’s the little things that continue to impress me!
Filed Under Computers & Tech on August 16, 2005 | Leave a Comment
I’ve had my Mac Mini for a few months now and it is still surprising me from time to time. It comes with built in speakers which, for the size of the thing, are very impressive, but of course nothing on proper speakers. I had the volume up full with no speakers plugged in, then while something was playing I plugged in the speakers not realizing that they were on, did I get my eardrums blasted off? No! OS X turned the volume down for me when it detected the speakers being plugged in!
It really is the little things that make a lasting impression …..
Aug
14
OSXPM – How did I ever do without it!?
Filed Under Computers & Tech on August 14, 2005 | Leave a Comment
Something that I always thought was a bit wrong with OS X was that there seemed to be no way of un-installing a .pkg file once you installed it. As a result I tended to avoid software that used .pkgs and instead opt for software that used the more standard OS X technique of being entirely encapsulated in a .app file with no installer. Not anymore thanks to OSXGNU.org!
OSXGNU.org have created a nice COCA GUI to allow you to easily install and more importantly un-install .pkg files. It is an absolute must have and comes it self as a .pkg and installs it self in the Applications folder under Utilities.
You can download it here: http://old.osxgnu.org/software/Utils/OSXGNU/
Here is a screenshot from their webpage:
Aug
14
Colour ls on OS X 10.3 and Higher
Filed Under Computers & Tech on | 2 Comments
Many people seem to go to bizare lengths to get colour ls working on OS X and end up doing things like building their own ls from source or getting fink or darwinports to build a new ls for them. This is utterly pointless as the ls that comes with OS X 10.3 and higher has colour support built in!
So, how do you enable it?
For some reason best known to themselves the clever people in Apple decided that the flag for colour on ls should not be –color like it is on just about every other OS on the planet but instead the rather bizarre choice of -G!
So, to enable colour ls all you have to do is add the following to your .bash_profile file:
alias ls="ls -G"
If, like me, you like to have a black background on your terminal you may also want to set the colours so that folders are an easy to see yellow instead of an eye-hurting dark blue. To do this just add the following to your .bash_profile too:
export LSCOLORS=dxfxcxdxbxegedabagacad
Don’t ask me what the letters means, all I know is it works!