Showing posts with label OSX. Show all posts
Showing posts with label OSX. Show all posts

Monday 7 March 2016

Show line numbers in vi editor on Mac OS X

Guys in vi editor in order to show the line numbers, do the following :

Open vi editor to create or open a script
Click escape (esc) key and type colon.
Now enter set number and Enter

Check out the screenshot for reference :

Cheers!

Sunday 6 March 2016

Get Mac OS X System Information from Terminal

Hii! Guys lets come back today on another topic related to Apple Mac OS X. Correct me if am wrong, the first thing you would after purchasing your new Macbook or iMac is double-check the system information right?

Typically you would click the Apple logo on top left -> About this Mac -> System Report. Well could you get the same information from command line? Yes you can!

Open terminal and just type the following :

system_profiler

It will list the system information in a matter of few seconds. You could also filter the command to get only specific information by piping to a grep command.

For eg :

system_profiler | grep Memory

system_profiler | grep Processor

system_profiler | grep CPU

Wednesday 2 March 2016

How to open Terminal from within Eclipse on Mac OS X

In Eclipse, from the Help menu -> Eclipse Marketplace, lookup for Terminal. Install the plugin TM Terminal that you'll come across as below:


Once installed, simply mention the shell interpreter location in the Preferences for e.g. /bin/bash and also choose the initial working directory as either your User Home or Eclipse Workspace.


Now go to Window -> Show View and choose Terminal to show it within Eclipse and click the following blue button to open a new session.


This way you can run shell scripts from within Eclipse itself. Cheers!

Tuesday 1 March 2016

Call shell script from Apache Ant build on Mac OS X

Guys, you might come across some situations where you need to invoke shell and execute a bash script while building a project using Apache Ant. In today's example let's see how to run a shell script from within an Ant build.

For illustration, lets create a simple bash script to list the disk usage of our system. Let's name this file as "DiskUsage.sh"

#!/bin/bash

# Sample shell script to be invoked from an Ant build

echo "Disk usage is as follows : \n"
echo "=========================== \n"


df -h

Now let's create a simple build-test.xml using the exec command to invoke the shell as follows :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="call-shell" basedir=".">

<target name="call-shell">
<exec executable="/bin/bash">
<arg value="DiskUsage.sh"/>
</exec>
</target>


</project>

Run the ant build from command line as ant -f build-test.xml. Note that the shell script and build.xml happen to be at the same location/hierarchy within the project. If your shell script lies elsewhere, you may perhaps want to refer to its path in the build.properties instead.

You could also invoke the "expect" scripting prompt in a similar fashion to execute an expect script that we discussed in our earlier post.

More information on the exec can be read at the official Ant manual.

Sunday 28 February 2016

View Jar contents without opening on Mac OS X

Hello readers, today's topic is a fairly basic interview question wherein you are asked to view the jar package contents without exploding/opening it.

In order to do so, type the following in terminal for example :

unzip -l 28-02-2016-TestJar-1.8.jar 

Output : 

Archive:  28-02-2016-TestJar-1.8.jar
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  02-28-16 13:34   META-INF/
      142  02-28-16 13:33   META-INF/MANIFEST.MF
        0  02-28-16 13:33   com/
        0  02-28-16 13:33   com/ironcladzone/
      571  02-28-16 13:33   com/ironcladzone/Create_Directory.class
      755  02-28-16 13:33   com/ironcladzone/FileSize.class
      367  02-28-16 13:33   com/ironcladzone/HelloICZ.class
      463  02-28-16 13:33   com/ironcladzone/String_mani1.class
      534  02-28-16 13:33   com/ironcladzone/String_manip2.class
      601  02-28-16 13:33   com/ironcladzone/String_manip3.class
      407  02-28-16 13:33   com/ironcladzone/String_manip4.class
      474  02-28-16 13:33   com/ironcladzone/String_manip5.class
      935  02-28-16 13:33   com/ironcladzone/User_Input.class
      201  02-28-16 13:33   com/ironcladzone/callMacApp.class
      909  02-28-16 13:33   com/ironcladzone/operator1.class
 --------                   -------
     6359                   15 files

Note the -l switch is for listing the jar contents. You can use the same technique for any kind of package - jar / war / tar / ear etc.

Saturday 27 February 2016

Automate tasks using Expect scripts on Mac OS X

