Thursday, June 09, 2011

Switch JAVA_HOME environment variable with just a double click

Hassle-free way for setting JAVA_HOME quickly on windows machine using VB script.

Why I decided to write a (VB)script ?
Recently I had to work in environment where I had to set JAVA_HOME environment variable very frequently and I really got bored by the lengthy steps we usually follow to set an environment variable like open file explorer then right click My Computer -> select Properties -> Advanced Tab -> Environment Varibles -> choose ur variable and edit it and then again copy required path. So I searched on internet a bit and by spending few hours I could mange to write a successful vb script to do what was required.
Surprisingly, writing a registry file did not work..
To save the search effort, I am writing this blog..

Steps / Procedure to write the script:

[1] Create two VB Script files as shown below.

File Name: set_jdk6.vbs

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER")
WshEnv("JAVA_HOME") = "C:\Program Files\Java\jdk1.6"


File Name: set_jdk5.vbs

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER")
WshEnv("JAVA_HOME") = "C:\Program Files\Java\jdk1.5"

[2] Now simply double click above files to switch JAVA_HOME environment variable on your windows machine.

That's it. Cheers...

Wednesday, June 01, 2011

Java Robot Example - Program to keep your computer away from Idle mode


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * This program will keep your computer alive i.e. it does not let computer go in Idle mode.
 * Default wake up time = 1 min
 */
public class KeepAliveRobot{
 
    private static boolean pause = true;
    private static final int DEFAULT_SLEEP_TIME = 60000; // 60000 miliseconds = 1 min
 
    public static void main(String[] args)
                 throws AWTException,IOException {

        Robot robot = new Robot();
        Properties p = new Properties();
        int sleepTime = DEFAULT_SLEEP_TIME;
        try {
            p.load(new FileInputStream("live.properties"));
            sleepTime = Integer.parseInt(p.getProperty("wakeup.time"));
           
          } catch (Exception e) {
            e.printStackTrace();
        }
          while (pause) {
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException e1) {
                break;
            }
               
            robot.keyPress(KeyEvent.VK_ALT);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_ALT);         
             
        }   
    }
}
 

Java program to test whether server is reachable ?


import java.net.InetAddress;
 
public class PingExample
{
    public static void main(String[] args)
    {
        try
        {
            InetAddress address = InetAddress.getByName("127.0.0.1");
    
            /**
             * FROM JAVADOC
             *
             * Test whether that address is reachable. Best effort is made by the
             * implementation to try to reach the host, but firewalls and server
             * configuration may block requests resulting in a unreachable status
             * while some specific ports may be accessible.
             * A typical implementation will use ICMP ECHO REQUESTs if the
             * privilege can be obtained, otherwise it will try to establish
             * a TCP connection on port 7 (Echo) of the destination host.
             * <p>
             * The timeout value, in milliseconds, indicates the maximum amount of time
             * the try should take. If the operation times out before getting an
             * answer, the host is deemed unreachable. A negative value will result
             * in an IllegalArgumentException being thrown.
             *
             * @param    timeout    the time, in milliseconds, before the call aborts
             * @return a <code>boolean</code> indicating if the address is reachable.
             * @throws IOException if a network error occurs
             * @throws  IllegalArgumentException if <code>timeout</code> is negative.
             * @since 1.5
             */
            boolean reachable = address.isReachable(10000);
            System.out.println("Is host reachable? " + reachable);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}