Monday, August 06, 2007

Focus - lot to do ..

After JDK 1.4, there was a lot to see about focus in Java. Its one of the most interesting and challenging topics in AWT/Swing. JDK1.4 onwards there is one central class which takes care about the focus on components/windows KeyBoardFocusManager. Not only that, now you have freedom to write your own FocusManager.

This is one example where I tried to catch the focus lost and gain event on Focusable components.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class ButtonTest{

public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

class ButtonFrame extends JFrame {
public ButtonFrame()
{
setTitle("Button Test Frame");
setSize(LENGTH,WIDTH);
ButtonPanel panel = new ButtonPanel();
add(panel);
}

int LENGTH = 300;
int WIDTH = 300;

}

class ButtonPanel extends JPanel implements FocusListener {

public ButtonPanel ()
{

JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
JButton button3 = new JButton("Button3");
JTextField text1 = new JTextField("Enter here");
add(button1);
add(button2);
add(button3);
add(text1);
button1.addFocusListener(this);
button2.addFocusListener(this);
button3.addFocusListener(this);
text1.addFocusListener(this);

}

public void focusGained(FocusEvent e) {
display("Focus gained", e);
}

public void focusLost(FocusEvent e) {
display("Focus lost", e);
}
void display(String prefix, FocusEvent e) {
System.out.println( prefix+" " + e.getComponent().getClass().getName());
}
}

If you are working on AWT or Swing , I would personally prefer to visit this article of JavaWorld:
http://www.javaworld.com/javaworld/jw-07-1998/jw-07-swing-focus.html

Saturday, July 14, 2007

Robot Class

Robot class is one of the most common classes used by guys doing automated testing. Giving some cool features to automate process. But still I find some thing lacking in this library or maybe I don't know how to use them.

Here I try to write a simple code, which write some text on a text editor and then close the editor without saving the file. Many things are hard coded in this example, like

robot.mouseMove(1420,20); // this is my desktop close button place in full screen mode.
So, if you want to run this code, just change this line according to your desktop. After changing run this code, open any editor say notepad within 10 seconds and then this example will write some text on it and then it will close the window.

import java.awt.Robot;
import java.awt.event.*;

public class MainClass {

public static void main(String[] args) throws Exception{


Robot robot = new Robot();
// giving you 10 sec to open any text editor.
robot.delay(10000);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_T);


robot.mouseMove(1420,20);
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
Thread.sleep(50);
robot.mousePress( InputEvent.BUTTON1_MASK );
robot.mouseRelease( InputEvent.BUTTON1_MASK );
robot.delay(1000);
robot.keyPress(KeyEvent.VK_TAB);
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);

}
}

This is some useless example, but one can thing of some good examples but using this class.

Saturday, July 07, 2007

jikes vs javac

In my last post Priyanshu made a comment, " have you tried jikes". So, decided to give a try.

jikes is something which converts .java file into .class file. Yes, it is javac but IBM product not a Sun product. And let me begin with jikes page on sourceforge.net :

"You may wonder why the world needs another Java compiler, considering that Sun provides javac free with its SDK. Jikes has five advantages that make it a valuable contribution to the Java community:".

Providing free things, softwares and codes is a big deal for IBM but never for Sun. So don't keep on singing this Open source contribution song all the time. Whole world know what IBM is giving free.

So, advantage(s)

No. 1 : jikes is a open source software.
And so javac is ! Didn't get the advantage.

No. 2: Strictly Java compatible - It is not providing any superset or subset variation support.

Good, but this little of strictness most of time increase the code line from thousands or so.

No. 3: High Performance: Compilation is faster than javac.

Who really bother about compilation, anyway you have to come again and run on java command. I guess world is ready to devote 2 more minute in compilation.

Anyway, I have compiled one of the .java file(one which i have used in the last post) on the most stable version of jikes:

jikes TestForJDB.java

Issued 5 system warnings:

*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.


*** Semantic Warning: The file "E:/Program Files/Java/jdk1.6.0/jre/lib/ext/" doe
s not exist or else is not a valid zip file.

