Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Searching in Eclipse without the annoying CTRL-F Find Box

I've been using IntelliJ's IDEA IDE for the past few years for any Java development work. I've recently moved to a new company who are standardising around Eclipse so I'm slowly getting used to it's way of working and it's many different keyboard shortcuts.

Inline code searching is a big plus in my book and my good friend Bill Comer has figured out how to do it - read his post on In line searching in Eclipse & IE7.

If you just want to know the answer then here it is:

CTRL-J


Technorati Tags: , , , ,

How to use logrotate with Apache's Tomcat (and any other Java process)

Logrotate is an excellent utility for managing log files that can start grow into an unmanageble size, Tomcat's catalina.out is one such file.

Most of the solutions (scroll down to the 'Rotate the logs' section) for managing catalina.out, or other log files generated by Tomcat or any Java process for that matter, don't mention the copytruncate option:

Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one, It can be used when some program can not be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.
This basically means that you don't have to configure your logrotate script to restart Tomcat just so you can tidy up your log files - it trades off uptime against completeness of log records.

I found out about this from the Mission Data Blog in their post titled 'managing disk space with logrotate'. Is definitely worth taking a good look through the man page for logrotate to see if there are any other gems you can take advantage of for your setup.

Technorati Tags: , , ,

Getting Tomcat contexts to work in IntelliJ IDEA (and stop it starting Tomcat twice!)

If you run Tomcat within IDEA then you might have noticed a couple of things:

  1. Your application gets started, undeployed and then started again, increasing the start up time of Tomcat and reinitialising any load-on-startup servlets for a second time.

  2. Your carefully crafted <context> stanzas don't work as IDEA hasn't picked then up from either server.xml or META-INF/context.xml.
When you start Tomcat inside IDEA it copies the contents of your Tomcat conf directory to an IntelliJ system area (on Linux it's hidden in your home directory) and then runs Tomcat looking at those configuration files instead of the normal ones. In doing this it seems to ignore any context.xml files, any context stanzas of the server.xml and messes up any context files you might have in conf/Catalina/.

To get around this big mess and just use your Tomcat as if you ran it outside of IDEA you simply add a CATALINA_BASE environment variable to the configuration of Tomcat (within IDEA) pointing to your Tomcat directory:



Now when you start Tomcat within IDEA you should notice that your contexts are working correctly!

Technorati Tags: , , , ,

Warning: Java caches DNS to IP address lookups forever!

Found an interesting problem this morning after the IP address of a web server was changed in our DNS.

Web browsers could see the site at the new address, the command line of ping server could see the site just fine, but the long-running Java pinger application could not see the server at all. All we received were messages informing us that it could not contact the moved server.

After some digging on the internet I quickly found the reason why:

networkaddress.cache.ttl (default: -1)
Specified in java.security to indicate the caching policy for successful name lookups from the name service.. The value is specified as as integer to indicate the number of seconds to cache the successful lookup.

A value of -1 indicates "cache forever".
Sourced from: J2SE 1.4.2 Networking Properties

This is also mentioned in another post on how to Disable DNS caching.

So you need to restart the JVM to pick up recent DNS changes... or you could change the networkaddress.cache.ttl System property. But this doesn't work in some cases due to bug 6247501 - although the bug is closed, read the comments at the bottom of the page - not sure if the bug was closed after these comments or before...

Technorati Tags: , , , ,

Xalan classes go missing once Java is upgraded from 1.4 to 1.5

We found a bit of a strange problem today at work - some Xalan classes seemed to have disappeared. This week we upgraded some of our Tomcat web servers from Java 1.4 to Java 1.5. Everything appeared to work seamlessly, but today we noticed that one section of one site failed with a 'ClassNotFoundException'.

It was complaining about a couple of Xalan classes. We cracked open the rt.jar file ($JAVA_HOME/jre/lib/rt.jar) of 1.4 to see what used to be there. Lo and behold the Xalan classes were there right in the middle of rt.jar.

We then moved over to our new 1.5 install and cracked open it's rt.jar. This time the Xalan classes were hidden out of the way with a 'com.sun' prefix to every class. Obviously Sun don't want you using their shipped version of Xalan...

We decided that the best thing to do was to download a proper Xalan distro and put that in the application's WEB-INF/lib directory.

We downloaded xalan-j_2_7_0-bin.tar.gz from the UK mirror of Xalan's binaries and extracted xalan.jar & serializer.jar to be copied to our web server.

After a Tomcat restart our problem was solved!

Technorati Tags: , ,

Decompiling Java .class files using JAD

I gave myself quite a fright today when after having a problem moving some CVS-backed Java source files around, I found that the code that I had written yesterday (and failed to check in) was now nowhere to be found.

Tracked it down to just one missing source file, and luckily I had a compiled class file of the old code. Now all I needed was a tool to resurrect the source file from the ashes...

After 5 minutes of searching around on Google I found what I was looking for, a Java decompiler called JAD. I downloaded the statically linked Intel Linux version, extracted the zip and placed my class file in the extracted directory.

Running JAD was as easy as ./jad IveMessedUp.class which produced the original in double quick time.

JAD was a life-saver (well time-saver at least) and so I wanted to spread the word about this lovely tool.

Technorati Tags: , , , ,

Solving XPlanner's $DISPLAY issues on a headless Linux (Debian) server

A month or so ago at work we started playing around with XPlanner, a project planning and tracking tool for eXtreme Programming (XP) teams. I downloaded it to my Ubuntu workstation, unzipped it and ran startup_xplanner.sh. Everything worked fine, all the graphs displayed, all the reports, etc.

We then decided that we would start to use it seriously and so it needed moving to a central server. This is when our problems started. We started to get errors such as:

java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
.

This was due to XPlanner now running on a headless server but it requires the Java AWT graphics libraries to draw the graphs. These in turn use the underlying system's X server, which requires some kind of X server running.

After some investigation I found Xvfb, an X server virtual framebuffer which will happily run on a headless server.

To install Xvfb, login as root and then apt-get the package:

apt-get install xvfb

We now need to create a couple of start/stop scripts so that both Xvfb and XPlanner are started when the system reboots.

Create a file in /etc/init.d called xvfb with the contents below:

#!/bin/bash

if [ -z "$1" ]; then
echo "`basename $0` {start|stop}"
exit
fi

case "$1" in
start)
/usr/bin/X11/Xvfb :0.0 &
;;