Expect is one of the hidden gems and quite esoteric scripting language for automating tasks on Mac OS X and on most unix flavors.

The idea behind expect is you can program the response to a programmed request string. It's much like a pre-programmed auto-complete feature, in which you have already told the system what the response should be, if it comes across a specific expected request.

Let's try out a basic simple script. In terminal create a new script file : vi expect_test.exp
Note the .exp extension, similar to .sh of bash scripts.

#!/usr/bin/expect

expect "hello"

send "Welcome to IroncladZone \n"

Now if you execute the script ./expect_test.exp and type the string "hello", it will automatically send the response "Welcome to IroncladZone".

Saturday 20 February 2016

Ant - Build.xml example on Mac OS X

In today's post guys, we'll cover the topic of building a very simple Java based project using Apache Ant. We'll learn how to write a basic build.xml file for building it.  Let's first look into the code for build.xml and then we'll breakdown the code with the explanation.

build.xml :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="create-jar" basedir=".">

<property name="build.home" value="${basedir}" />

<target name="clean">
<delete dir="${build.home}/src" />
<delete dir="${build.home}/bin" />
<delete dir="${build.home}/lib" />
<delete dir="${build.home}/test" />
<delete dir="${build.home}/dist" />
</target>

<target name="createdir" depends="clean">
<mkdir dir="${build.home}/src" />
<mkdir dir="${build.home}/bin" />
<mkdir dir="${build.home}/lib" />
<mkdir dir="${build.home}/test" />
<mkdir dir="${build.home}/dist" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
</path>

<target name="copydir" depends="createdir">
<copydir src="/Users/ironcladzone/Documents/workspace/AntBuildTest/src"
dest="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"> </copydir>
</target>

<target name="compile" depends="copydir">
<javac classpathref="classpath" includeantruntime="false"
srcdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"
destdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/bin"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/TestJar.jar"
includes="**/*.class"></jar>
</target>


</project>

Now let's look into the folder structure in the Eclipse Package Explorer :


Now, let's get down to the explanation guys :

1. First of all, AntBuildTest is my project workspace which has the source code in it.

2. Next I created a build directory under which I created the above build.xml file.

3. Now in the builds.xml I have defined various steps/tasks also known as targets viz : clean, createdir, copydir, compile, create-jar. Note that a build.xml will still successfully build even if it doesn't have a single target defined.

4. For every build.xml file, we first have to define the <project></project> tag, in which we mention the project name, basedir and default target to be executed.

5. Basedir is the location of the current root directory in which build.xml exists i.e the absolute path

6. The default target that we define, will be the task/target which will be executed. Now the target can have dependencies on other targets.

7. Note that target can have one or many dependencies on multiple targets.

8. The 'clean' target is the target to delete all sub-directories that we create in the 'createdir' target. So every time we perform a clean build, the directories created in the last build will be deleted.

9. In the 'createdir' target, we make directories namely : src, bin, lib, test, dist.

10. We now want to copy the source code from our local workspace to the 'src 'directory that we create in the 'createdir' target,. So in the 'copydir' target we define the step to copy the source code.

11. Now once we have the source code, we need to compile it right. So we define the 'compile' target wherein we define 'srcdir' i.e the location where we have our java files. Also we mention the 'destdir' as the bin folder i.e this is where we want our .class files to be saved.

12. During compilation, we need to specifically tell the xml where the .class files would be. So we need to define the classpath. 

13. We define the classpath i.e the bin directory which will have the .class files.

<pathelement location="${build.home}/bin"></pathelement>