Didn't get what this warning message is and really not why you show it to me 5 times.

Then I have tried to look into -version command.

javac -version

- javac 1.6.0

jikes -version

- Jikes Compiler - Version 1.22 - 3 October 2004
Copyright (C) IBM Corporation 1997-2003, 2004.
- Licensed Materials - Program Property of IBM - All Rights Reserved.
Originally written by Philippe Charles and David Shields of IBM Research,
Jikes is now maintained and refined by the Jikes Project at:

Please consult this URL for more information and for reporting problems.

Very informative :)). Originally written by ... what does that mean ?



No 4. jikes provides some cool feature like incremental build.

I didn't get anything out of it.

Look at this feature.

jikes ++ TestForJDB.java

// After the 5 warning message.

Incremental: Enter to continue or q + Enter to quit:

nothing changed...

Incremental: Enter to continue or q + Enter to quit: (this if you change the code)

ok...

Incremental: Enter to continue or q + Enter to quit:

nothing changed...


No 5. clear error message.

I have removed one semi-colon from the code at line no. 25. Look at the error message printed by two compilers:

javac TestForJDB.java

TestForJDB.java:25: ';' expected
stringJDB = value
^
1 error

jikes TestForJDB.java

Found 1 syntax error in "E:/Program Files/Java/jdk1.6.0/bin/TestForJDB.java":

25. stringJDB = value
^---------------^
*** Syntax Error: ";" inserted to complete BlockStatementsopt

Are you able to find out something extra ? By the way what was the meaning of BlockStatementsopt ?


I guess I made too many of negative statement :), Ok some good features are here:

- it is providing some additional options like

> jikes +Punused-package-imports TestForJDB.java - which at compilation time, will show you those import statement which are not in use.

> jikes +Pnaming-convention testForJDB.java - it will throw warning if you are not following the naming convention ( its really an idea of Sun, don't mind :) )


Again, I didn't like the verbose information(javac -verbose filename.java) . Where is the time information man... parsing time, total time to make class file... these are some of the cool use of verbose option. Showing only which all files you are reading is a useless information.

Anyway, nice to see one step ahead in the world of open source.

Sunday, July 01, 2007

xjc.exe ?

Today I was looking some options in jdk/bin and suddenly I came across a file name xjc.exe in JDK6(I guess Mustang is no more official name of JDK6). Quickly I checked the existence of this file in JDK 5.0 and JDK 1.4.2. File was not present. Goggling didn't provided too much of help and I tried with myself :)

I just copied an XSD file from somewhere:

FileName: unknowntest.xsd (some problem with blogger, not able to take xml code :D, so pasting the image)










and passed this XSD file into xjc option:
Usage: xjc [-options ...] ... [-b ] ...

> xjc unknowntest.xsd

parsing a schema...
compiling a schema...
generated\Country.java
generated\ObjectFactory.java

Ah I got 2 Java files yeppp - Country.java and ObjectFactory.java
Look at these 2 files ( I am removing the documentation part of it, to make it small)

package generated;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"pop"
})
@XmlRootElement(name = "country")
public class Country {

@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected BigDecimal pop;

/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}

/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}

/**
* Gets the value of the pop property.
*
* @return
* possible object is
* {@link BigDecimal }
*
*/
public BigDecimal getPop() {
return pop;
}

/**
* Sets the value of the pop property.
*
* @param value
* allowed object is
* {@link BigDecimal }
*
*/
public void setPop(BigDecimal value) {
this.pop = value;
}

}

and objectFactory don't have too much in it. Just creating a Country class instance. Oh so it is something cool, its making all the getter and setter method for me, what else I need :).

Remember, such type of methods are too much in demand when you are writing any jsp page. So, if you are clear with the data type, just make an xsd file and xjc will convert in into java code.

I still doubt what xjc refers for ... may be xml to java converter :)

Friday, June 29, 2007

Java Debugger

Since there is lot of high class debugger available with GUI, we never bother to use Debugger provided by JDK itself. Actually I also get to use of this someday back. Luckily lot of things available to use and some good options but some error message really wired you. But good if you want to look into those dirty threads and stack calls. Anyways lets try for some basics.

I have written a small code to debug(really don't have anything in the code to debug :) )

public class TestForJDB {

String stringJDB;

public static void main(String[] args)

{

TestForJDB test4jdb = new TestForJDB();

test4jdb.setString("test for JDB");

String getstring = test4jdb.getString();

String returnString = test4jdb.toString();

}

public TestForJDB()

{

System.out.println("Testing for JDB ");

}

public String getString()

{

return stringJDB;

}

public void setString(String value)

{

stringJDB = value;

}

public String toString()

{

return stringJDB ;

}

}


Now lets start jdb. Remember compile with -g option so that class file will get the debug informations.


E:\Program Files\Java\jdk1.6.0\bin>jdb TestForJDB

Initializing jdb ...

>

Putting some breakpoints. We have 2 ways either by line number or by method name.

> stop at TestForJDB:4

Deferring breakpoint TestForJDB:4.

It will be set after the class is loaded.

> stop at TestForJDB:11

Deferring breakpoint TestForJDB:11.

It will be set after the class is loaded.

> stop at TestForJDB:16

Deferring breakpoint TestForJDB:16.

It will be set after the class is loaded.

> stop at TestForJDB:20

Deferring breakpoint TestForJDB:20.

It will be set after the class is loaded.

> stop at TestForJDB:24

Deferring breakpoint TestForJDB:24.

It will be set after the class is loaded.


Now lets go ahead and run the class file

> run TestForJDB


It will show you some crappy information. Anyway, lets try to look into the methods available:


main[1] methods TestForJDB

** methods list **

TestForJDB main(java.lang.String[])

TestForJDB ()

TestForJDB getString()

TestForJDB setString(java.lang.String)

TestForJDB toString()

java.lang.Object ()

java.lang.Object registerNatives()

java.lang.Object getClass()

java.lang.Object hashCode()

java.lang.Object equals(java.lang.Object)

java.lang.Object clone()

java.lang.Object toString()

java.lang.Object notify()

java.lang.Object notifyAll()

java.lang.Object wait(long)

java.lang.Object wait(long, int)

java.lang.Object wait()

java.lang.Object finalize()

java.lang.Object ()


main[1] step

>

Step completed: "thread=main", TestForJDB.main(), line=6 bci=0

6 TestForJDB test4jdb = new TestForJDB();


Something unique to see here, bci = 0, what does that mean ? Don't know actually. BCI is Byte code Instrumentation and probably help if you want to go for native level debugging.


list command will tell you the current position of the debugger.

main[1] list

2 String stringJDB;

3

4 public static void main(String[] args)

5 {

6 => TestForJDB test4jdb = new TestForJDB();

7 test4jdb.setString("test for JDB");

8 String getstring = test4jdb.getString();

9 String returnString = test4jdb.toString();

10 }

11 public TestForJDB()


locals, a useful command will tell you the Methods arguments and local variable at that break point.


main[1] locals

Method arguments:

args = instance of java.lang.String[0] (id=331)

Local variables:


There are many other options like dump, step up which are useful for serious debugging. Quite Strange to know that such a tool was introduced in JDK 1.2. If you want to look into the architecture of JDB then visit this site: http://java.sun.com/j2se/1.5.0/docs/guide/jpda/index.html


Saturday, June 16, 2007

Monday, May 21, 2007

Java FX ... The X Factor !

Ah, so one more scripting language. It seems that we are going more and more lazy in writing code. Anyway, I am all set for JavaFX programing. So far looking cool. Downloading NetBeans solves most of the problem.

Setting up for Java FX: Here. First time saw anything that can be done easily in Java(because I guess its not Java :) )

And my Cruel World Program,

import javafx.ui.*;

var window = new Frame();
window.title = "Title Bar";
window.width = 400;
window.height = 200;
var label = new Label();
label.text = "Cruel World";
window.content = label;
window.visible = true;


By the way where is the class. Sir this is the world of Scripting :). I guess a easier way to write code with more smoothness and less tension of inheritance, abstract funda.

So, trying for a code, in which I will take my photo and zoom it. So Nice :D. Will comeback with code in next blog.

Sunday, May 20, 2007

Searching Impl.

This is one bad implementation of the last blog. Need to change a lot of part to run in generic way, I am too lazy :)


import java.util.*;
public class SearchFile
{
public static void main(String args[])
{
try
{
String osName = System.getProperty("os.name" );
System.out.println(osName);
String[] cmd = new String[5];
if( osName.equals( "Windows Vista" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "dir *.java /s";
cmd[3] = ">";
cmd[4] = "Hello.txt";
}
/* else if( osName.equals( "Unix" ) )
{
find name filename

...

} */
Runtime rt = Runtime.getRuntime();
System.out.println("Executing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]+ " " + cmd[3]+ " " + cmd[4]);
Process proc = rt.exec(cmd);
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

Look at the search speed after first run. Its simple awesome. But it will create problem if user dont have permission to run a process.

Friday, May 11, 2007

How to get ?

I was trying to automate some of my manual work, I came across one problem. Is java providing any option by which if I give the file name and directory name it will go ahead and search that file in the given directory or sub-directory and return me the complete path of the file ? Unfortunately not :(, there is no direct API that can do this work for me. But Kannan told me that one can make a code which can recursively do this work. And he already had written some code for this. I was trying to tackle this problem with a different approach. The approach is something like:

On windows, dos gives me an option to search a file in a dir or sub-dir by command:
dir filename /s
Linux and Solaris gives me option of search the same by command: find /directory_name filename
So, why not using the runtime option of java system class and write a code like:

if ( OS == Windows) {get it by dir filename /s}
elseif (OS == Linux or Solaris or Unix){get it by find /directory_name filename}
else {God Knows only :)}
Right now I am not ready with code(because this dos is not giving anything directly, so need to remove useless things) but will come back soon with the results and the optimal code. Any suggestions are most welcome.

Saturday, March 31, 2007

Class file demand for which Java version

Someday back I got a call from my roommate's friend asking about that he has moved some class files from one system to other and now those class files are not running. I guess he got those class files from somewhere and he has no idea about which version of JDK it will run.

It's not such a tough job to find out that which version of JDK your class file is demanding. What we need to know is the format of class file and rest is like in your hands. Just look at the code for getting more clear picture.

import java.io.*;
class CheckClassFileFormat {
public static void main(String[] args) throws Exception
{
int magicNumber;
int minorVersion;
int majorVersion;
// and many more
DataInputStream data = new DataInputStream(new FileInputStream(args[0]));
magicNumber = data.readInt();
if(magicNumber !=0xcafebabe) // why 0xcafebabe.. read it, its so funny
{
System.out.println("Not a correct class format");
System.exit(1);
}
minorVersion = data.readShort();
majorVersion = data.readShort();
String version ="no version";

/*The Java virtual machine implementation of Sun's JDK release 1.0.2 supports class file format versions 45.0 through 45.3 inclusive. Sun's JDK releases 1.1.X can support class file formats of versions in the range 45.0 through 45.65535 inclusive. Implementations of version 1.2 of the Java 2 platform can support class file formats of versions in the range 45.0 through 46.0 inclusive.*/

if (majorVersion < 48) {
version = "jdk1.3.1";
} else if (majorVersion == 48) {
version = "jdk1.4.2";
} else if (majorVersion == 49) {
version = "jdk1.5";
} else {
version = "jdk 6";
}
// can follow up the same line for new releases.

System.out.println(args[0] + ": correct jdk version is " + version);
}
}
Just pass the classfile (like Test.class) in argument and we get to know on which JDK it need to run :)

Sunday, March 25, 2007

String vs StringBuffer vs StringBuilder

Let me first tell you what is StringBuilder. StringBuilder is a class analogous to StringBuffer added in JDK 1.5. This class is designed to use in place where StringBuffer is used by single thread(like in most of the cases). According to documentation, StringBuilder should work faster than StringBuffer. So " thread unsafe, fast".

I was reading one of the posts of orkut Java community asking "what is this capacity in StringBuffer and even we can add two strings from String class why to go for StringBuffer". Valid question ! GC need to work little more in case of String, but thats fair. As a java wellwisher let me try to do some publicity of Java API :D. And here it goes:

No don't use String class for concatenation operation, always use StringBuffer/StringBuilder and let me tell you why ?

This is a simple Java code for string addition in String and StringBuffer:

class StringTest {
public static void main(String[] args)
{
String s = "just a string";
s = s + "add me too";
System.out.println(s);
/*
StringBuffer s = new StringBuffer("just a string") ; //StringBuilder s = new StringBuilder("just a string");
s = s.append("add me too");
System.out.println(s);
*/
}
}

Alright, now have a look on the bytecode of this program(don't be panic).

>> javac StringTest.java
>> javap -c StringTest

Compiled from "StringTest.java"
class StringTest extends java.lang.Object{
StringTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.
":()V
4: return

public static void main(java.lang.String[]);
Code:
0: ldc #2; //String just a string
2: astore_1
3: new #3; //class java/lang/StringBuilder
6: dup
7: invokespecial #4; //Method java/lang/StringBuilder.
":()V
10: aload_1
11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/
String;)Ljava/lang/StringBuilder;
14: ldc #6; //String add me too
16: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)
Ljava/lang/StringBuilder;
19: invokevirtual #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
22: astore_1
23: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
26: aload_1
27: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
30: return

}


Just see line no. 11. Interesting, the plus sign we used for addition is not as innocent as it looks. String itself use StringBuffer(StringBuilder) to add two strings and hence taking much more time than normal append operation done by StringBuffer. Let me give you more evidence, run verbose option and check the time

>> javac -verbose StringTest.java

and check the other one, that is, with StringBuffer one.

You can clearly figure out the time difference and make a try with StringBuilder, time should reduce furthermore.

So, how is my publicity :D

Tuesday, March 20, 2007

Bug Patterns in Java

An attitude of solving the problem when it aroses is always be so called " a lovely" behaviour of Software Engineers and so I. Most of us love this and never bother to read book :) . Anyway with a half-hearted mind, I start reading a book "Bug Patterns in Java". In the first glance name of the book suprised me becasue I never before saw a language book which discuss only about bugs. I liked the title and started the book. Also, as java becomes a mature product, a maturity of something around 11-12 years, resolving any bug of this language is a nightmare's job.

Author,Eric Allen started with a very macro level saying every bug falls under a pattern and if not then go ahead and make a pattern for it :). Let me tell you about one of the most common patterns. One pattern call "Rogue tile", the cause of this bug is we do copy paste of code, bug get fixed in the orignal copy and we forget to fix it into the copy paste code. So funny, but yes this is one of the most common errors we used to do.

So far, so good. Hoping for something great out of it :)

Wednesday, March 07, 2007

JDK 1.7 - Whats New ?

I remember the exact date when Mustang got released. Mustang is going great because of it's deadly features like JS support, Annotations and many more.

Moving forward to JDK 1.7-Dolphin which has a expected release in 2008! Whats new ?

I am making a complete presentation on Dolphin and will upload it soon ! Proposals are:

1. SuperJAR's - this is something really cool

2. Co-bundling with other language interpreter like JRuby, Jython.

3. Closures in java - something in one line we can understand local variables for a function - kept alive after the function has returned

4. More enhancement with Java Persistence API

and many more ...

The new JDK comes into a new flavor of open source, so get ready >

Tuesday, February 27, 2007

Pattern Class

Finite Automata ! Remember ! We used to make a lot of Automata at college time. Making Pattern is one of the most tricky jobs and we need to take care of lot of things.Best Part of the whole story is JDK 1.4.2 onwards support Pattern Class and believe me its simply awesome.
So, I decided to make a check for Class name, if a class name starts with Caps its a perfect one else not as per Java coding guideline. So, here is my simple class which I need to check :

class box {
int width = 0;
int height = 0;
}

Now look at the checks (all these are valid )

class Box
class (space) Box
(space) class
Box
public class Box

So, Decided to make a pattern ".*class[ \t\n\f\r]+[A-Z].*" means anything before the String class is fine, there can be space,tab in between class and classname(more than one,so +) , classname should start with a Caps. I am not checking any other character except the first one, with the assumption that file is already a compiled class.

There can be a lot more optimized pattern and more you optimize,more you feel happy :). Here goes my simple,ugly written code that checks the coding standard(only for class name, and that even for one class only :D) box.java file.

See hoe easy to make a option say java -checkstandard which checks for the correct coding guideline in .java file. And if its not according to coding guideline, we can throw the error and why to throw the error, we can replace it with right name(right according to Java Standard). So the code will change

class box {
int width = 0;
int height = 0;
}

into

class Box {
int width = 0;
int height = 0;
}


// Here goes the code //

import java.io.*;
import java.util.regex.*;

class ClassNameChecker {
public static void main(String[] args)
{
String line;
String sub;
try {
BufferedReader in = new BufferedReader(new FileReader(" box.java"));
line = in.readLine();
Pattern p = Pattern.compile(".*class[ \t\n\f\r]+[A-Z].*");
Matcher m = p.matcher(line);
if(m.matches())
{
System.out.println("Class Name follow Java Coding Guidelines");
}
else
{
System.out.println("Not a Java coding Standard");
}
}
catch(Exception e)
{
//something something
}
}
}

Tuesday, February 13, 2007

Next Decade is challenging ...

Revolution in Science is most unlikely in last 2-3 decade. I guess adoption of Internet was one and the only revolution of last few decades. But if we consider optimization, we did a great job ! From maintaining diary to writing blogs, from railway counter reservation to e-reservation. Very well depicted by this youtube video of Web 2.0



But I have a gut feeling that in the upcoming decade we are going to see some of the revolutions. And here I get the first news: "First Quantum Computer Tuesday". British Columbia-based D-Wave will demonstrate the world's first quantum computer this Tuesday. D-Wave is doing a good job in making Quantum Computers commercial but in spite of hard work they are fighting with Financial optimization.

Saturday, February 10, 2007

File Transfer ...

I was looking at all the possibilites of transferring file from one system(source system(s)) to other(target system) through pure java code. Socket programming is one of the ways suggested by my friend. But it seems to be a tedious job... opening connection, sending request, closing connection. And the base line truth is that I don't know how to do these all :).

I was searching in Java Library for file transfer and I saw Java FTP client Library support. We can upload or download files from remote computer running an FTP server simply with some method calls.

Look at this code:

FTPClient objectClient = new FTPClient();
objectClient.connect("SystemName","username","password");
objectClient.download("D:\\Myfile\\","RequiredFile.txt");
objectClient.disconnect();

FTPClient Package was supported by Sun, its never be a part of jdk package and I guess it was deprecated now :((
Any simple idea to do file transfer ?

Friday, February 02, 2007

Dead or Alive !!

Ahh this time its cool. Again a good conversion but this time I guess Java is going pretty cool. Work is to write a script (no script, program :D ) which checks the status(Dead or Alive) of Network machines. Yes an easy job for scripting language. Believe me, much easy in java as well. And here it goes:

import java.io.*;
import java.net.InetAddress;
public class MainClass {
public static void main(String[] args) throws IOException {
try {
int timeout = 2000;
BufferedReader br = new BufferedReader(new FileReader("machine.txt"));
String machineName = null;
while ((machineName = br.readLine()) != null) {
InetAddress address = InetAddress.getByName(machineName.trim());
if (address.isReachable(timeout))
System.out.println(address + " is Alive");
else
System.out.println(address + " is Dead");
}
} catch (Exception e) {
System.out.printf("Unknown host");
}
}
}

Thanks to my team member to add this reading file code. If you remove file reading lines, code is not more than 4-5 lines. machine.txt contains name of the machines like
Machine1
Machine2
Machine3
Dabba4

I have compiled this code on JDK 1.5 but it should run on the lower version of JDK's as well (hopefully).

Tuesday, January 30, 2007

Running Javac from Java !

Oh Long time, No post .. So I decided to go for one :). I suspended the thread of JVM Part-N because some of my friends told that these things are also written in book, write something extra. For writing extra, I don't have knowledge :D

Anyway, as we all know Scripting language is the most powerful language for running real application and most of the time I get it challenging to convert that script code into Java code, but believe me I am always ready to take this challenge. So this time my challenge was to run set of Java codes from a single Java Program. In general we run java code by opening cmd and doing those javac,java.

Again, its a damn easy job for a scripting language, but ... Ah come on you are a java programmer. And here the result, though currently I am running one Java code only from other Java Program (sorry for writing the code in a bad way)

import java.io.*;

class JavaCode {
public static void main(String[] args1)
{
com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
File file = new File("V.java");
String filename = file.getName();
String classname = filename.substring(0, filename.length()-5);
String[] args = new String[] { filename };
int status = javac.compile(args);
System.out.println(status);
}
}

(Some bad naming convention, some crap hard coding.. but you can pardon me for that :D ). Some unused part of code like taking classname, I am going to use it further to run the code. Ok this code is compiling and telling the status of V.java ...some Helloworld code (please put both in same location :) )

Ok so next is to run and print Hello World ... Not a tough job :)

Sunday, January 07, 2007

JVM Internals – Part II (Space Organization)

(Click on the main title to go for Part I)

In my previous post, I forgot to write about JVM specification (mind it, it’s a specification, Sun implementation of this is HotSpot, there are many others). JVM specification is written by Tim and Frank (The JavaTM Virtual Machine Specification, this is a free download book written by them). This specification is basically written in three parts ...saying:

~ A set of instructions – Bytecode
~ A binary format – Class file format
~ An algorithm for identifying programs that can’t compromise the integrity of the JVM – verification.


Yes, so coming to the point. JVM is divided into 4 conceptual data spaces:

# Class Area – where the code and constants are kept
# Heap – home for object
# Java Stack – keep tracks on method invocation and data associated with that
# Native Method Stacks – Something to do with native method

Lets take an example … where is our lovely class Foo :)

Class Foo {

public static void main(String[] args)
{
System.out.println(“Hello World(s)”);
}
}

So tell me how many object we have?

We have 3 objects: args, out and Hello World(s). All goes into Heap.

Class Area:

Class: Foo
Method: main ( actually we have main method descriptor here)

Java Stack:

It has stack handling the push and pop of local variables like Hello World(s), out and args.

Native Method Stack: I am relaxing :)

Stack going up and down is a bogus story.

So, I am directly moving on Garbage Collector >>>

Saturday, January 06, 2007

JVM Internals - Part I

Beginning of New Year seems to be challenging for me. I am not getting enough time to recollect my ideas and write a blog on that. But in the end of last year, I decided to write blogs on JVM internals from very basic, so that a person who knows Hello World in Java can understand it. And here it goes:

Everything is sequential in this story and the most difficult is to find the entry point. Most of us are taking VM as a black box but looking inside this black box in not a tough job (neither an easy job).

VM is a machine dependent code that sacrifices itself to make java application machine independent. Lets first see the Hotspot into programmers perspective than latter we will analyze it on designer/architect perspective. Around 160 MB of this code written in C++ has 3 major parts:
~ Runtime Compilers
~ Garbage collectors
~ Everything else (JNI code, assembler code, oop (ordinary object pointer) code)

I will talk about all these later in my blog. So the folder structure goes something like this:
- vm
---- gc
---- runtime
---- compiler (communicates with other code of VM)
---- code (for Justin Time Compiler)
---- optimization
---- JNI (Java Native Interface to interact Java with c, c++ and vice-versa)
---- asm (assembler generated by VM)
---- oop (ordinary object pointers are java objects written in C++)
---- ...
---- ...

Understanding VM means understanding these parts. I will try to cover all of them in my upcoming blogs. In Sun, teams are dedicated for all these and believe me runtime people even don’t talk with gc people.