stop)
killall Xvfb
;;
esac

Create another file in /etc/init.d called xplanner with the contents below:

#!/bin/bash

DISPLAY=:0.0
export DISPLAY

JAVA_HOME=/usr/local/tools/j2sdk1.4.2_04
export JAVA_HOME

XPLANNER_HOME=/usr/local/tools/xplanner-0.7b4-standalone

if [ -z "$1" ]; then
echo "`basename $0` {start|stop}"
exit
fi

cd $XPLANNER_HOME

case "$1" in
start)
$XPLANNER_HOME/startup_xplanner.sh
;;

stop)
$XPLANNER_HOME/shutdown_xplanner.sh
;;
esac

cd -

We need to ensure that Xvfb is up and running before XPlanner attempts to start, so we need to force the order in which the start/stop scripts are executed:

update-rc.d xvfb defaults 10
update-rc.d xplanner defaults 80

Now reboot the server and you should have an auto-starting XPlanner which actually displays the graphs!

Technorati Tags: , , , , , ,

A list of US 'time zone IDs' for use with java.util.TimeZone

If you program in Java and have to convert dates and times between different timezones then you will know that the Date, Calendar & TimeZone objects of the java.util package are the way to go.

The JavaDoc for java.util.TimeZone mentions that you can use a 'time zone ID' of "America/Los_Angeles" to get the US Pacific Time. It doesn't give examples of any other time zone ID, so here is a list of the standard US time zone IDs:

AreaAbbrevZone Name
Eastern TimeESTAmerica/New_York
Central TimeCSTAmerica/Chicago
Mountain TimeMSTAmerica/Denver
Pacific TimePSTAmerica/Los_Angeles
Alaska TimeASTAmerica/Anchorage
Hawaii-Aleutian TimeHSTAmerica/Adak

(Table sourced from Statoids: Time Zones of the United States.)

Technorati Tags: , , , , ,

Migration to Ruby for Java developers

As an experienced Java developer converting to a Ruby newbie I found the following posts from Sam Newman rather helpful in making the switch:

