Posts in the “java” category

Ant multi-line text replacement

Summary: An Ant multiline text replacement example.

I'm working on a Java project where we recently started using the JNDI capabilities in Glassfish, but our development environment has been to use Tomcat as our application server (without JNDI lookups). To keep my Apache Ant build process working in both environments I quickly learned that I was going to need to perform multiline text replacements with Ant.

The default Glassfish password is ...

Question: What is the default Glassfish password (i.e., the default Glassfish admin password, also know as the default master password)?

Answer: adminadmin. (The word "admin" repeated twice.)

It's pretty easy to miss this in Sun's Glassfish documentation, so I thought I'd share it here.

 

Flatten directories when performing an Ant copy

Here's an Ant copy task example where I begin with a hierarchical structure of jar files in my development environment (in my lib folder), then flatten out all my lib subdirectories into one directory in my production environment when I copy all the jar files to my production library directory (using the flatten attribute of the Ant copy task):

How to load multiple Spring application context files for a web application

Here's an example web.xml configuration file where I show how to configure the Spring Framework to properly load for a Java web application. I'm intentionally trying to keep this Spring example simple, so my web.xml file has almost the bare minimum configuration.

I show how to load the Spring ContextLoaderListener class with the listener-class tag, and also show how to load multiple Spring application context files using the context-param, param-name, and param-value XML tags.

Fixing a Spring "unsatisfied dependency" error message

Wow, today I thought I was doing some good code cleanup, and in my nice, working application, I merged a couple of Spring application context files into one single application context file, and got this horrible-looking Spring "unsatisfied dependency" error message when trying to run my suite of unit tests:

How to load a Spring application context file from a standalone Java application

Here's the source code for a standalone Java application where I load a Spring application context file with the ClassPathXmlApplicationContext method. In other blogs I've shown how to load the application context for web applications, but I also wanted to show how to do this for a standalone program.

Here's a source code for my Java program that loads a Spring application context file from the typical main method:

Sample Ant clean, prepare, and compile tasks

Summary: An Ant clean, prepare, and compile example.

I'm not going to discuss the following Ant build script code too much, but I thought I would share it here. In short, it shows how I declare several Ant tasks, including my typical clean, prepare, and compile tasks.

Ant FAQ: Ant replace task examples

Ant replace FAQ: Can you share some examples of the Ant replace task?

I've been sharing a lot of Ant tasks lately, and here's another example, this time some Ant replace task examples.

Ant replace task examples

The Ant build script lines shown below demonstrate how to issue a series of Ant replace commands to replace a token in the file with a variable you want to substitute for that token:

Deploy only your JSP files with this Ant build task

A lot of times when you're working on a Java web application you only need to deploy your JSP files. This happens, for instance, when you're just editing the JSP files to modify the look and feel of your web application. In cases like this there's no need to rebuild your entire application, deploy it, then restart your application server (Tomcat, Glassfish, JBoss, whatever).

Java - Getting the hostname on Windows Server 2003

Funny, this seems about five years late, but using Java, if you want to get the HOSTNAME on Windows Server 2003 (and possibly any version of Windows 2000), you have to do a little extra work. The environment variable you need to access on those versions of Windows is referred to as COMPUTERNAME, so in my case, since my software is going to be running on a lot of different (and currently-unknown) computer systems, I created a little convenience method to handle this problem.

Glassfish JDK path problem solved

Glassfish JDK path problem: Some time ago I downloaded Glassfish, and then got away from it for a while. Today when I tried to run this command

C:\glassfish\bin> asadmin start-domain domain1

I got this error message from Glassfish:

A Java DecimalFormat example

Here's a quick example of how to use the Java DecimalFormat class to format float and double numbers for output, such as printing information in a currency format.

The example below creates a simple DecimalFormat that is similar to a U.S. currency format. As you can see from the for loop, it begins printing at 100, and prints decimal numbers up to a value of just over 1,000: