Working with Java on OS-X: Tips and Tricks
Working with Java on OS-X Mavericks/Yosemite can have its pit falls…well working with Java on anything has pit falls, but OS-X has its own quirks. I’ve been working with Java for longer than I can believe – I suppose I remember the 1.0x days. I’ve been away from it for quite some time, using Python, PHP, JS, and things like that. Android brought me back in touch with the language and recently I’ve become involved in some open source work that uses Spring, J2EE, MySQL, and a whole bunch of J2EE-ish stuff. I’ve found it a bit of a struggle to get things working smoothly – onto my list of tips and tricks.
Note: I’m specifically talking about OS-X Mavericks and Yosemite…these tips are only tested on that edition of OS-X unless I specifically say otherwise below.
Java Tips Specific to OS-X
This is your friend – get to know and love homebrew.
- Brew with care
Brew is great, but some packages I still prefer to control myself. Specifically installing maven and tomcat manually I prefer. No real reason…just I’m a control freak I guess. Update 11/07/2015: I used brew to install Maven 3 today and it went just fine – brew install maven.
El Capitan Note: SIP makes changes that you will need to correct. /usr/local needs to exist and brew needs to write to it. Prior to installing El Capitan I made sure it existed and then I made it writable with this command:
sudo chown -R $(whoami):admin /usr/local
For more details: Homebrew El Capitan Update.
- Sick of “cd” hell??
You can drag and drop folders into the terminal window to get to a location without “cd’ing” yourself crazy. Just type “cd” and space and then drag the folder. Bingo!
- Setting Environment Variables
There’s a ton of advice out there. Ignore it. To create environment variables, create a .profile file in your home directory and then add your stuff there. You can set JAVA_HOME, CATALINA stuff, mvn stuff, etc. You can use Text Wrangler or whatever you like (Sublime) to edit the file. File, Shift, Open…to see the hidden files….oh yeah…here’s my current .profile
export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=512m"
export CATALINA_OPTS="-Xms1024m -Xmx2048m -XX:MaxPermSize=1024m"
I got some pretty big memory limits set there 🙂
- Edit Hidden Files
In your editor hold down Shift while selecting File –> Open. Awesome! Fire away VI and emacs junkies…
- Copy to the Clipboard from a terminal window
Its handy to get stuff placed on the OSX clipboard from a terminal window. For example your ssh public key for ssh connections. To do this use this command:
cat <the_filename.ext> | pbcopy
- Apple Java vs. Oracle/Open JDK Java
Apple installs its own Java on their machines and it can be a bit of a mystery about how to best replace this with a generic JDK/JRE or one from Oracle. Oracle’s installer is better than Apple’s, but I recommend brew for installing Java – see the tips below. Whatever method you choose, after that’s done add the following line to your .profile file. Don’t forget to close and reopen any terminal windows (or source it) for this to take effect – and yes the back ticks are important:
Java Tips in General
- PermGen and Memory Options
A classic – when you run out of memory, up the amounts with this option usually set as an environment variable or passed on the command line. 11/07/2015: PremGen is getting deprecated in the latest revs of java so this won’t be so important going forward.
"-Xms1024m -Xmx2048m -XX:MaxPermSize=1024m"
- Maven Profile
Its usually good to have a separate env variable file for maven options. Simply drop a .mavenrc file in your home directory.
- Open JDK vs. Oracle JDK
While I like and regularly use Open JDK, I find it helpful to have both around in case I see something strange that I cannot explain. A quick test in the other JDK is a helpful sanity check when you are getting mysterious errors.
- Consider a Brew JDK install
The installers from Apple and Oracle are flaky. I recommend a brew based installation using these commands:
brew tap caskroom/cask
brew install brew-cask
brew cask install java
If you've had brew installed for awhile you might get an error about moving your Caskroom. If you get that here's a great post to fix it. To update the JDK to this: brew update brew cask reinstall java
- To check what Java is installed and managed by Cask use:
brew cask info java
- Get Your Names Straight
By default OSX leaves some things unset and this can freak out Java and other stuff as well. Use scutils to set your names to something. I recommend setting them all to the same value:
scutil --set ComputerName myComputerName
scutil --set LocalHostName myComputerName
scutil --set HostName myComputerName
Keep in mind that these are used by many local IP based products so you may need to update config files on your system. For example you’ll need to update a local WordPress config if your db connection info is pointing to localhost.
- Lint & Find Bugs
Regularly scanning my own code is very helpful for keeping myself disciplined and on the right path with various conventions. Even simple things like “.equals” instead of “==” for string comparisons.
I’ll keep adding to this list as I re-discover things.