Java: run command as root by Runtime.getRuntime().exec() in Ubuntu

Hey :)

a few days ago I needed to run `/etc/init.d/networking restart` command by Runtime.getRuntime().exec() in Java EE web application. The first and easiest way that came to mind was sudo without password and… It Worked!
* To execute sudo without password, open /etc/sudoers by text editor like `nano`:

$ sudo nano /etc/sudoers

And add your user or group to the end of file like below:

# for user
USER_NAME ALL= NOPASSWD: ALL

# for group
%GROUP_NAME ALL= NOPASSWD: ALL

let’s see my Java code:

String command = "sudo /etc/init.d/networking restart";
Runtime runtime = Runtime.getRuntime();
try {
    Process process = runtime.exec(command);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

Troubleshooting
if you get `sudo: no tty present and no askpass program specified` error, make sure the user that runs command is in /etc/sudoers.

let me know if you find similar or easier way :)

Installing Sun JDK 5 on Ubuntu 9.10 and 10.04

Hello :)

As you known, Sun JDK version 1.5 or 5 is deleted from Ubuntu 10.4 and 9.10 repositories and the version 6 has been replaced.

The easiest way to install Sun JDK 5 version is add its repository from Ubuntu 9.04 to the list of repositories in 9.10 and 10.04. For this purpose, follow the steps.

1- Open /etc/apt/sources.list with a text editor like gedit:

sudo gedit /etc/apt/sources.list

2- Add the following lines to the end of the file then save it and close:

  ## For sun-java5-jdk
 deb http://ir.archive.ubuntu.com/ubuntu jaunty-updates main multiverse

3- Update the packages lists and install sun-java5-jdk:

 sudo aptitude update
 sudo aptitude install sun-java5-jdk

* Above method can be used for other applications.

Another way to install jdk 5 is download software package and its dependencies from packages.ubuntu.com.

Good luck

How to posting to Identi.ca from the CLI

terminal - cli
Hi :)

Identi.ca is an open source social networking and micro-blogging service.
I like Ideni.ca. It’s good service.

I use CLI (command line) everyday. I like to dented from it. It’s simple.
Just install cURL:

$ sudo apt-get install curl

And type:

$ curl -u username:password -d status="message" http://identi.ca/api/statuses/update.xml

You will receive a response containing the XML coding for your post which acts as a confirmation that your post was submitted.

Also you can create a shell file for this
Open a new text document and add the following, save it as identica.sh (or anything ending in sh):

#!/bin/bash
curl -u username:password -d status=″$1" http://identi.ca/api/statuses/update.xml

Make sure you change the chmod to 777 using the following command.

$ chmod 777 /path/to/file/

When you located the file (or add a bash prompt) it makes it simplier to identi.ca the rules. For example you can type the following to command to link to the file.

$ ./path/to/idetica.sh "Message"

Nested X11 environment session


Hi :)

Instead of using a full-blown new virtual X for developing software you can use Xephyr to embed your KDE 4 session into your working KDE 3 or other X11 environment.

If you want to get a minimal KDE session up and running, just launch Xephyr (available in Kubuntu as xserver-xephyr; Gentoo users compile x11-base/xorg-server with USE=”kdrive”):

Xephyr :1 -extension GLX &

You can now launch KDE:

export DISPLAY=:1
/path/to/kde4/bin/startkde &

For other X11 environment just change /path/to/kde4 like:

/usr/bin/startx &

or

/usr/bin/startkde &

or

/usr/bin/startxfce&

You can use “locate” command to find paths like:

locate startkde

Appendix
Happy Nowruz 1387 (Iranian new year holiday)

Good Luck

What's dpkg-reconfigure

Reconfigure an already installed package like gdm.

dpkg-reconfigure package_name

For example you can reconfigure X Server:

sudo dpkg-reconfigure xserver-xorg