adding java and java-supported development tools to os x yosemite

I’ve been adding capabilities to my Mac Mini since powering it up the middle of this past week. After performing all the necessary OS updates, I installed Xcode 6.2.2. Xcode comes with a series of tools, one of which will be shown later in this article.The installation of Xcode is important to the clean installation of two Java-based tools later on in this post, Android Studio and IntelliJ IDE 14.

System Specifics

  • Mac mini Server (Late 2012)
  • Processor 2.3 GHz Intel Core i7
  • Memory 4GB
  • Two, 2 TB HDD

Preconditions

  • Mac OS X Yosemite Version 10.10.1 (update free from App Store)
  • Mac Server 4.0.3 (update free from App Store)
  • Xcode 6.2.2 (installed free from App Store)
  • Apple’s Java (Java 6) is not installed

Software Installed

Installation of Latest Java

Download the latest version from the Oracle Technology Network. You should be able to find the latest under New Downloads in the upper right corner of the web page. For this example I’m installing the 64-bit Java 8 update 31 (jdk-8u31-macosx-x64.dmg). After download start the installation by clicking or double clicking, depending on where you find the dmg file, to launch the installer.
Simply follow the directions and double click the icon (the transparent yellow square in the open box) to start the installation of Java. The first screen presented to you will be an introduction. There’s very little there, so click through.

When you click to install, it will ask you for your login account password, so type it in to continue.
The installation is a simple progress bar. When it reaches the end all the software will be installed on your system. The last dialog is the summary with a close button. Once installed you can add a JAVA_HOME variable if you like with an “export JAVA_HOME=$(/usr/libexec/java_home)” in your .profile, but I don’t believe it’s necessary. You certainly don’t need to edit $PATH, as there are links to java, javac, and jar in /usr/bin.

You can, if you also wish, download from the Oracle site the Java 8 demo examples. They’re all in a zip file. Once unzipped you’ll find all the demo and example applications that were once bundled with Java. Also included in this collection are example JavaFX applications which are well work looking at. JavaFX is so much better for UI development than the older and much worse looking Swing.

Android Studio and IntelliJ 14 IDE Pre-Installation Preparation

The installation for both is very similar, which they should be, considering that Android Studio is based on IntelliJ IDE. For both download their respective dmg files, double click them to open them in Finder, then drag them into the Application folder. Once in the Application folder you need to make a change to their respective plist files. Find the applications in the Application folder, and right click on them. Select Show Package Contents from the menu.

When the Contents folder appears, click through to the contents where info.plist is located. Double clock on info.plist.When Xcode opens up the plist (you did install Xcode, did you not?) open up JVMOptions, then select JVMVersion and clock on the string to edit it. The string, as shipped, is “1.6*”. Note the asterisk. If the string is left this way you will be prompted by both applications to install Apple’s much older Java 6. Changing the string to “1.6+”, with a “+” replacing “*”, tells the application to use the version of Java installed on the machine. Save this file and then double click the application to finish installation configuration.

For Android Studio I chose to download the separate Android SDK and install it (for my purposes) under $HOME/Java. The SDK comes as a zip file, so unzipping it under $HOME/Java produces $HOME/Java/android-sdk-macosx.

Installing Android Studio 1.0.1

With everything properly prepared, we’re ready to finish Android Studio’s installation (and IntelliJ if you’re so inclined). I’m only going to show Android Studio because it’s the more complex due to the Android SDK, but other than that they’re nearly identical.Make sure you open both top-level menu items and agree to their licensing.You’ll go through several minutes (or longer) of watching Android Studio download any Android SDK files it thinks it needs. This is just a typical capture of this process, and towards the end.We’re finally done!And ready to start development. The installation of IntelliJ is essentially the same but shorter. I’ll spare you the details.

Notes to The Gentle Reader

  • If you think this is overly complicated, it’s not. It’s no worse (and some might argue even simpler) than installation on Linux and Windows. The only obscure piece I had to go looking for was the plist adjustment. This information is missing on both Google’s Android Studio and Jetbrain’s IntelliJ IDEA websites. You’re welcome.
  • This is an initial install. I have no idea what will happen when I update Java, and I will update Java as each version is released. You update for the bug fixes. Failure to update any software is an invitation to grief, which is so easy to avoid. If any problems crop up when an update occurs then I’ll post an update about it here.

yet another c++ logging function

There comes a time in any programmer’s life when they write logging functions. Sometimes it’s little more than std::cout <<, or sometimes it’s a full bore implementation along the lines of Boost.Log. Sometimes you want more than a print statement without the overhead of a Boost.Log implementation. What I’m about to list is that kind of logging function. It comes in just two files, a header and implementation, with a simple test file to try it out.

First, the code listings.

#ifndef IT_LOGGER_H#define IT_LOGGER_H#include <iostream>namespace IT{class Logger {public:enum Level {INFO,   // Everything.ERROR,  // An error in the code itself.FATAL   // What is caused by a runtime exception.};// Set Logger to an output stream. Default is std::cout.//explicit Logger(std::ostream &ostr);// Set the stream we want to log to. Default is std::cout.//static std::ostream &setStream(std::ostream *ostr = 0);// Set the reporting level. Default is INFO.//static void setLevel(const IT::Logger::Level = INFO);// The logging function. This is wrapped in the LOG macro below.//static void log(const Level, const std::string fileName, const int line, const std::string msg);};}// Generate a logger entry. Macros are evil, but this is a necessary evil.//#define LOG(level, string) \IT::Logger::log((IT::Logger::level), __FILE__, __LINE__, (string));#endif

it_logger.h

#include <cctype>#include <cstdlib>#include <ctime>#include <fstream>#include <sstream>#include <strings.h>#include "it_logger.h"// All the code in this anonymous namespace block is executed once and only once// during application startup. This is a "silent" new for Logging to make sure// it is properly configured and sane.//namespace{// Check to see where logging output will go. Default is standard out.//// If the user defines the environmental variable ITLOGFILE, then that// will be the logging output. The file will be opened relative to the// home directory of the user running the application. For example, if// the user, using bash, performs 'export ITLOGFILE=foo.txt', then runs// the application, all logging will go to $HOME/foo.txt.//// Failures to open are silent, and the default on failure is standard out.//std::ostream* checkLoggingOutput(void) {std::ostream *logout = &std::cout;std::stringstream logfilename;char *home = getenv("HOME");char *env = getenv("ITLOGFILE");if ((home != NULL) && (env != NULL)) {logfilename << home << "/" << env;std::ofstream *ofs =new std::ofstream(logfilename.str().c_str(), std::ofstream::out | std::ofstream::app);if (ofs != NULL && ofs->is_open()) {logout = ofs;}}return logout;}std::ostream *outPtr = checkLoggingOutput();// Check what logging level to use. Default is IT::Logger::ERROR.//// If the user defines the environmental variable ITLOGLEVEL, then that// will be the logging output. Levels are INFO, ERROR, and FATAL.// Logging level is set, using bash as an example,// by 'export ITLOGLEVEL=INFO' (if you want INFO level logging or higher).// The three levels are case insensitive (info is the same as INFO, etc).//// Failures due to misspellings are silent. Default level is ERROR.//const IT::Logger::Level checkLoggingLevel(void) {IT::Logger::Level level = IT::Logger::ERROR;char *env = getenv("ITLOGLEVEL");if (env != NULL) {if (strcasecmp(env, "INFO") == 0) {level = IT::Logger::INFO;}else if (strcasecmp(env, "ERROR") == 0) {level = IT::Logger::ERROR;}else if (strcasecmp(env, "FATAL") == 0) {level = IT::Logger::FATAL;}}return level;}IT::Logger::Level levelFilter = checkLoggingLevel();std::ostream &setStream(std::ostream *ostr) {outPtr = ostr != NULL ? ostr : &std::cout;return *ostr;}static const char* lname[] = {"-  - INFO  : ","!!!! ERROR : ","**** FATAL : "};const char* levelToString(const IT::Logger::Level level) {return (level >= 0 && level < sizeof(lname)) ? lname[level] : "UNKNOWN: ";}}// end anonymous namespaceIT::Logger::Logger(std::ostream &ostr) {setStream(&ostr);}std::ostream &IT::Logger::setStream(std::ostream *ostr) {return setStream(ostr);}void IT::Logger::setLevel(const IT::Logger::Level level) {::levelFilter = level;}// A LOG macro is wrapped around this specific function call.// The level is one of the three IT::Logger::Levels defined in the header.// The filename is the source file name in which this was invoked via the macro.// The line number is the source file line number, i.e. where you// would expect to find when you open up the source file in a text editor.// The message is the explicit message written with the macro.// Logging is timestamped with the system's current local time.void IT::Logger::log(const Level level,const std::string filename,const int line,const std::string message) {if (level >= ::levelFilter) {char atime[80];time_t rawtime = time(NULL);tm *curtime = localtime(&rawtime);strftime(atime, sizeof(atime), "%c %Z : ", curtime);*outPtr << levelToString(level) << atime;*outPtr << filename << " : " << line << " : " << message << std::endl;}}

it_logger.cpp

#include "it_logger.h"int main(int argv, char *argc[]) {std::cout << "Starting..." << std::endl;LOG(INFO, "Info logging.");LOG(ERROR, "Error logging.");LOG(FATAL, "Fatal logging.");IT::Logger::setLevel(IT::Logger::ERROR);std::cout << std::endl;LOG(INFO, "Info logging 2.");LOG(ERROR, "Error logging 2.");LOG(FATAL, "Fatal logging 2.");return 0;}

logtest.cpp

Usage

Include the header file in the source you want to add logging; see logtest above for examples on how to use it in code. See the notes in the code for details about the environmental variables ITLOGLEVEL and ITLOGFILE for simple tuning and controlling output. Compile and then use. I’ve tested this on RHEL 5 and Ubuntu 14.10, both with g++.