Glimmer

Faint insights into (mostly) the worlds of automation and productivity

Finding Java and NetBeans

| Comments

Working with NetBeans 9 with an OpenJDK 10 install on a MacOS Mojave machine.

I teach a course EE333 where we use Java as the basis for our efforts in learning object-oriented design and programming. We had to migrate to NetBeans 9 as NetBeans 8.2 became unworkable in some environments and unsupported. We also went to Java 10. First with Oracle although on November 16, 2018 they started wanting to uninstall it and have users move to Java 11 which I gather is going to have long-term support.

Both the Oracle code and the OpenJDK code for MacOS have a new (to me at least) structure where it has a more Mac-like file structure with a “Contents” folder in between the JDK-name and the “Home” directory. There is also a MacOS folder and an info.plist.

/Library/Java Directory with JDK 10 on MacOS

This means that the JAVA_HOME should be set a bit differently (to /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home) in the ~/.bash_profile configuration file.

There were several errors in the Ant-based building in NetBeans 9 which essentially were stating that it was not possible to find the java executable and that the RT (runtime) version would be used instead.

In the build.xml file (on lines 120, 121, 127, 362, and 378 on the file I had), there was a path error that may either be just an error or more likely something that is not built correctly for the later JDK versions. This removed a bunch of error messages.

An example (120, 121) of this in diff form (leading spaces removed) is

1
2
3
4
-  <available file="${java.home}${file.separator}${file.separator}bin${file.separator}java"/>
-  <available file="${java.home}${file.separator}${file.separator}bin${file.separator}java.exe"/>
+  <available file="${java.home}${file.separator}..${file.separator}bin${file.separator}java"/>
+  <available file="${java.home}${file.separator}..${file.separator}bin${file.separator}java.exe"/>

But then, it looks like on restart, NetBeans did not like it and in nbproject/UPDATED.TXT it says

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
============================================
Project ScheduleBuilder build script updated
============================================

Project build script file jfx-impl.xml in nbproject sub-directory has not been recognized
as compliant with this version of NetBeans JavaFX support module. To ensure correct
and complete functionality within this NetBeans installation the script file has been
backed up to jfx-impl_backup.xml and then updated to the currently supported state.

FX Project build script auto-update may be triggered on project open either after
NetBeans installation update or by manual changes in jfx-impl.xml. Please note that
changing jfx-impl.xml manually is not recommended. Any build customization code should
be placed only in build.xml in project root directory.

Remark: The auto-update mechanism can be disabled by setting property
javafx.disable.autoupdate=true
Automatic opening of this notification when project files are updated can be disabled by setting property
javafx.disable.autoupdate.notification=true
(in build.properties, private.properties or project.properties).

Remark: Files nbproject/jfx-impl_backup*.xml and this file nbproject/UPDATED.TXT
are not used when building the project and can be freely deleted.

which essentially undid the changes. Good news is that the error messages are not (yet) back. I removed the two files noted as removable.

I will try to remember to check the NetBeans 10vc3 to see if the issue still exists.

In the JDK11, JavaFX will be removed from the JDK and become a standalone package to maintain and install. Both NetBeans and Java are on multi-times a year release cycle making it challenging to keep up with things for a class baseline. Hopefully, a stability point is soon. I know many folks are working towards this goal.

Success – Hard Work And-or Luck?

| Comments

In his article Absolute Success is Luck. Relative Success is Hard Work., James Clear provides a pair of stories about how luck (including the circumstances one is born into) and hard work both play a role in success. He concludes that luck controls absolute success and hard work controls relative success.

Later in the article, he has an engineeering/science way of conveying his idea:

Imagine you can map success on a graph. Success is measured on the Y-axis. Time is measured on the X-axis. And when you are born, the ball you pluck out of Buffett’s Ovarian Lottery determines the y-intercept. Those who are born lucky start higher on the graph. Those who are born into tougher circumstances start lower.

Here’s the key: You can only control the slope of your success, not your initial position.

There are more gems as well as several good stories in the article and I highly recommend you read it.

He closes noting that one can not control luck but can control the hard work input. I would qualify this to note that it has to be correctly focused hard work.

Finding Reminders in Evernote

| Comments

Here is a hint that I was not able to find quickly in Google nor in the EverNote help.

Enter “Reminders” into the Search Bar and then select “Notes with Reminders” to find all notes that have reminders. Very logical but took a few minutes and hopefully this short post will same someone else a few.

NetBeans Template Configurer

| Comments

Many years ago, I wrote a post about how to use the NetBeans template system for my students. It quickly covered the basics and demonstrated how to set things up for the course’s documentation standard.

