Sunday, September 14, 2008

JAR - Command Reference

Common JAR file operations

To create a JAR file
jar cf jar-file input-file(s)

To view the contents of a JAR file
jar tf jar-file

To extract the contents of a JAR file
jar xf jar-file

To extract specific files from a JAR file
jar xf jar-file archived-file(s)

To run an application packaged as a JAR file
(requires the Main-class manifest header)
java -jar app.jar

To invoke an applet packaged in a JAR file to be displayed in your web page,put the following Tags.


<applet height="height" archive="JarFileName.jar" width="width" code="AppletClassName.class">
<param name="_cx" value="26">
<param name="_cy" value="26">
</applet>



Ref:http://java.sun.com/docs/books/tutorial/deployment/jar/

Tweaking MS Windows Command Prompt

Change the Font in the Command Prompt
http://smallvoid.com/article/winnt-cmd-add-font.html
http://phatness.com/node/1643

Add the "Open Command Prompt Here" Option in Windows Exploper
http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm

Saturday, September 13, 2008

Convert TNS to JDBC String or TNS String to JDBC

For the developers who costantly work with the sql editor and then the java code (jsp or servlets), changing the connections string from tns to JDBC or JDBC to tns is a very common exercise .
Here im presenting some code to do this conversion.
Hope this will be help for some of u...

Convert JDBC to TNS

public void convertJDBC2TNS(){
System.out.println("In My button action listener");

//str is something like
//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME
//we want the output to be something like
//XE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=XE)))


String str = n.getTextArea().getText();
String str2= new String();
System.out.println("My text is : \n"+str);

for(int i=0;i(lessthan)str.length();i++){
char c = str.charAt(i);
if(c==' '||c=='\t'||c=='\n'){
// do nothing
}
else{
char cl = java.lang.Character.toLowerCase(c);
char[] charArray ={cl};
str2 = str2.concat( new String(charArray));
}
}

System.out.println("String = "+str2+"\nlength = "+str2.length());
//XE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=XE)))

int hostIndex = str2.indexOf(":@");
int portIndex = str2.indexOf(":", hostIndex+1);
int sidIndex = str2.indexOf(":", portIndex+1);
System.out.println("hostIndex = "+ hostIndex+"\n portIndex = "+ portIndex+"\n sidIndex = "+ sidIndex );

int closingIndex;
closingIndex = str2.indexOf(":", hostIndex+1);
String host = str2.substring(hostIndex+2, closingIndex);
System.out.println("host = "+host);

closingIndex = str2.indexOf(":", portIndex+1);
String port = str2.substring(portIndex+1, closingIndex);
System.out.println("port = "+port);

//closingIndex = str2.indexOf(")", sidIndex);
String sid = str2.substring(sidIndex+1);
System.out.println("sid = "+sid);

//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME
String tnsString = sid.toUpperCase()+"=(DESCRIPTION="+"\n\t"+
"(ADDRESS=(PROTOCOL=tcp)(HOST="+host+")(PORT="+port+"))"+"\n\t"+
"(CONNECT_DATA=(SID="+sid.toUpperCase()+"))"+"\n\t"+
")";
System.out.println("My text after conversion is : \n"+tnsString);
n.getTextArea().setText(tnsString);
}



Convert TNS to JDBC


public void convertTNS2JDBC(){
System.out.println("In My button action listener");

//your str is something like
//XE=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=XE)))
//and you want to convert it to
//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME

String str = n.getTextArea().getText();
String str2= new String();
System.out.println("My text is : \n"+str);

for(int i=0;i(lessthan)str.length();i++){
char c = str.charAt(i);
if(c==' '||c=='\t'||c=='\n'){
// do nothing
}
else{
char cl = java.lang.Character.toLowerCase(c);
char[] charArray ={cl};
str2 = str2.concat( new String(charArray));
}
}

System.out.println("String = "+str2+"\nlength = "+str2.length());
//mz5ms202=(description=(address=(protocol=tcp)(host=ap6005rems)(port=1575))(connect_data=(sid=mz5ms202))

int hostIndex = str2.indexOf("host");
int portIndex = str2.indexOf("port");
int sidIndex = str2.indexOf("sid");
System.out.println("hostIndex = "+ hostIndex+"\n portIndex = "+ portIndex+"\n sidIndex = "+ sidIndex );

int closingBraceIndex;
closingBraceIndex = str2.indexOf(")", hostIndex);
String host = str2.substring(hostIndex+5, closingBraceIndex);
System.out.println("host = "+host);

closingBraceIndex = str2.indexOf(")", portIndex);
String port = str2.substring(portIndex+5, closingBraceIndex);
System.out.println("port = "+port);

closingBraceIndex = str2.indexOf(")", sidIndex);
String sid = str2.substring(sidIndex+4, closingBraceIndex);
System.out.println("sid = "+sid);

//jdbc:oracle:thin:@ADDRESS:PORT:DBNAME
String jdbcString = "jdbc:oracle:thin:@"+host+":"+port+":"+sid+"";
System.out.println("My text after conversion is : \n"+jdbcString);
n.getTextArea().setText(jdbcString);
}

