Posts in the “java” category

How to control Java heap size (memory) allocation (xmx, xms)

Java memory FAQ: How do I control the amount of memory my Java program uses (i.e., Java RAM usage)?

One of the Linux servers that I use is a little starved for memory, but I need to run a Java program on it periodically to run some utility tasks. However, every time I try to run the program I get this Java heap size error message:

[toc hidden:1]

A Java MySQL SELECT example

Summary: This is a Java/MySQL SQL SELECT example, demonstrating how to issue a SQL SELECT command from your Java source code, using a MySQL database.

Creating and populating a Java ArrayList (Java 9 and newer)

If you want to create and populate a Java ArrayList with Java 9, 11, and newer, you can use this syntax:

List<> ints = ArrayList<Integer>(List.of(1,2,3));

As shown, this uses the usual ArrayList constructor and the Java List.of method. Once you have an ArrayList like this you can continue to add new elements to it as usual:


A Java HTTPS client example

Java HTTPS client FAQ: Can you share some source code for a Java HTTPS client application?

Sure, here's the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL. I actually found some of this in a newsgroup a while ago, but I can't find the source today to give them credit, so my apologies for that.

A Java Model View Controller example (Part 3)

<< Back to Part 2 of our Java MVC Example

MVC - Handling data-changing events

Whenever the user adds, edits, or deletes data, such as our Process data, all the proper GUI components need to be notified of this event so they can properly update their displays. In this application, when a user adds a new Process, this means that we need to update our MainProcessTable. We implement this as follows:

How to create and throw a custom exception in Java


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

Here's a quick example of how to create and throw a custom exception class in Java. In this tutorial I'll demonstrate how to:

  • Create a custom exception class in Java
  • Throw our custom Java exception
  • Catch our custom exception, and
  • Look at the output from our custom exception when we print a stack trace

JavaFX TextArea: How to insert text at the cursor/caret position

JavaFX TextArea FAQ: How do I insert a string/text into a TextArea at the cursor/caret position?

Solution: First get the position of the cursor/caret in the TextArea, then insert your new text at that position:

int caretPosition = notesTextArea.getCaretPosition;
notesTextArea.insertText(caretPosition, "My new text");

JavaFX: How to set minimum dialog width/size

JavaFX tip: To set the minimum width of a dialog, such as a TextInputDialog, use the setMinWidth method on the dialog’s dialog pane, like this:

val d = new TextInputDialog("")
d.setTitle("Open URL")
d.setHeaderText("Open a URL")
d.getDialogPane.setMinWidth(500)  <-- solution

Java String replaceAll: Why isn’t my “replace” method working?

Java String replace FAQ: Why isn't my Java String replace / replaceAll / replaceFirst code working?

The Java String class has several methods that usually make it really easy to replace one text pattern with another. I say "usually" because what occassionally happens is that I forget that a Java String is immutable (it can't be changed), so I accidentally try some String replace, replaceAll, or replaceFirst code that looks like this:

How to use multiple regex patterns with replaceAll (Java String class)


Java FAQ: How can I use multiple regular expression patterns with the replaceAll method in the Java String class?

Here’s a little example that shows how to replace many regular expression (regex) patterns with one replacement string in Scala and Java. I’ll show all of this code in Scala’s interactive interpreter environment, but in this case Scala is very similar to Java, so the initial solution can easily be converted to Java.

Java: How to append text to a file with the FileWriter class

Java file writing FAQ: How do I append text to the end of a text file in Java?

The short answer is that you should create a FileWriter instance with the append flag set to true, like this:

BufferedWriter bw = new BufferedWriter(new FileWriter("checkbook.dat", true));

The rest of this article explains this.

Java sound example: How to play a sound file in Java

My Java sound/audio example: I'm working on a simple "meditation" application that plays a sound after a certain period of time (the sound of a gong), so I thought I'd share some source code out here that demonstrates how to play a sound file in a Java application like this.

(Note: I initially found this technique described at, but the code below is taken from my project.)

In this case the sound file I'm going to play is an "au" file, but I believe this same technique works with most other sound file types as well.

Java sound: A command line Java program to play a sound file (in headless mode)

As part of my ongoing HAL 9000 voice obsession, I downloaded a bunch of "HAL 9000" sound files last night. But, when you double-click a sound file on Mac OS X, it automatically plays through iTunes, which is good for some things, but bad for what I wanted to do. So, I wrote a quick little "Java sound" program to read the name of a sound file from the command line, and then play the sound file.