* Ruby For Java (and C#) Programmers, Part 1 - Conventions, methods, modules, and classes
* Ruby For Java (and C#) Programmers, Part 2 - Operators, methods, and more on classes
* Ruby For Java (and C#) Programmers, Part 3 - Introducing Arrays, Hashes and the typing system

If your anything like me having a good book by your side can really help when picking up a new language. There is one Ruby book that everyone seems to have, Programming Ruby: The Pragmatic Programmer's Guide. It really is an excellent book, I liked the first edition so much that I bought the second as well!

Let me know if you find any other resources that would be helpful to anyone else making the switch.

Another useful resource for people interested in see what Ruby is all about is Try Ruby! - an online interactive Ruby terminal that runs in a browser, created by the rather insane why the lucky stiff. Try it, you might like it!

Technorati Tags: , , , , ,

Ruby to replace Java - or compliment it?

I've been singing Ruby's praises ever since I started researching the language a couple of years ago. I've not written much Ruby code, picked up the 1st pickaxe book, read it a couple of times, then the next year came round so I've now got the 2nd edition!

I've written a few Ruby 'scripts' (no objects or classes) and have been impressed with how quickly I have been able to get it to do my bidding compared to Java. One application running on Linux needed to read some data from a Postgresql database, generate a Windows Zip file then FTP it to a remote server. When I thought about coding this in Java I started to shiver, Java's strong point has never been it's ease to interact with the command line, and FTP libraries are not part of the java. or javax. packages.

Several people have asked "so are you saying that Java is rubbish and we should all move to Ruby?". My basic answer is "no, but I want to really get into a scripting language and Ruby seems OO enough for me to get into it fairly easily".

Dave Thomas (one of the Pragmatic Programmers) said it best recently when he said "not exclusively: you're likely to want to use Rails as well as Java." Dave has started a number of posts around this topic the first is titled "Is Ruby Better Than ...?".

Java and Ruby are similar in some respects and different in others, if I need to write some quick utility scripts or manipulate some text files then Ruby would be my choice. If I need to access some middleware message queues and fit into an existing application server infrastructure then Java would be the language of choice.

At the end of the day, it's all down to adding strings to your bow, sometimes the choice is just which strings to invest your time and effort in...

Technorati Tags: , , , ,

Pretty printing Java classpaths using Ant's pathconvert task

Lance Hankins posted on Saturday about more readable classpaths using the <propertyregex> task. It jogged my memory back about 4 years when I was trying to solve the same problem. This was back in the days of Ant 1.4.1 and just before the existence of the optional Ant-Contrib package. Back then I was considered a bit of an Ant Meister by my colleagues as I managed to spend hours tweaking the smallest of things in my lovingly created Ant build environment searching for perfection...

So how did I solve this particular issue? A clue is in the title of this post, I used the <pathconvert> task, which is normally used to convert lists of files, directories, etc. and convert them to a different platform's style. So path separators using Windows ';' get converted to Unix ':', etc. But you can also use it to convert the path separators to something else, in my case a nicely formatted string for displaying in a printable form.

Here's the code:


<!-- get the source compile classpath in a printable form -->
<pathconvert pathsep="${line.separator}| |-- "
property="echo.path.compile"
refid="path.classpath.compile">
</pathconvert>

The original classpath is referenced in 'path.classpath.compile', and the newly formatted classpath is saved in 'echo.path.compile'. The magic is the pathsep statement. This says that the path separator (the ';' or ':') is to be replaced with a new line (CR or CRLF depending on operating system) followed by some characters that make the listing look nice.

To display your pretty printed classpath just use:

<echo message="|-- compile classpath"/>
<echo message="| |"/>
<echo message="| |-- ${echo.path.compile}"/>

This gives the following output:

[echo] |-- compile classpath
[echo] | |
[echo] | |-- /tomcat/common/lib/jsp-api.jar
[echo] | |-- /tomcat/common/lib/ant.jar
[echo] | |-- /tomcat/common/lib/jmx.jar
[echo] | |-- /tomcat/common/lib/commons-collections-3.1.jar
[echo] | |-- /tomcat/common/lib/commons-dbcp-1.2.1.jar
[echo] | |-- /tomcat/common/lib/commons-el.jar
[echo] | |-- /tomcat/common/lib/commons-lang-2.1.jar
[echo] | |-- /tomcat/common/lib/commons-pool-1.2.jar
[echo] | |-- /tomcat/common/lib/tools.jar
[echo] | |-- ...

So there you have it, nicely formatted classpaths using only standard Ant tasks.

Technorati Tags: , ,