Posts in the “java” category

Java random numbers (how to create)

Java random numbers FAQ: How do I create random numbers in Java?

To create random numbers in a Java program, use the java.util.Random class.

As a simple example, this code generates a random number between 0 and 9:

Ant and Java assertions (Enable Java assertions in Ant)

Enable Java assertions in Ant (FAQ): How do I enable Java assertions in Ant?

Here's something that should have been a little more obvious to me: how to enable assertions when compiling Java code that uses the version 1.4 "assertions" feature:

An Ant build script for web applications

Ant web build script FAQ: Can you share an example Ant build script for a web application?

Sure, here's a sample Ant build script for a Java web application. I've included other example Ant scripts for Java Swing/JFC/GUI applications on this website, but this one demonstrates how to use Ant to build a web app instead.

Ant WAR task - A sample Ant build script that builds a WAR file

You'll probably have to know a little bit about Java, Ant, and build files for this sample build.xml file to be any use to you, but if you're looking for a sample Ant build script that can be used to create a war file, or one that simple uses a war task, this example might work for you.

I'm not going to provide any explanation of any of this right now, but if I have time in the future I'll come back here and add a few comments about how this build script works.

Debug Ant build problems with echo statements

Ant FAQ: How to use echo commands to debug Ant build scripts.

I've been moving back and forth between a ton of different computers lately -- Mac, Linux, and Windows computers -- and I had some problems with an Ant build script. This particular Ant script is complex, and I kept making mistakes, and needed a good way to debug the build process as I set up my application on these different systems.

A Glassfish command reference (cheat sheet)

Here's a copy of a Glassfish cheat sheet (reference page) that I always keep near me whenever I'm working with a Glassfish server. I like a lot of things about Glassfish, but trying to remember all of these commands for the Glassfish CLI is not one of my favorite things.

Here's my Glassfish command reference:

What is the command to start a Glassfish server?

Question: What is the command to start a Glassfish server?

Answer: I use the following command to start my Glassfish server:

asadmin start-domain domain1

Although this command refers to starting domain1, at least in my case, it also serves to start the entire Glassfish server. I don't know how this works for multiple domains, but my guess is that if you start any one of your domains, you will start up your Glassfish server.

What is the command to change my Glassfish admin password?

Question: What is the command to change my Glassfish admin (administrator) password?

Answer: You can change the Glassfish admin password with this asadmin command:

asadmin change-admin-password

The Glassfish master password

Note that you can also change the Glassfish master password with this command:

asadmin change-master-password

 

A Tomcat startup script that deletes old log files before starting up

One thing I don't like about using Tomcat in development and test environments is that the log files tend to grow, eventually growing for days on end if you don't clean them out. Having all these old log files hanging around just makes it harder to debug development problems, so I like to delete them all every time I restart Tomcat.

How to compile a Java program with Ant

While I'm digging around through Ant build scripts today, here's a sample Ant target I created that demonstrates how to compile a Java program, while also showing a few other things, including how to exclude your JUnit tests during the compile process.

Ant - How to use a date or timestamp in an Ant build script

Summary: An Ant date and timestamp (tstamp) task example.

I was just digging through some Ant build scripts I've created, and I noticed a segment of a build script that first creates a timestamp, and then uses that timestamp in the process of creating a manifest file. (This build script is used for building a Java Swing application.)

Here's the code from my Ant script that does this timestamp magic:

An Ant 'exclude classes' example

Problem: You want to build your Java project using Ant but you need to be able to skip certain files -- typically unit test files -- during the compilation or deployment processes.

Solution: You can skip files during the Ant compilation process by using the Ant exclude pattern. Here's an example that shows several exclude patterns in some XML code taken directly from an Ant build script:

A better test for detecting Unix operating systems in an Ant build script

In several previous tutorials (see my references below) about testing for operating systems within Ant build scripts, and then conditionally executing targets based on the results of those tests, I noted that Mac OS X operating systems respond to both Mac and Unix test conditions based on the Ant "os family" test. I mentioned that I thought this behavior was probably correct, because Mac OS X is built an a Unix base (BSD, to be specific).

Spring Dao/JDBC tip - a great way to test your Spring DAO code

The Spring Framework gives you a great way to test your Spring JDBC (Spring DAO) code. Just use a special Spring JDBC class with a very long name, and you can test your Spring JDBC code against a real database.

Some people don't like this approach, but I think it's invaluable for (a) testing your SQL syntax when you first create it, and (b) making sure your SQL syntax is still valid after database changes occur.