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);
}
}
}
This is a place where you can find some hard searched, some regularly used, some fundoo - innovative, and some my own RnD utility stuff.
Visitor's questions, suggestions, comments are welcome.
Wednesday, June 01, 2011
Java Robot Example - Program to keep your computer away from Idle mode
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();
}
}
}
Wednesday, December 29, 2010
ANT - sample build.xml & build.properties files for creating WAR
<project name="TestProject" default="war" basedir=".">
<property file="build.properties"/>
<property name="src.home" value="${basedir}/src"></property>
<property name="dist.home" value="${basedir}/dist"/>
<property name="web.home" value="${basedir}/WebRoot"></property>
<property name="build.home" value="${web.home}/WEB-INF/classes"/>
<property name="lib.home" value="${web.home}/WEB-INF/lib"/>
<property name="lib.ext.home" value="${basedir}/lib"/>
<path id="compile.classpath">
<fileset dir="${lib.home}">
<include name="*.jar"/>
</fileset>
<fileset dir="${lib.ext.home}">
<include name="*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="${dist.home}" />
</target>
<target name="compile" depends="init" >
<javac destdir="${build.home}" debug="true" srcdir="${src.home}">
<classpath refid="compile.classpath"/>
</javac>
<copy todir="${build.home}">
<fileset dir="${src.home}" includes="**/*.properties,**/*.xml"/>
</copy>
</target>
<target name="war" depends="compile">
<war destfile="${dist.home}/${app.name}.war" webxml="${web.home}/WEB-INF/web.xml">
<fileset dir="${web.home}"/>
</war>
</target>
<target name="clean">
<delete dir="${dist.home}" />
<delete includeemptydirs="true">
<fileset dir="${build.home}" includes="**/*"/>
</delete>
</target>
<target name="deploy2Tomcat">
<copy todir="${tomcat.webapps.dir}">
<fileset dir="${dist.home}" includes="${app.name}.war" />
</copy>
</target>
</project>
<copy todir="${build.home}">
<fileset dir="${src.home}" includes="**/*.properties,**/*.xml"/>
</copy>
</target>
<target name="war" depends="compile">
<war destfile="${dist.home}/${app.name}.war" webxml="${web.home}/WEB-INF/web.xml">
<fileset dir="${web.home}"/>
</war>
</target>
<target name="clean">
<delete dir="${dist.home}" />
<delete includeemptydirs="true">
<fileset dir="${build.home}" includes="**/*"/>
</delete>
</target>
<target name="deploy2Tomcat">
<copy todir="${tomcat.webapps.dir}">
<fileset dir="${dist.home}" includes="${app.name}.war" />
</copy>
</target>
</project>
build.properties
app.name=TestProject
catalina.home=D:/apache-tomcat-6.0.20
tomcat.webapps.dir=${catalina.home}/webapps
Tuesday, September 07, 2010
Macintosh - Run jar files by double clicking
This page assumes you want to run a .jar file directly, on your Macintosh, without splitting it up in any way.
First you have to have the "MRJ", the Macintosh Java Runtime. Then you have to find "JBindery".
In the Command Menu, where it says Class, you enter the name of the main program. For example, "jdrill", "CFclient", or some other classname.
Then pull down the 'ClassPath' submenu, click on 'Add .zip file', and select the .jar file you want. Then click "Run".
You can create an application by selecting the 'FILE' menu, then 'Save As...'. Once you've done that, you can double click on the icon to execute the java crossfire client.
Saturday, July 31, 2010
Command Prompt option on Right Click Menu
How to add command prompt option to right click ?
Most of us often need to open the command prompt which opens at User's Home directory and we had to change the path manually every time we open the command prompt.
For making this much easier, I am providing here a simple way which you will be glad to use instead of irritating/time consuming way.
With below steps, you can open the command prompt and find yourself at the location you want to reach with just a single click of mouse.
[1] Create a new file and save it with any name but with the extension ".reg" i.e. registry file extension.
filename ezample:= abc.reg
[2] Now copy paste below code inside that file and again save it.
Windows Registry Editor Version 5.00
@="Command Prompt"
[HKEY_CLASSES_ROOT\Directory\shell\Command\Command]
@="cmd.exe /k cd %1"
[3] Execute this file by simply double-clicking over it.
[4] Command Prompt option is added whenever you do a right-click on a folder.
This is one time procedure.
[5] Now onwards whenever you want to open the command prompt and reach out to a particular location, you can just browse through windows file explorer and right click on that particular folder and select command prompt option.
Friday, July 30, 2010
Eclipse - Debug Web Application running on Tomcat/JBoss
How to debug a web application, running on Tomcat/JBoss server, in Eclipse ?
Here are the simple steps to follow:
Tomcat
set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket
catalina.bat jpda start
[2] Start eclipse -> Run menu -> debug configuration -> Remote Java Application -> New launch configuration -> Make sure port is 8000 in connection properties.
[1] Edit the run.bat file of JBoss to use the following debug options (note the suspend=n option, this tells the server not to wait till the debugger attaches itself to the server
set JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n %JAVA_OPTS%
2) Start the server from the command line, using the run.bat file.
3) Once the server has completely started, i go to my (plain) Eclipse project and create a new "Remote Java Application" debug session (if it's not already created). In the "Port" text box i specify 8787 (the same as what i have in the run.bat). Then click on Debug.
The steps will be the same for NetBeans too (expect for the part where you have to configure NetBeans to listen to the debug port).
Sunday, October 29, 2006
File Upload - JSP
[1] Add a form tag in your jsp file as shown below:
<input type="file" name="theFile"></form>
[2] Add snippet code as below in the jsp file mentioned for the attribute action of form tag above i.e. upload.jsp:
<%
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf ("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < byteread =" in.read(dataBytes," file =" new">
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
String savePath = request.getRealPath ("documents");
saveFile = savePath + "/" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " +saveFile);
}
%>