Posts in the “java” category

CAFEBABE?

And in the category of “Strangest Things I Never Knew About Java,” I give you ... CAFEBABE.

JMX example: A Java JMX Swing application

Here is some sample Java source code for some JMX tests that I created a while ago. I started my first tests with an introductory JMX tutorial, then created this JMX example to see how JMX might work with a standalone application, in this case, a Java Swing GUI application. Let's look at the code.

First, here's the source code for a Java interface (an MBean interface) named HelloMBean:

How to put comments in your code with Javadoc

Introduction

One of the nice things about Java is javadoc. The javadoc utility lets you put your comments right next to your code, inside your ".java" source files. When you're satisfied with your code and comments, you simply run the javadoc command, and your HTML-style documentation is automatically created for you.

Jeyes, a Java version of Xeyes

Jeyes, a Java version of Xeyes

In my spare time back in 2011 I created a Java version of the old Unix/X-Windows “Xeyes” application. If you ever used Xeyes, you know it as a set of eyes that are displayed on-screen, and follow the mouse cursor as you move it around.

Now in 2019 I just brought it back to life, and here’s a 56-second video that shows how it works:

If for some reason that video doesn’t work, you can see it at this YouTube link.

The app currently only works on MacOS, but it can probably be ported to Linux and Windows systems by anyone motivated. The source code is pretty rough — I wrote it in less than two days back in 2011 — but if you want to build it or improve it, you can find the code here:

As you can see there, the build works with SBT/Assembly and the app-packaging process uses javapackager.

Feel free to fork it, clean it up, improve it, etc.

MacOS JAVA_HOME location (for Java JDK/SDK)

Java/Mac FAQ: Where is JAVA_HOME located on Mac OS X systems?

JDK location

This has changed over time, but if you're using Mac OS X 10.9 or newer, your JDK/SDK  JAVA_HOME location for Java 8 will be something like this:

/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home

For Java 7 it was also in the same area:

/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/

Of course that will vary by the JDK version you have installed.

Java comment examples

Question: How do I create a comment in Java?

Answer: There are two ways to create Java comments, and both are shown in the example code below:

// this is a one-line comment

/**
 * this is also
 * a comment, but it can span multiple
 * lines.
 */

The // syntax makes everything after it on the current line a Java comment, and the /** ... */ syntax lets you create multi-line Java comments.

A simple Java Generics example class

To take a break from a project I was working on yesterday I gave my brain a little exercise to see if I could remember how to create a Java class that used the Java 5 Generics magic. Since I’ve used Scala a lot recently, I decided to create a Java version of the Scala Tuple class.

The Tuple class simply stores two values, which are often key/value pairs. Beginning with the end in mind, here’s how you would typically use a Tuple in Java (if it existed):

A Java web service client that gets a list of objects

I just solved a problem with a Java web service client I've been working on. I've been trying to read a Java web service that was created with Apache Axis2, and it has methods that can return an array or List of User objects. I couldn't find any examples on the Axis2 web site that showed how to get an array or List from a web service client, but I finally find the solution by digging around a little.

In this post I'll provide some sample Java source code that shows what I did to solve this problem.