Tuesday, January 22, 2008

Read PDF on your PDA - pdfcrop

I've a lot of pdf - books, tutorial an so on - collected everywhere in internet.
My dream was to read them on my Nokia 770 when I'm not home like a normal book, but they were too small and hard to read.

Now I've found a wonderful small tool that solves this problem: pdfcrop.

According to his guide:
PDFCROP takes a PDF file as input, calculates the BoundingBox
for each page by the help of ghostscript and generates a output
PDF file with removed margins.


I've tryed it until now with dozens pdf and I've to say that it does his job very well1!!!

Tuesday, January 15, 2008

The easiest way to print text

The most of us use System.out.println to print to the screen but maybe a more conveniet way is

PrintStream out = System.out;
out.println("print something");


because if you want to redirect your output to a file, you have simply to change the construction of PrintStream as follows:

File myfile = new File("myfile.txt");
PrintStream out = new PrintStream(myfile);


Remember that, if you're writing a command-line program that can be executed everywhere in the filesystem, to precede your filename with the complete path of the working dir

String workingDir = System.getProperty("user.dir") 
+ System.getProperty("file.separator");
File myfile = new File(workingDir+"myfile.txt");

Sunday, January 13, 2008

Java Command Line apps under Linux

Your java command line apps cannot properly work under linux because of the spaces beetwen arguments.

For example the following bash script gives you wrong results:
#!/bin/sh
java -classpath $CLASSPATH eu.kostia.ejb.client.SendEmail $*


This is instead the right way:

#!/bin/sh

# concatenate args and use eval/exec to
# preserve spaces in paths, options and args
args=""
for arg in "$@" ; do
args="$args \"$arg\""
done

CLASSPATH=lib/commons-cli-1.1.jar:bin
cmd="java -classpath $CLASSPATH eu.kostia.ejb.client.SendEmail $args"

eval "exec $cmd"

As you can see, we concatenate first the arguments (enclosed in "...") and then we execute the command.