14. After compilation, we define the 'create-jar' target, in which we create the jar package - TestJar.jar. Make sure not to miss the .jar file extension. Note that we have mentioned the include parameter for including the **/*.class files that need to be included in it.

15. We have also defined the 'destfile' parameter i.e the location where we want the jar file to be saved i.e the 'dist' directory for distribution.

16. Oh and I missed the 'lib' directory, which will basically hold the external jar files required for our project.

17. Well, I hope I covered the simple basic fundamental points surrounding an Ant build.  Let me know if I missed any point guys. If I did, I'll try to cover them in detail in an another upcoming post covering an advanced scenario. Till then, ciao!

Tuesday 9 February 2016

How to setup Apache Ant on Mac OS X

Folks, presenting a quick tutorial on how to setup the build tool Apache Ant on a Mac OS X machine.

Download the relevant ant archive version from Apache Ant homepage. For this example we download the latest v1.9.6.
Store it locally at any location you like; maybe like /Users/Username/apache-ant-1.9.6

Now lets edit the .bash_profile to define the ANT_HOME environment variable.

sudo vim ~/.bash_profile

Add the following lines to it (make sure to replace with your own username in the path):

export ANT_HOME=/Users/IroncladZone/apache-ant-1.9.6/
export PATH=$PATH:$ANT_HOME/bin

Now source ~/.bash_profile and restart terminal.

Check the above settings by looking up the ant version in terminal :

ant -v
Apache Ant(TM) version 1.9.6 compiled on June 29 2015
Trying the default build file: build.xml
Buildfile: build.xml does not exist!
Build failed

This means your ant is ready to use for builds. Going forward all you have to do is pass the build.xml to trigger the build.

Folks, watch out for this space for more basic to intermediate to advanced tutorials coming up soon.

Monday 8 February 2016

How to Set JAVA_HOME environment variable in Mac OS X

Check out a quick reference guide to set the JAVA_HOME environment variable in Mac OS X.

Open terminal and type the following :

  • sudo vim ~/.bash_profile
  • Enter your password
  • Now in the vim editor, insert the following text :
export JAVA_HOME=(/usr/libexec/java_home)
  • Save the .bash_profile from the vim editor using :wq
  • Now type the following in terminal :
source ~/.bash_profile

Confirm your changes by typing the following in terminal :

echo $JAVA_HOME

You should see the value/path of JAVA_HOME environment variable you just set as above.



Transfer files between two servers using SCP

We can transfer files between 2 remote hosts using the scp command as follows :

scp Username@Server1:/path-to-file-to-be-copied/ Username@Server2:/destination-path-where-files-are-to-be-copied/

Note that you may have to enter the credentials manually after this to logon to both the hosts.

Eg :

scp user123@Unix_Host_1:/Users/user123/Documents/Textfile.txt user123@Unix_Host_2:/users/user123/Documents/

You may also use the -3 switch to transfer files from one machine to another through your local host.

Check out the following available switches on a Mac OS X machine :

usage: scp [-12346BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]

           [[user@]host1:]file1 ... [[user@]host2:]file2



Sunday 7 February 2016

Compare files contents in Mac OS X from command line using Comm

Comm is the command in Mac OS X if you want to compare the contents of two files through the command line.

The usage is as follows :

comm -switch file1 file2

where -switch can be any of the following :

-1 : This will not display the lines which are UNIQUELY present only in File1
-2 : This will not display the lines which are UNIQUELY present only in File2
-3 : This will not display the lines which are COMMON in both the files. i.e it will only display the unique lines from File1 and the File2

-i : This will basically display 3 columns. The 1st column will display lines which are UNIQUELY present only in File1. The 2nd column will display lines which are UNIQUELY present only in File2. And the 3rd column will display lines which are COMMON in both the files.

For eg:

we have taken two Text files for comparison, the contents of which are as follows :

TextFile1.txt

ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ

TextFile2.txt

ABCD
DEFG
GHI
JKL
MNO
PQR
STU
VWXYZ
YZ

------ Usage 1 ------

comm -1 TextFile1.txt TextFile2.txt
ABCD
DEFG
GHI
JKL
MNO
PQR
STU
VWXYZ

YZ

------ Usage 2 ------

comm -2 TextFile1.txt TextFile2.txt
ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX

YZ

------ Usage 3 ------

comm -3 TextFile1.txt TextFile2.txt
ABC
ABCD
DEF
DEFG
VWX

VWXYZ

------ Usage 4 ------

comm -i TextFile1.txt TextFile2.txt 
ABC
ABCD
DEF
DEFG
GHI
JKL
MNO
PQR
STU
VWX

VWXYZ


Friday 5 February 2016

Stealth mode in Mac OS X

In Mac OS X, you can enable Stealth mode so that ping requests to your IP address are unreachable.

Go to System preferences -> Security & Privacy -> Firewall -> Firewall Options

Tick the Stealth mode options as shown below:


Now if you try to ping your Mac's IP address from another machine, you'll get the Request Timeout error with the stealth mode enabled.

Tuesday 2 February 2016

Hidden Screenshot Tricks on Mac OS X

Here's a quick guide to the keyboard shortcuts for taking screenshots on Mac OS X.
  • For capturing the entire screen press Command+Shift+3.
  • For capturing only the current active window press Command+Shift+4 and then the space bar and Return.
  • For capturing a specific limited region from the screen press Command+Shift+4 and choose the area by dragging the crosshairs.
Now here'a a small compilation of the hidden tricks of screenshots :
-----------------------
By default the screenshots are saved as a .png file on the desktop. For changing the default save location do the following.
Open Terminal and type the following for example :

defaults write com.apple.screencapture location /Users/IronCladWriter/Pictures

Make sure to replace the location path as desired.
-----------------------
If you want to change the image type from default .png to say .jpg, simply type the following :

defaults write com.apple.screencapture type jpg
-----------------------
To apply the above changes type in Terminal :

killall SystemUIServer
-----------------------

Alternatively you could fire up the Grab application, as seen in Other Utilities


Saturday 11 October 2014

Contra video game for iPad

Guys do you remember the good old days playing classic games like Bomberman, Contra etc during the Atari & Sega console box days. I was recently so happy to see Contra ported to the iPad. I immediately downloaded it and have been playing it for quite some time now. 

For those who are unaware of the game, please note that this is a classic side scrolling shooter action game. In fact it is so addictive, I bet you would play even while commuting or just lazying around the couch. A bunch of weapons goodies like the laser gun, SL gun, SH gun, power shield make for an interesting experience. Also with each new level, the difficulty gets increasing proportionally. Honestly, there's nothing pathbreaking in this game, but play it just for the good old shooting fun. Aah the vintage classics!

Check out a few screenshots here :) This will definitely make you download it.






Saturday 10 May 2014

Lock Screen in Mac OS X

Here's a quick tip for the month of May - Q) How to lock screen of Mac OS X?

Answer : Control + Shift + Eject button

Correct me if am wrong, the Windows equivalent to lock screen is Windows button + L  right

Sunday 20 April 2014

MySQL JDBC Connector on Mac OSX

If you're working on any Java based program which uses MySQL as it's back-end database, you might find this article extremely helpful.

In order to pull data from the MySQL database, it is first important to establish a connection with it so that the script can talk with it.

The connection can be established by using the MySQL JDBC driver. Now, this driver needs to be separately downloaded. Here's the download link btw. If you're working on Mac OSX, please select the platform-independent version of the Connector from the drop down.

Once downloaded, untar the tar (or unzip the zip file). Copy the mysql-connector-x.x-bin.jar to /Library/Java/Extensions.

If you're using Eclipse IDE for editing class files, you need to make some changes in Eclipse's preferences. So go to Eclipse -> Preferences -> Java -> User Libraries. Click 'New' to create a new user library named as "MYSQL_CONNECTOR_LIBRARY" for instance. Now, click "Add External JARs..." button and browse to the downloaded jar : mysql-connector-x.x-bin.jar.

So if you're Java application is trying to connect to MySQL database, the following code snippet might come in handy for quick reference.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;


try {

connection=DriverManager.getConnection("jdbc:mysql://localhost:3306", "username", "password");

}

Tuesday 1 April 2014

Validate Fonts on Mac OS X

Occasionally or infact very rarely, fonts might get corrupt on Mac OS X. Mac OS X offers the option to validate the authenticity of fonts so that they are safe to use.

Open Fontbook

Click "All Fonts" from the Collection bar on left. Now select and highlight all the installed fonts using shift key (from top to bottom).

From the File menu -> choose Validate Fonts

You'll see the results of the output, something like below. This is a useful procedure to check the font authenticity, if you download & install some third party fonts from the Internet. Just an extra step of caution to make sure that nothing is corrupted. Hope it helps. Ciao.


Wednesday 5 March 2014

Artifactory on Mac OS X

Recently, while trying to install Artifactory on my Mac OS X machine, I encountered a weird error. Finding a solution was a bit tricky and took some time to figure it out.

Artifactory is basically an open source repository manager. I downloaded the latest current version v3.1.1.1. In this tutorial, we'll cover the easy method using hot deployment on servlet container.

Note that Artifactory comes pre-bundled with it's war file which we can deploy onto Tomcat. Although it does come pre-loaded with it's own container, I wanted to deploy the war using Tomcat 7. Since I already have Tomcat 7 installed on my machine.

When I logged on to Tomcat manager and tried to deploy the Artifactory war, I got the generic looking error : FAIL - application at context path could not be started.

So I fired up the terminal and looked into the Tomcat Catalina logs and here's what I came across :




The error "Setting property 'disableURLRewriting' to 'true' did not find a matching property." was looking really strange. So I googled a bit and realised that Artifactory needs JDK 1.7 to run. Refer this forum for some insights.

Now since Mac machines come pre-loaded with Apple supplied Java 6, I was in a kind of a fix. I wondered what to do. Should I download and install Java 7 & uninstall Java 6?

However I was skeptical of uninstalling Java 6. Also, I was not quite sure about what overall impact it would have by uninstalling Java 6 altogether. Since I mentioned previously that Google Chrome does not work with 64-bit Java 7. I spent some time doing impact analysis but didn't quite reach a conclusive decision. A little bit of coffee did the trick :)

Now, here's the workaround. Download the JDK 1.7 for Mac from this link. Now luckily I had the Eclipse IDE with Tomcat plugin ready. If you don't have this setup yet, I highly recommend working with the Eclipse IDE. Lots of customizations are possible.

Open Tomcat Preferences and and expand Java > Installed JRE from the left menu. Click Add. Now browse to /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home. Describe it with some name like Jdk 1.7. Apply settings and don't exit the preferences as yet.

Make sure you only mention the path till Home. If you mention path further like .../Contents/Home/bin/java, you will see an error like "Target is not a JDK root. Java executable not found."

Check the screenshot for reference.




Now, scroll down the preferences' left pane and expand Tomcat > JVM Settings. From the drop down, select Jdk 1.7 that just defined above. So henceforth, Tomcat container will only be invoked using Jdk 1.7. If for any reasons, you have to invoke using Java 6, you can always choose Apple supplied Java 6 from this list. Check the screenshot for reference.




Now simply hit the Start Tomcat button. Bingo. Wait and watch the console till you see that Artifactory has started. Something like this : 

###########################################################
### Artifactory successfully started (29.031 seconds)   ###
###########################################################

Now, all you have to do is open browser to localhost:<listener-port-no>/artifactory. By default, tomcat's listener port is 8080. You can modify it if you want to.




In my next tutorial, I'll post about working with Artifactory & Maven repositories. Stay tuned folks.

Tuesday 4 March 2014

Using MD5 checksum utility on Mac OS X

If you might be aware, every Linux distribution comes with the MD5 checksum utility - md5sum. Similarly UNIX flavors like Mac OS X, come pre-bundled with utility called md5

The OS X md5 utility can be found at /sbin/md5

I'll quickly show some steps on how to use it :

  • Open Terminal
  • Create a blank empty file anywhere on the system (I mean just anywhere) using touch


Eg : touch test.xml
  • Now type the following to generate the md5 checksum of this file 
Eg : md5 test.xml > test.xml.md5

That's it. Now open the test.xml.md5 using TextEdit or Text Wrangler and you'll see the file's equivalent md5 checksum. 

If you want to change the format of the output similar to Linux's md5sum use the -r switch 

Eg : md5 -r test.xml > test.xml.md5

Note that MD5 checksums are used to verify the integrity of a file. Even a single new character added to the file will change the equivalent md5 output. Check for yourself. Modify the test.xml by adding some content to it. Now run the md5 utility again as shown above. The md5 equivalent of the file will now be different. The checksums are important to verify that the files / resources are intact throughout and ensure that they are not corrupt or tampered.

You can use this method to verify the integrity of zip or tar files that you download from the internet.

A free utility Checksum Validator is also available. At the heart of it's gui, it uses this md5 command.

This information can come particularly handy while using Maven as well. Suppose for some odd reason, you have to manually create md5 checksums of the dependency jar's - then this method tells you how to do it.

Friday 28 February 2014

Info on Google Chrome with Java 7 on Mac OS X

Fact Check : Guys, did you know the following fact about Chrome : Google Chrome does not work with Java 7 on Mac OS X. Turns out that the 32-bit Chrome browser does not support Oracle Java 7 on Mac OS X.



Folks, please make a note of this, in case you're planning to upgrade Apple supplied Java 6 to Java 7.

Also note that Chrome works perfectly fine with Java 6 while other browsers like Mozilla Firefox & Apple Safari do support Java 7 out of the process.

Refer the following official Oracle Java link about this fact.

Chrome's 64-bit version is among it's future plans as listed on this Chromium Design Documents page.
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker