Easily switch between java versions using alternatives in Linux

This approach works in several distros, I’ve been using it in Ubuntu for a while and just used it in Fedora as well.
As a developer, you might need to switch between java versions often, this approach will come in handy then.
We will be using the command “alternatives”, in this case to check the configuration of your Java installation. The default is most often OpenJDK, while you might need Oracle Java.
Run “alternatives –display java” to see which versions you can currently choose from:
[code gutter=”false”]
[joris@today ~]$ alternatives –display java
java – status is manual.
link currently points to /usr/java/latest/bin/java
Current `best’ version is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.31-3.b13.fc21.x86_64/jre/bin/java.
[joris@today ~]$
[/code]
There’s no Oracle Java yet, make sure you’ve installed Oracle Java. If you haven’t, you can check this blog post: Install Oracle Java in Fedora, Red Hat or CentOS using Yum and RPM
When Oracle Java is installed, you can add it to your alternatives: “sudo alternatives –install /usr/bin/java java /usr/java/latest/bin/java 20000”
Please note: I used “latest” in the command above, another options is to specifically set the version you want. This way you can install several JDK’s and switch as shown below.
When that is finished, you can select your current flavour of Java:
[code gutter=”false”]
[joris@today ~]$ sudo alternatives –config java
There are 2 programs which provide ‘java’.
Selection Command
———————————————–
* 1 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.31-3.b13.fc21.x86_64/jre/bin/java
+ 2 /usr/java/latest/bin/java
Enter to keep the current selection[+], or type selection number:
[/code]
Choose the option you want to switch between Java versions.
As mentioned by enkouyami, please check if you need to use update-java-alternatives instead of alternatives. The use of alternatives was valid when I wrote the post, but might not be working anymore!

Select an XML tag or node using Oracle PL SQL

Hi guys,
I’ve been using this trick for a while and it’s quite useful when querying Oracle Service Bus logs. I found myself trying to explain this one to a colleague and thought it made a nice post 🙂
Let’s start with the basic command:
extractvalue
Which translates into:
[code language=”sql”]
SELECT EXTRACTVALUE(
xmltype(xml_val),
‘/xml-fragment/tns:product’,
‘xmlns:tns="http://example.org/"’,
‘xmlns:ans="http://anothernamespace.org/"’,
‘xmlns:yans="http://yetanothernamespace.org/"’
)
x
FROM xml_table
[/code]
Note: the first argument is being cast from CLOB to XMLTYPE and that you can keep adding namespaces at the end by adding commas.
I’ve added three rows in my table “XML_TABLE” for this example:
[code language=”XML”]
ROW1:
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
ROW2:
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
ROW3:
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
[/code]
Next we’ll query our XML_TABLE:
[code language=”sql”]
SELECT
EXTRACTVALUE( xmltype(xmlval), ‘/book/title’ ) AS title,
EXTRACTVALUE( xmltype(xmlval), ‘/book/author’ ) AS author,
EXTRACTVALUE( xmltype(xmlval), ‘/book/year’ ) AS year,
EXTRACTVALUE( xmltype(xmlval), ‘/book/price’ ) AS price
FROM xml_table;
[/code]
Which results in this output:
Workspace 1_034
Awesome, right!? 🙂
Continue reading “Select an XML tag or node using Oracle PL SQL”

Bash: Shortcuts to your favorite directories with CDPATH

This is a fairly handy trick.
I’m always logging in to different servers, which all have a different location for their logs. With this trick you can login and just type “cd logs” from anywhere.
First we will add our directory to the CDPATH variable:
[code language=”Bash”]
joris@badattitude /data/share/my_domain
$ echo $CDPATH
joris@badattitude /data/share/my_domain
$ export CDPATH=/data/share/my_domain
joris@badattitude /data/share/my_domain
$ echo $CDPATH
/data/share/my_domain
[/code]
Then we’ll check if it is working:
[code language=”Bash”]
joris@badattitude ~
$ cd logs
/data/share/my_domain/logs
joris@badattitude /data/share/my_domain/logs
$
[/code]
Awesome, we’ve arrived in our logs directory straight from our homedir!

SSH Remote Execute command, multiple command and with interaction

This article shows how to execute remote commands via ssh, but you’ll send the commands from your own shell.
[code highlight=”1″]
ssh my_server ‘ls -l /home/my_home_dir’
[/code]
This will result in this output:
[code highlight=”1″]
$ ssh ae2 ‘ls -lha ~’
total 36K
drwxr-xr-x 2 joris joris 4.0K Jan 23 11:42 .
drwxr-xr-x. 5 root root 4.0K Jan 23 11:41 ..
-rw-r–r– 1 joris joris 54 Jan 23 11:41 .bash_logout
-rw-r–r– 1 joris joris 507 Jan 23 11:41 .bash_profile
-rw-r–r– 1 joris joris 213 Jan 23 11:41 .bashrc
-rw——- 1 joris joris 51 Jan 23 11:42 .history
-rw-r–r– 1 joris joris 171 Jan 23 11:41 .kshrc
-rw-r–r– 1 joris joris 375 Jan 23 11:41 .profile
-rw-r–r– 1 joris joris 153 Jan 23 11:41 .vimrc
[/code]
What’s even better, is that you can run multiple commands separated with a semi colon, like this:
[code highlight=”1″]
ssh my_server ‘ls -l /home/my_home_dir;whoami’
[/code]
And the best trick is this one, user input with an interactive command, sending input and output back and forth!
[code highlight=”1″]
ssh -t my_server ‘vi ~/.bash_profile’
[/code]

Start JDeveloper on Ubuntu Linux for Fusion Middleware SOA Suite 12c

Okay, this was a bit awkward, I installed the quick start 12c Fusion Middleware and was well underway to getting it all up and running.
Untill I closed JDev and couldn’t find the executable anymore..!
For anyone who had the same embarrassing situation, it’s located here:

*MiddlewareInstallationFolder*/jdeveloper/jdev/bin/jdev

Find Java JRE location on Ubuntu Linux

Everybody knows that the java executable is located in /usr/bin/java , but what if you need the JDK / JRE location itself?
Just using “whereis” will not get you there, that will point you to the /usr/bin/java point.
joris@howlingmad: ~_011
 
So, let’s find out a but more about /usr/bin/java:

ls -l /usr/bin |grep java

joris@howlingmad: ~_012
 
Awesome, this will lead us somewhere, it’s a symlink to /etc/alternatives/java
So let’s do the same there:

ls -l /etc/alternatives/ |grep java

And we’ve hit the jackpot, among the lines here, there’s a bunch of lines pointing us to the JRE location:
joris@howlingmad: ~_013
 
As you can see in the screenshit, our java executable within the JRE location is:

/usr/lib/jvm/java-7-oracle/jre/bin/java

Which means the JRE location is:

/usr/lib/jvm/java-7-oracle/jre

 
 
 

Oracle SQL Developer 4 does not run on Oracle Java 7 on Ubuntu 14.04

Wow, ain’t this awkward :-). I cannot run Oracle SQL Developer 4 (4.0.2) on Ubuntu with Oracle JDK 7..

To be complete: when running SQL Developer with JDK 7 from Oracle itself, displays the following error;

joris@dipshit:~/programs/sqldeveloper$ ./sqldeveloper.sh
Oracle SQL Developer
Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
LOAD TIME : 968#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x6aa69be0, pid=9537, tid=1836366656
#
# JRE version: Java(TM) SE Runtime Environment (7.0_65-b17) (build 1.7.0_65-b17)
# Java VM: Java HotSpot(TM) Server VM (24.65-b04 mixed mode linux-x86 )
# Problematic frame:
# C 0x6aa69be0
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/joris/programs/sqldeveloper/sqldeveloper/bin/hs_err_pid9537.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
/home/joris/programs/sqldeveloper/sqldeveloper/bin/../../ide/bin/launcher.sh: line 1193: 9537 Aborted (core dumped) ${JAVA} "${APP_VM_OPTS[@]}" ${APP_ENV_VARS} -classpath ${APP_CLASSPATH} ${APP_MAIN_CLASS} "${APP_APP_OPTS[@]}"

Solution: Run Oracle SQL Developer with OpenJDK

First we’ll need to install OpenJDK:

sudo apt-get install openjdk-7-jdk 

Then we’ll need to change the path which SQL Developer uses. This was asked once when you first started it and it is saved in the following path:

~/.sqldeveloper/4.0.0/product.conf

The file [[ product.conf ]] contains the value SetJavaHome, we need to change this to the OpenJDK path;

If you're running 32 bit Ubuntu:

SetJavaHome /usr/lib/jvm/java-7-openjdk-i386

Or if you're running 64 bit Ubuntu:

SetJavaHome /usr/lib/jvm/java-7-openjdk-amd64

After saving this change, you can start SQL Developer on Ubuntu 14.04 and it will use OpenJDK 7, without changing your regular Java settings!

SSH: Different settings (keys!) for different hosts

There’s a simple way to create aliases in a SSH config file. This way you can connect way easier to different hosts, combined with this blog post to use keys to log in.
Where you used to use this connect string:

ssh pi@192.168.0.5
pi@192.168.0.5's password: <<enter boresome password>>

You can now just enter

ssh pi

Where “pi” is the alias that you’ll be using!
The only thing you’ll need to do is create the following file:
[[ ~/.ssh/config ]]

Host pi
  HostName 192.168.0.5
  User pi
  << (!) Only add below line if you're using keys to log in >>
  IdentityFile ~/.ssh/keys/raspberry_key

Aint that awesome!?
Ps. If you’re still being asked to enter your password, check your keys and please look at this blog post I wrote.
 

Suspend in Gnome 3

I was searching for the suspend button in Gnome 3 and could not find it, even though I knew it was enabled when I logged into Unity and was able to suspend the same laptop..!
Appearently it is hidden (a bit).
This is the top right menu in Gnome 3 by default (without suspend options)
Screenshot from 2014-08-11 13:43:44
If you press “alt” while this menu is open, it will show the suspend button (with the pause icon):
Screenshot from 2014-08-11 13:44:22
So now I can suspend my laptop again!
Happy energy savings! 🙂
 

SSH without password

SSH is one of the most friendly deamons in the Linux toolbox, you can port forward your home server, you can surf the internet via your own proxy server, you can transfer files, it’s the first thing I set up on a freshly installed box and by far the most used service around my home (yeah, that’s nerdy) 🙂
This post will outline how you can create a public and private RSA key pair, and then we will use that key pair to authenticate ourselves to another computer in the network.
We will need to complete the following steps:

  1. Create a public/private rsa key pair
  2. Copy the public key to the remote host via ssh-copy-id
  3. Login to the remote system without a password

Create a public/private rsa key pair

If you haven’t created a rsa key pair yet, we can create it with this command:

ssh-keygen

As shown in this screenshot:
Screenshot from 2014-07-28 21:28:18If you have previously created a key pair, you will be asked to replace this.
For my home machines I don’t use a passphrase, if you’re more paranoid (or careful) you can do so. Please read this article for more info on passphrases and how to use them.
The keys have been generated in /home/yourloginname/.ssh and are called id_rsa and id_rsa.pub.
Never, never send someone your private (id_rsa) key! That is the same as handing over your house keys..

Copy the public key to the remote host via ssh-copy-id

This step should be repeated for all hosts to which you want to SSH with the newly created RSA key pair.
The command used is:

ssh-copy-id -i ~/.ssh/id_rsa joris@192.168.0.10

Screenshot from 2014-07-28 21:30:35
 
You will need to enter your password one last time, after that your public key is added to the authorized_keys file, which is automatically created on the target system.
After this step you don’t need to use a password any more!

Login to the remote system without a password

Just SSH into the remote system:
Screenshot from 2014-07-28 21:32:01
And wonder what you’re gonna do with all that extra time you’ve just won because entering passwords belongs to the past… 🙂