Posts in the “java” category

A Java method that tests if a String is null or blank

Problem: You're working on a Java application, and you're repeatedly performing a test to determine whether a String (or many strings) are either blank or null.

Solution

Create a simple Java method to perform this test for you. The following Java method returns true if a String is blank or null, otherwise it returns false:

Java StringTokenizer example

Can you show me a Java StringTokenizer example?

Sure. The Java StringTokenizer class can be used to split a Java String into tokens. Here's a simple StringTokenizer example:

// start with a String of space-separated words
String tags = "pizza pepperoni food cheese";

// convert each tag to a token
StringTokenizer st = new StringTokenizer(tags," ");

while ( st.hasMoreTokens() )
{
  String token = (String)st.nextToken();
  System.out.println(token);
}

This segment of code will have this output:

A Spring MySQL BasicDataSource connection example

Spring MySQL FAQ: Can you share a Java Spring MySQL example, showing how to create a Spring Framework application context file so a Java standalone application can connect to a MySQL database?

Sure, here's a Java/Spring MySQL example, specifically showing a Spring application context file that sets up a BasicDataSource (connection) to let your Java application connect to a MySQL database.

Java benchmark performance - A Stopwatch class for timings, benchmarks, and performance tuning

I think I found the Java Stopwatch class (shown below) in a book named Java Platform Performance. You can use the class for general performance timings, or make it part of an overall benchmarking system.

I take no credit for this Java code -- I didn't write it, and at the moment I can't find that book -- but I found it on my laptop, and wanted to make sure I have a copy of it laying around for when I need it.

Java directory list - list all files matching a filename pattern

Java file and directory FAQ: How do I get a list of all files in a directory that match a certain filename pattern?

For a recent Java project I needed to be able to get a list of files within a directory that matched a specific filename pattern. In this case, I needed a list of all files in a directory that began with the underscore character (_). This is relatively easy in Java; as usual it's just hard to find a good example.

A Java “ping” program/class

Java ping FAQ: How do I ping a computer from a Java program (or Java class, or Java method)?

I've been working on a new Java networking application, and as part of network debugging, I wanted to be able to ping a server from my Java program. I thought writing a "Java ping" class/program would be straightforward, but in short, it wasn't, so I wrote a little helper class to let me call the system ping command, and use the output from it.

JList ListSelectionListener example

I need to work on this JList ListSelectionListener example some more, but I thought I'd share it in its current state, in case it will help anyone get started.

Sample Cobertura ant build script

Summary: A Sample Cobertura Ant build script.

I still haven't gotten around to writing a Cobertura code-coverage tutorial, but in lieu of that, I thought I'd include an ant build script here that does a lot of powerful things, including a task that generates Cobertura code-coverage reports.

A sample ant build script for a standalone Java application

Ant Java build scripts: Can you share an example of an Ant build script for a standalone Java application?

This is a sample Ant build script for a Java application I wrote. It is a GUI/Swing application named "AlwaysOnTop". I include it here because I'm a big believer in learning by example, and maybe this Ant script can help you create an Ant script for your own applications.

A Java Log4J example

Java Log4J FAQ: Can you share a simple Log4J example?

I don't have a lot of time today to get into all the Log4J properties and configuration options you can use, but I wanted to share at least one Java Log4J example here, so here goes.

A Log4J example Java class

The following Java Log4J example class is a simple example that initializes, and then uses, the Log4J logging library for Java applications. As you can see the configuration is pretty simple.

Java properties: How to get the user’s home directory

Java properties FAQ: In Java, how do I determine a user’s home directory?

Answer: I often need to do this when creating Java/Swing GUI applications. Fortunately it's simple (once you know how to do it), just use a line of code like this, using the Java System class getProperty method:

String homeDir = System.getProperty("user.home");

On my Mac OS X system, the String homeDir now contains the following content:

Java concurrency classes, utilities, and tutorials

Just some quick note here today as I dig into the Java 5 concurrency classes and utilities. Mostly, I just wanted to save a few good links I've found related to Java concurrency and multi-core programming:

A custom Java mouse cursor that displays the mouse XY coordinates

Java mouse FAQ: How do I create a custom Java mouse cursor (mouse pointer)?

For my Java/Swing XY mouse position/coordinates application I finally dug into Swing and figured out how to create a Java mouse cursor that shows the X/Y position (coordinates) of the mouse cursor at any moment in time. I can't figure the right way to word that, so it may not make sense, but hopefully this picture of what I've done will help demonstrate my custom Java mouse cursor:

Java - custom mouse cursor - show X/Y coordinates/position/location

How to cast a null value in Java

Until a little while ago I don’t think I had ever thought about intentionally casting a null value in Java, but then I ran into a problem and realized that the solution was to cast a null value, like this:

FileDialog d = new FileDialog((java.awt.Frame) null);

You have to do that in this case because FileDialog has several one-argument constructors, including one that takes a JFrame and another that takes a JDialog. If you just put null in the constructor the Java compiler or your favorite IDE will complain, so you have to cast the null value to one of those specific types, and this syntax shows how to do this. (My app uses multiple frames, and at the moment I’d rather put null in the FileDialog constructor than try to determine which frame is currently in the foreground.)