Scala, Java, Unix, MacOS tutorials (page 378)

Recently I was asked "How can I maximize a frame (JFrame) in Java?"

Like a lot of things, once you know how to do it, it turns out to be pretty easy.

How to maximize a JFrame

Here's some code that shows how to maximize a JFrame:

Perl file exists FAQ: How can I perform a test to see if a file exists with Perl?

You can test to see if a file exists with the Perl "-e" file operator.

A Perl file exists example

Here's a short test/example:

Several years ago I taught classes on Java programming and Object-Oriented Analysis and Design (OOA/OOD). After using Java training material from other companies for a little while (and being unhappy with their quality and high fees), I decided to write my own Java course material.

Today I'm glad to announce that I'm giving this Java and OOA/OOD training material (150+ pages) away for free -- no registration required. (I always hate it when I have to "register" to get something free, so you'll find no registration forms here.)

Linux crontab mail FAQ: Can you share an example of a Linux crontab entry you use to send email on a regular basis?

Solution: Here’s the source code for a really simple Linux mail script that I used to send an email message to one of my co-workers every month. This script used the Unix or Linux mail command to email a file to her that showed a list of all the websites on our server that she needed to bill our customers for.

Java file FAQ: Can you demonstrate how to use the Java FileReader class?

I have a number of examples on this site about how to read a file with Java, but I've never dug into it from the aspect of how and why to use a Java FileReader, so I thought I'd take a few moments and dig into the FileReader class in this article.

The Java FileReader class

The FileReader javadoc provides a nice introduction to the FileReader class:

Summary: Java regular expressions in Java 1.4, featuring regex expressions and pattern matching, using the new Java Pattern and Matcher classes.

The following Java example offers an introduction to regular expressions in Java 1.4. In this code we're creating a regular expression that can search for a date. Specifically, that date must be in a format of two digits, followed by a hyphen, followed by two digits, followed by a hyphen, followed by four digits.

This date pattern is created in this line of code:

Java FAQ: Can you share some examples of common Java regular expressions?

The following list shows some of the most common regular expressions that are used in computer programming. I haven’t tested any of these yet with Java, but I think most will work off the shelf, or should at least work with minor tweaks.

An interesting note is that there are several ways to write the same regular expression, and I intentionally tried to stress that in the examples below.

Question: Using Java, how can I open a URL from my program, and then read the content of that URL?

A couple of days ago I was asked how to sort the output from the Unix and Linux ls command. Off the top of my head I knew how to sort the ls output by file modification time, and also knew how to sort ls with the Linux sort command, but I didn't realize there were other cool file sorting options available until I looked them up.

In this short tutorial I'll demonstrate the Unix/Linux ls command file sorting options I just learned.

Java File I/O FAQ: How do I open a file and read text from it using Java? (Also written as, “How do I use the Java BufferedReader and FileReader classes to read from a text file?”)

Solution: Here’s a method taken from a Java class I wrote that shows how to open and read a file using the Java FileReader class.

Unix/Linux processes FAQ: Can you share some examples of the Linux ps command? (Or, how do I use theps command?)

The basic Linux ps command

If you run the ps command by itself, it only shows very basic information about the processes you are currently running. For example, if you issue the basic command like this without any arguments:

ps

you'll see output from this command looks something like this:

Problem: I just issued a Linux find command, and got the following error message:

find: missing argument to -exec

Solution: Amazingly, it turns out that the computer is right, and I messed up my command. I entered my find command like this:

I just finished a Ruby client program that gets the current weather for multiple cities, using the Yahoo! Weather RSS Feed, and then mails the output to me. (Besides demonstrating this Ruby weather capability, it also shows how to send a simple mail message using the Ruby mail API.)

This page shows the contents of the Linux crontab man page that deals with the crontab file format (syntax), or as it says in the man page, "crontab - tables for driving cron". This crontab man page output was created on a CentOS Linux system.

You can see this same crontab man page output by entering this command on your own Linux system:

I was just digging around through a Java FTP program I wrote, and found the following class, which might be a nice reference for other people. This class lists all the possible FTP server return codes (status codes) that your Java FTP program can receive in return to a call to an FTP server.

Java exceptions FAQ: How do I create a custom exception in Java?

Java “custom exception” solution

The solution is to:

  1. Create a custom exception class in Java
  2. Throw the custom Java exception
  3. In other code, catch the custom exception, and
  4. Look at the output from our custom exception when we print a stack trace

I demonstrate this in the following example.

Linux crontab format FAQ: Do you have an example of a Unix/Linux crontab file format?

I have a hard time remembering the crontab file format, so I thought I’d share an example crontab file here today. The following file is the root crontab file from a CentOS Linux server I use in a test environment.

Spring JDBC FAQ: Can you provide an example of a SQL UPDATE query used with Spring JDBC, i.e., Spring Dao class and methods?

Here's some example Java code from a Spring Dao class where I use Spring JDBC and the JDBCTemplate update method to run a SQL UPDATE statement. In short, if you can get access to the Spring JDBCTemplate, this code shows a simple example of how to execute a SQL UPDATE statement.

Spring JDBC/Dao FAQ: Can you share some Spring JDBC examples, specifically SELECT query examples using Spring Dao objects?

[toc hidden:1]

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.