Syntax Highlighting the Source Code in Html/Blogs

Your text here...

<pre name="code" class="sql">
put your code here
</pre>

rest of ur text goes here...

Best Posts
http://en.blog.wordpress.com/2007/09/03/posting-source-code/
http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/


More...
http://blog.felho.hu/posting-source-code-in-wordpress-escaping-and-syntax-highlighting-the-inserted-code.html

Monday, September 1, 2008

My Hello world JSP... :)

Here is a short tutorial of how to create a simple hello world servlet and deploy it in a ApacheTomcat Server.
This is intended for the people who are very new to the JSP and servlets.
Ppl who are experts in the JSP and Servlets find this post very childish.... :D.

On an Overall view we will be doing the following simple steps.
Create a Severlet and its Deployment descripter.
Deploy it in a web server.
Run the page to test the servlet.

Step 1: Create a Project directory some where on your Hard drive.
Example: Create the main project directory HelloWorldApp and three subdirectories with name classes,etc and src respectively.



Step 2: Create a file with name HelloWorldServlet.java, You can copy paste the following code.Your are a java developer... so you can add in some more code here as well... :)




import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class HelloWorldServlet extends HttpServlet{

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{

PrintWriter out = response.getWriter();
java.util.Date today = new java.util.Date();
out.println("<html>"+
"<body>"+
"<h1 align=left>Hello World !!!</h1>"+
"<br>"+"Now the date and time is : "+today+"</body>"+"</html>");
}


}


Step 3: Create a deployment descriptor file i.e web.xml in the etc folder of your project.
Copy paste the following code.






HelloWorld Servlet
HelloWorldServlet


HelloWorld Servlet
/SayHello




Step 4:Compile your servlet classe and generate the class file under the classes folder of your project


F:\DevCore\HelloWorldApp>javac -classpath C:\ApacheTomcat6018\lib\servlet-api.jar -d classes src\HelloWorldServlet.java






Step 5: Create a folder HelloWorldApp under the Tomcat webapps folder.
Create a subfolder WEB-INF under the HelloWorldApp folder
Create a subfolder classes under the WEB-INF folder

Step 6: Copy the web.xml folder from your project directory ot the HelloWorldApp/WEB-INF folder under the Tomcat.
Copy the HelloWorldServlet.class file to the HelloWorldApp/WEB-INF/classes folder under the Tomcat.






Step 7: Start/Restart Apache Tomcat

Step 8: Enter the url "http://localhost/HelloWorldApp/SayHello" into your favourite Web browser...



Hurrah!!!.., here is your JSP Page running...

Ref : Chapter-1, Head First JSP and Servlets

Tuesday, April 22, 2008

Building a Intra Home Network by cross cabling.

It was had been years that i had been using PC and which gone quite old and that was the time i felt to buy a latest one.
Everthing went fine i bought a brand new PC home with whatever the later configuration that i could get till date.but then came the question.., what am i going to do with my old pc??, Im i going to through it away ???... definitely that is not a better option as still being old my old machine works great.. with a Intel p IV processor and a 1GB ram and 80HDD,dvd writer.., everthing well and good.Then came the idea how about putting my new pcs into a network and play around a little of network stuff and learn a little bit of how we can hack on machines.. :D.
Then i was searching on alternatives.., i found that simple cross cabling would be the best option since it only requires a crossed ethernet wire with a little pc configuration and nothing else.You really save a lot of money by not buying a router for this purpose.
Use can use a thus formed network as a LAN and also for Internet sharing.
Here i give the simple steps how to build a intra home network in the linked doc
http://surnida.bhasker.googlepages.com/NetworkSettings.doc
The information about how to make a crossed ethernet cable can be found at the following links.
http://en.wikipedia.org/wiki/Ethernet_crossover_cable
http://www.homenethelp.com/web/explain/about-ethernet-crossover.asp
http://www.leeindy.com/connecting_two_computers_using_crossover_cables.shtml
http://www.experts-exchange.com/Networking/Q_20960308.html

Friday, March 14, 2008

Customizing editplus with java tools...

Customizing the applications to the way i need exactly had been an interest of me for a very long time..., I always feel that there is something always missing in the application that i'm using.., may be it is a ms word or photoshop or editplus or netbeans..
Then i felt that there is one way to go beyond this.., "plugins" or sometimes called extensions or user tools...
Here i'm going to give u a small example of how you can extend the functionality of editplus with the custom java plugins or tools that you write which makes ur life simpler...

Use Case: I'm going to use a simple use case which i felt was the most time consuming thing to do and which was a missing feature in the editplus.
Being a java developer most of the time i write a lot of System.out.println() (sop) statements in my java code which helps a lot in debugging and let me know how where my program is going.
At the same time it becomes a big burden for me when i have to remove these sops...
Here i'll give you an example of how you can remove the sop statments in a java source file with a custom tool..,in our case a simple java class file.(You can think of any other functionality that you might be concerned with may be adding or deleting content and anything more.., by allmeans you are playing with files...)

People who are familiar with editplus might be knowing how the editplus can be integrated with the java compiler and the java runtime.With the java compiler you can compile your java class file and with the java runtime you can actually run your compiled class file
more information on this please visit editplus faq:http://www.editplus.com/faq.html

Following are the snapshots on how you can do this..

In my scenario java is installed on my machine at the following location..
D:\Program Files\Java\jdk1.6.0_01
Location of javac.exe
D:\Program Files\Java\jdk1.6.0_01\bin\javac.exe
Location of java.exe
D:\Program Files\Java\jdk1.6.0_01\bin\java.exe

Java Compiler settings:

























Java Runtime settigs:


























These two settings help you compile a java src file and run it..., what we are looking for is writing a tool of our own and configure it with ediplus.
First we start with writing a java src file with exactly does the action we wanted to do.., place the whole logic in the main method and with the arguments if required.
Here im giving you the example i tried, the logic for my tool in RemoveSOP.java
(Click on the image for a better view to open it in a different window or tab)


/*
* RemoveSOP.java
*
* Created on March 15, 2008, 11:40 AM
*
* To change this template, choose Tools Template Manager
* and open the template in the editor.
*/

/**
*
* @author Bhasker
*/
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class RemoveSOP {

/** Creates a new instance of RemoveSOP */
public RemoveSOP() {
}

public static void main(String[] args) throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;

try {
inputStream =
new BufferedReader(new FileReader(args[0]));
outputStream =
new PrintWriter(new FileWriter(args[0]+"_Modified"));

String l;
while ((l = inputStream.readLine()) != null) {
//System.out.println(l);
if(l.contains("System.out.println(")){

while (!(l.contains(");"))){
l = inputStream.readLine();
}
}
else{
outputStream.println(l);
System.out.println(l);
}
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}

}
}

}

Here is how you configure the JavaTool class file with its editplus as a user tool...


Here is my java source file MyJavaSrcFile.java with which im going to test for my javaplugintool...



/*
* MyjavaSrcFile.java
*
* Created on March 15, 2008, 11:47 AM
*
* To change this template, choose Tools Template Manager
* and open the template in the editor.
*/

/**
*
* @author Bhasker
*/
public class MyjavaSrcFile {

/** Creates a new instance of MyjavaSrcFile */
public MyjavaSrcFile() {
}

public static void main(String args[]){

System.out.println("****************************************************************************\n" +
" xdebug : " + " Welcome to the dummy program... :D ");
int num1 = 4;
int num2 = 5;
int num3;

System.out.println("****************************************************************************\n" +
" xdebug : " + " Ready to do the multiplication of "+num1+" with " +
""+num2+" times");

System.out.println("Result ... : "+num1+" X "+num2+" = "+num1*num2+"\n");

num3 = num1*num2;

System.out.println("****************************************************************************\n" +
" xdebug : " + " Have a nice day good bye.... :) ");


}

}



The log is as follows...



/*
* MyjavaSrcFile.java
*
* Created on March 15, 2008, 11:47 AM
*
* To change this template, choose Tools Template Manager
* and open the template in the editor.
*/

/**
*
* @author Bhasker
*/
public class MyjavaSrcFile {

/** Creates a new instance of MyjavaSrcFile */
public MyjavaSrcFile() {
}

public static void main(String args[]){


int num1 = 4;
int num2 = 5;
int num3;




num3 = num1*num2;




}

}

With the my java source file opened i click on the java tool button or ctrl+ so as to run the tool on this file.This will create a new file +_Modifed with all the sops removed which i can replace the whole of the source in my file after i confirm the changes with a compare tool like beyond compare.

Here are the results that you can compare....
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikfE_wcTaH_no_CTSvPfL4Q-Zw_pVBJq5FA-O90mp8LE3eplJ9yWr34uLT4xG8RQX0Wtu5NqmLg8nK8sbwuHlKUVqr6Isy4DiZl9Nc4JmpwBDxb1fdJoHzp-mqyKnvkYhJLPZxp3JldTo/s1600-h/compare.PNG
























He he..,the sops are removed in just second....
I came to hear from ppl that there are something called macros which does the same work more simply..., I'm yet to make my hands dirty with that....
Also the way i mentioned is not just limited to java tools but you can configure any other exe or jar or any file in a similar way..