Posts in the “java” category

A Java class that lists constants for all FTP server return codes

Here's a Java class that I created today that creates constants for all the FTP server return codes listed on this Wikipedia page. If you ever start doing programming against a real FTP server, or perhaps a mock FTP server, you'll know what these FTP return codes are needed for.

Without any further delay, here's the Java source code for a class I've named FtpServerReturnCodes:

Using JavaMail with multiple search terms (Yahoo mailbox POP3 searching)

I normally don't share source code that doesn't work, but I just want to make a note here for myself on how to use JavaMail to search for multiple search terms. While the code doesn't currently work, I believe it does show the correct syntax for searching for multiple search terms, and there's just something about the overall JavaMail API (or Yahoo Mail) that I don't understand at this moment.

Search a JavaMail mailbox for unseen messages

While working on a Java application yesterday, I ran into a situation where I needed to search my POP3 mailbox for unseen messages, i.e., email messages I haven't read yet. (I'm adding some "You've got mail" functionality to one of my automated robots.)

Jumping right to the solution, here's the complete source code for a JavaMail example class that demonstrates how to search for unseen (unread) email messages in a POP/POP3 mailbox:

A Java send mail class - A simple class to simplify sending email

Here's the source code for a simple "Java send mail" class that can simplify the process of sending email messages from Java applications. I've named this class MailAgent, and I'm sharing the source code here for free. I've included a brief usage statement in the Javadoc at the top of the class. Hopefully it's all pretty straightforward, and a decent example of using the JavaMail API to send an email message.

A JavaMail POP reader example (pop3 reader)

While working on a Java application, I found the need for a simple Java mailbox reader. I wanted to be able to scan through one of my POP/POP3 mailboxes for messages, and then do something with those messages.

To that end I created the following example JavaMail POP mailbox reader. It connects to a standard POP/POP3 mailbox, then scans through all the messages in the "Inbox" folder.

Java: How to test if a String contains a case-insensitive regex pattern

Java String FAQ: How can I tell if a Java String contains a given regular expression (regex) pattern?

In a Java program, you want to determine whether a String contains a case-insensitive regular expression (regex). You don't want to manipulate the String or extract the match, you just want to determine whether the pattern exists at least one time in the given String.

Ant FAQ: Ant copy task examples

Java Ant build FAQ: Can you share some examples of the Ant copy task, i.e., the Ant copy task syntax?

Sure. I've shared quite a few Ant examples on the website now (just search the website for "Ant"), and here's another snippet of code from an Ant build script that shows how to use the Ant copy task.

As you can see from this sample code, I'm using the Ant copy task to copy files from source locations to other destination locations.

How to read a file URL in Java and Scala

I thought this might be harder, but if you have a file URL like “file:///Users/al/SherlockHolmes.txt”, and want to read those file contents as a Java URL, the process in Java and Scala is simple, as shown in this Scala code:

How to write Java STDOUT and STDERR to a file

I just learned how to send STDOUT and STDERR to a file in a Java application (without using Unix shell redirect symbols). Just add these two lines of code to the main method in your Java (or Scala) program:

System.setOut(new PrintStream("/Users/al/Projects/Sarah2/std.out"));
System.setErr(new PrintStream("/Users/al/Projects/Sarah2/std.err"));

Then when you use:

System.out.println("foo");

or

How to set vertical and horizontal scrollbars on a Java JScrollPane

If you want the horizontal and/or vertical scrollbars to always show on a Java JScrollPane, configure them like this:

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

Other options are:

HORIZONTAL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_NEVER

and:

How to set the Font on Java Swing components (JTextArea, JLabel, JList)

Here are a few examples of how to set the Font on Java Swing components, including JTextArea, JLabel, and JList:

// jtextarea
textArea.setFont(new Font("Monaco", Font.PLAIN, 20));

// jlabel
label.setFont(new Font("Helvetica Neue", Font.PLAIN, 14));

// jlist
list.setFont(new Font("Helvetica Neue", Font.PLAIN, 12));

A test application

You can use code like the following Scala code to easily test different fonts. Modify however you need to, but it displays a JFrame with a JTextArea, and you can change the font on it: