Category Archives: MacOS

Running JAVA class from Mac OS CLI

I wrote a utility in Java that I want to eventually run as a cron job, which means I need to be able to specify the path that the code uses for its files.

There is a command line parameter that is passed to the java command that can be used to specify a particular directory, and then my java code can use that as the base path.

My command line to execute the java code is:

java -cp /Users/cwg/workspace/getInfo/bin -Duser.dir=/Users/cwg/workspace/getInfo com.cwgtech.getInfo

This will call the java code from any path, and the -Duser.dir sets the user variable to point to the path my data is stored in.

In the java code, I access the passed variable with the following code:

workingPath = System.getProperty("user.dir") + "/";

Then when I want to access a file or folder, I just pre-pend it with the workingPath String.

Node xml = loadXML(workingPath + XMLFILE);

Note: Don’t use ~ in the path for the java command, use the fully qualified path!

Also, I ended up using a couple of external Jars in my code, so I had to modify the -cp option to include the path to my lib folder as follows:

-cp /Users/cwg/workspace/getInfo/bin:/Users/cwg/workspace/getInfo/libs/*

The * at the end of the second path says to use all the jar (or zip) files in the specified folder when looking for classes.  The : is the separator if you need more than one path in -classpath (can be shortened to -cp)

Saving a PSD file with a .PNG extension 😫

I’ve been working on an update to one of our apps for iOS. Unfortunately as I was updating the launch screens, I inadvertently saved a version for the iPhone 5 as a PSD file, but using the .PNG extension.   I did this by clicking on the old PNG file in the file selector to make sure it was named correctly, but them omitted to clicking on the Save Type drop down list and selecting PNG.

Now, the Mac figured it out, and would display the image in Finder and XCode was happy to load and preview it.  Even the iOS emulator didn’t mind using a PSD file in this way.  However, the actual iPhone was less than happy.

I wasted 3 hours trying to figure out the problem before I spotted my mistake. Surely something on the Mac side should have flagged the file as having a problem – the iPhone can’t load PSD files, why the hell should the emulator!

Yes, I know I saved the file incorrectly, but if it was incorrect, then it should have spewed out some kind of error.  Hopefully I’ll not make this mistake again!

Android ScreenCap with ADB

I found a great blog post on how to capture a screenshot from a connected device using ADB – I know I can use eclipse or android studio to do it, but I wanted a quick grab from the command line, and I didn’t want to be bothered with capping it to the local disk of the android device.

Anyway, here’s the one liner that works on MacOS:

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

Cron on MacOS

I wanted to add a Cron job to my Mac – I just needed to poll a PHP script on a server once a day, so pretty simple.

The trick is using crontab with nano as the editor! In order to pull that off (I hate VI), I used the following command:

env EDITOR=nano crontab -e

Then it was just some pretty simple use of CURL to fetch from the URL of the PHP script:

25 20 * * * curl -s http://xxx.xxxxxxx.com/refreshTimeStamps.php > /dev/null

So, at 8:25pm every night, curl will pull from that URL, dump the output to null and not send me a mail – easy!