Posts in the “java” category

The Java ‘keytool’ command, keystore files, and certificates

[toc]

Java keytool/keystore FAQ: Can you share some Java keytool and keystore command examples?

Sure. As a little bit of background, in creating my "Hyde (Hide Your Mac Desktop)" software application, I decided to venture into the world of commercial software, selling my app for a whopping 99 cents. While that price is trivial, creating the “software licensing” code for this application was anything but trivial.

I finally decided to use a Java licensing tool named TrueLicense to assist with the software licensing, and TrueLicense quickly led me down the path of learning about the Java keytool and keystore path. So that’s what this article is about: How to use the Java keytool command to work with private and public keys, and work with intermediate certificate files.

Java enum examples/tutorial (an enum reference page)

[toc]

Java enum FAQ: Can you share some Java enum examples, such as how to declare a Java enum, and how to use a Java enum in a for loop, if/then statement, and Java switch statement?

Sure. As described in the Sun/Oracle Java documentation, “you should use enum types any time you need to represent a fixed set of constants.” Let's take a look at some enum examples to see how this works.

A Java deep clone (deep copy) example

[toc]

Back when I was interviewing for computer programming positions in Boulder and Louisville, Colorado, I found that many interviewers ask questions about Java serialization. After being asked about serialization for the third time, I remembered an old Java deep clone hack that takes advantage of serialization.

Java “file exists” testing

Java file FAQ: How can I test to see if a file or directory exists in Java?

Solution: Use the Java File.exists method. Here’s an example that shows the basic technique:

File tmpDir = new File("/var/tmp");   // create a File object
boolean exists = tmpDir.exists();     // call its 'exists' method

The exists method of the Java File class returns true if the file or directory exists, and false otherwise.

Java: How to list of all the available Java/Swing fonts

Java Fonts FAQ: How do I create a list of all the fonts available on the current platform?

Answer: To list all the fonts available to you in a Java application (a Java Swing application), I use the GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() method of the GraphicsEnvironment class, which technically returns an array of all the font family names it finds on the local system.

Java memory test - How to consume all the memory (RAM) on a computer

Here’s the source code for a Java “memory eating” program I wrote. Its purpose is to consume all of the memory (RAM) on a PC by allocating 1 MB byte arrays until it runs out of RAM:

import java.util.Vector;

public class MemoryEater
{
  public static void main(String[] args)
  {
    Vector v = new Vector();
    while (true)
    {
      byte b[] = new byte[1048576];
      v.add(b);
      Runtime rt = Runtime.getRuntime();
      System.out.println( "free memory: " + rt.freeMemory() );
    }
  }
}

It’s also important to run this Java memory eating program with the -Xmx option to make sure your JVM gets all the memory you want it to have:

$ java -Xmx1024M MemoryEater

The example shown above will try to consume up to 1,024 MB RAM (1 GB). As I write about in this How to control Java heap size (memory) allocation (xmx, xms) post, you can also specify the Xmx parameter in gigabytes, such as this setting for one gigabyte:

-Xmx1g or -Xmx1G

I wrote a program similar to this when I started getting the Windows blue screen of death several times. I figured there was some type of hardware failure going on, so I tried to consume all the RAM in the system and then read it back in to see if the hardware failure was related to the RAM.

Java: How to print elements in a List (without using a 'for' loop)

I was just reminded that if you need to print every element in a Java List, you can use the forEach method on the List:

// [1] create a List of strings.
java.util.List<String> listOfStrings = CollectionConverters.asJava(xs);

// [2] print the List of strings using forEach and System.out.println.
// note that there is no need for a 'for' loop.
listOfStrings.forEach(System.out::println);

I can confirm that as of August, 2021, this solution works just fine. So if you ever need to print every element in a Java List — without using a for loop — I hope this example is helpful.

A `printf` format reference page (cheat sheet)

[toc]

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing.

Java String array examples (with Java 5 for loop syntax)

[toc]

Java String array FAQ: Can you share some Java array examples, specifically some String array examples, as well as the new for loop syntax that was introduced back in Java 5?

Sure. In this tutorial, I’ll show how to declare, populate, and iterate through Java string arrays, including the newer for-loop syntax. Because creating a String array is just like creating and using any other Java object array, these examples also work as more generic object array examples.

A Java Robot class mouse and keyboard/keystroke example

Java Robot class FAQ: Can you show me an example of how to use the Java Robot class?

Answer: Um, yeah, sure ... I say that a little jokingly. Okay, what really happened is that while developing this Java Robot example code on my Mac, I had to reboot it about 10 times. When you use the Java Robot class, you're poking your head out into the native operating system, and if you mess up with your GUI events -- at least on a Mac OS X system -- a lot of bad things can happen.

A Java CRUD generator (and PHP, and Ruby, and ...)

Way back in the late 1990s, I wrote a Java CRUD generator, which was based on the work of someone else. It was a static code generator, but like future dynamic frameworks like Ruby on Rails, CakePHP, and others, it scanned the database and generated source code from the database table definitions.

Java: How to square a number

Java math FAQ: How do I square a number in Java?

You can square a number in Java in at least two different ways:

  1. Multiply the number by itself.
  2. Call the Math.pow function.

Square a number by multiplying it by itself

Here’s how to square a number by multiplying it by itself:

i = 2
int square = i * i

In that case, if you print the value of square, it will be 4.

Java ‘array of objects’ syntax examples

Java array FAQ: Can you share some examples of how to create arrays in Java (Java object arrays)?

While I generally work with lists and maps in Java, I occasionally need to create object arrays in Java. Since I don't use arrays that often, I thought I'd share some examples here so I can have a handy Java array syntax reference.

A simple Java String array

I work with the String class a lot, and here's how to create a String array in Java:

Java best practice: Return a List, not a LinkedList

As I started to mention in another blog post, your Java code will be more flexible when you learn to return more-general object references. In most cases other developers only need to see your interface, not your implementation. Put another way, does it matter to anyone else if you used a LinkedList or an ArrayList? If it doesn't matter, then return a List, or perhaps even a Collection.