In Fall 2017, one of the UAB EE333 Project teams built a clever tool to do this initialization for the student. This makes it much easier for the new student to set up the automation at the beginning before they have any real experience with NetBeans. The tool solicits the data needed (student name, email, course) and installs the templates.

It has been tested on both Windows 10 and Mac OS 10.12 with NetBeans 8.2 and attempts to work with earlier and later versions of things but that is mostly outside the testing space. The team consisting of

  • Dylan Dalton
  • Schauber Gbalou
  • Matthew Manuel
  • Joey Richardson
  • Darrin Wang

have agreed to having the source code of their tool on GitHub under a MIT license. UAB students should be able to find a compiled version of the code in the form of a templates.jar file in their course Learning Management System.

The students cleverly put the basic templates (modified versions of the ones in NetBeans) in the jar file as resources allowing one to modify the templates as desired but only have to distribute a jar file to new students. We placed the code into Github.

Java Sound Update 2

| Comments

This is an update to a prior article.

UAB EE Students Andrew Peturis, Chaselyn Langley and other team members produced an interesting GUI Application for children to associate sounds with images. In the course of doing this, they wished to expand the control of the audio beyond my original example. They discovered a bit of behavior that was not (at least clearly) documented in the JAVA API.

They modified the original demo substantially for their application and supplied a limited version of it to me as an update to benefit future classes (as well as the broader Internete Community). I merged their two classes into one focused only on the sound system behaviour and hopefully preserved the important behaviors they wished to document. The new code is show below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * File: Sound.java
 * Authors: Andrew Peturis, Chaselyn Langley, David Green
 * Vers: 1.1.0 12/10/2015 dgg - update example to standalone
 * Vers: 1.0.0 10/20/2015 agp - initial coding
 *
 * Credits:  Team SoundBox - EE333 Fall 2015
 */

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Sound {

    private String sound;
    private Clip clip;
    private boolean isPlaying;  // Sound loaded up and has started playing (may be paused)


    // Don't allow default constructor
    private Sound() {
    }

    public Sound(String sound) {
        this.sound = sound;
        isPlaying = false;
    }

    // Play a sound using javax.sound and Clip interface
    public void play() {
        try {
            if (!isPlaying) {
                // Create a clip to hold a sound
                clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(new File(sound)));
                isPlaying = true;
            }
            // Play the audio clip with the audioplayer class
            clip.start();
        } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e ) {
            System.out.println("Things did not go well");
            System.exit(-1);
        }
    }

    // Continue a stopped sound
    public void resume() {
        if (isPlaying) {
            clip.start();
        }
    }

    // Pause a playing sound
    public void pause() {
        // clip.stop() will only pause the sound and still leave the sound in the line
        // waiting to be continued. It does not actually clear the line so a new action could be performed.
        if (isPlaying) {
            clip.stop();
        }
    }

    // Stop a sound from playing and clear out the line to play another sound if need be.
    public void end() {
        if (isPlaying) {
            pause();
            // clip.close(); will clear out the line and allow a new sound to play. clip.flush() was not 
            // used because it can only flush out a line of data already performed.
            clip.close();
            isPlaying = false;
        }
    }

    // Small demo program
    public static void main(String[] args) {
        Sound sound = new Sound("predator.wav");
        try {
            sound.play();
            System.out.println("Playing for 1 second");
            Thread.sleep(1000);     // let it play a second
            sound.pause();          // pause
            System.out.println("Pausing for 5 seconds");
            Thread.sleep(5000);     // continue playing
            System.out.println("Playing for 1 second");
            sound.resume();
            Thread.sleep(1000);      // play have a second
            System.out.println("Ending, resuming which should silently fail");
            sound.end();            // End sound
            sound.resume();         // Should be silent
            Thread.sleep(5000);     // Wait 5 seconds
            System.out.println("Playing again for 5 seconds");
            sound.play();           // Should start again
            Thread.sleep(5000);
            sound.end();
        } catch( InterruptedException e) {
            // just fall to end
            // System.exit(0);
        }
    }
}

Creating a Line Segment Graphic

| Comments

The following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * File: CreateSimplePlot.java
 * Author: David Green <dgreen@uab.edu>
 * Vers: 1.1.0 09/21/2015 dgg - change from Etch-A-Sketch to Plotter
 * Vers: 1.0.0 09/10/2014 dgg - initial coding
 *
 * Credits:  (if any for sections of code)
 */

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * Code from:  http://www.java2s.com/Code/Java/2D-Graphics-GUI/DrawanImageandsavetopng.htm
 * (modifications by ...)
 * @author David Green <dgreen@uab.edu>
 */
public class CreateSimplePlot {

  static public void main(String args[]) throws Exception {

    double t;       // time
    double oldT;    // old time
    double deltaT;  // time increment
    double y;       // plot value
    double oldY;    // old plot value

    try {
      int width = 700, height = 500;

      // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
      // into integer pixels
      BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

      Graphics2D ig2 = bi.createGraphics();

      // make full image white
      ig2.fillRect(0, 0, 700, 500);

      // draw black lines
      ig2.setPaint(Color.BLACK);

      oldT = 0.;
      oldY = plotFunction(oldT);
      deltaT = 5.;


      for (t = deltaT; t < 700.; t += deltaT ) {
        y = 250.0 + 200.0 * Math.sin( (t / 200.) * 2. * Math.PI );
        ig2.drawLine((int) oldT, (int) oldY, (int) t, (int) y);
        oldY = y;
        oldT = t;
      }

      // write the resulting file
      ImageIO.write(bi, "PNG", new File("plot.png"));

    } catch (IOException ie) {
      System.out.println("Exception raised " + ie);
    }
  }

  private static double plotFunction(double t) {
    return 250.0 + 200.0 * Math.sin( (t / 200.) * 2. * Math.PI );
  }
}

produces

Figure 1. Sinusoid example plot

as a PNG graphic file.

Software Programming Skills Popularity

| Comments

There are lots of ways to evaluate popularity, and in the end, all are probably very transitory. In his Quartz article, Max Nisen examined American job data and reports the top ten slanguage/frameworks (by salary offers) are

  1. Ruby on Rails
  2. Objective C
  3. Python
  4. Java
  5. C++
  6. JavaScript
  7. C
  8. R
  9. C#
  10. Visual Basic

Java Sound Update

| Comments

For years, I have been using (and advising the use of sun.audio) for playing sounds in the Java VM using Alvin Alexander’s article for inspiration. The downside of this approach, of course, is the dependency on classes that are not part of the JAVA API and therefore might change at anytime although they have been relatively stable for many years.

Recently, I found a stackoverflow discussion noting the existence of AudioSystem (and related clases).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * File: ModernRingPhone.java
 * Author: David Green <dgreen@uab.edu>
 * Assignment:  SoundOff - EE333 Fall 2014
 * Vers: 1.0.0 10/23/2014 dgg - initial coding
 *
 * Credits:  Concepts from http://stackoverflow.com/questions/15475276/short-sound-file-plays-in-netbeans-but-not-in-jar
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

/**
 *
 * @author David Green <dgreen@uab.edu>
 */
public class ModernRingPhone {

  public static void main(String[] args)
  {
    try {
        // open the sound file as a Java input stream
        String gongFile = "telephone-ring-3.wav";

        // create an audiostream from the inputstream
        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(new File(gongFile)));

        // play the audio clip with the audioplayer class
        clip.start();

        // wait for sound to finish before terminating program (necessary for 
        // running inside NetBeans, at least)
        Thread.sleep(10000);
    }
    catch ( Exception e) {
        System.out.println("Things did not go well" );
        e.printStackTrace();
        System.exit(-1);
    }
  }
}

Update: 2015-02-08 Removed unnecessary line of code

Update: 2015-12-10 Revised example is available here.

Updating Java/NetBeans on OS X

| Comments

Oracle Java on OS X

After moving to Oracle’s Java to use on OS X, one finds a new System Preference pane which launches a Java preferences App. Included here is a preset option to update Java. However, one should note that if one installed the JDK (Java Development Kit) as opposed to the JRE (Java Run Time environment) only the JRE portion of the package is automatically updated. It appears one has to manually download and update the JDK to get that portion of the package updated. Investigating a bit, it looks like the attention is on the JRE and only the linkages for the Applet usage are updated automatically. The JDK and its command line version of java are not updated in addition to all the other components of the JDK. This is probably not all bad as it means the development environment does not change on you without notice.

NetBeans on OS X

To update NetBeans, download a later version, start the new version and agree to migration from the earlier release. Then, when you are ready to eliminate the old version of NetBeans, go to the /Applications/NetBeans directory and delete only that old version.

Update: 25 Jan 2014 — The template directory is made when the first template is created. On the Mac, it is located at ~/Library/Application\ Support/NetBeans/7.4/config/Templates and has a Classes directory and a Properties directory. It appears that one can move the templates from one config directory to a new one as part of migrating.