Thursday Jun 18, 2009

New java verifier class file ver 50 onwards

While searching for something else, I chanced upon this article on the new class verifier. This gives more information on why version 50 is different and what are the improvement on it.


 Of course for the more keen here is a pointer to JSR-202.


Tuesday Jan 27, 2009

Elliptical Gradient Button in JavaFX

In JavaFX, we can make our own fancy buttons. Here in this blog, I tried to write an elliptical button. Have a look.



Here is the code :


package ellipticalbutton;

import javafx.scene.effect.\*;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.\*;
import javafx.scene.Scene;
import javafx.scene.shape.\*;
import javafx.scene.text.\*;
import javafx.stage.\*;

var x=70;
var y=40;

var el:Ellipse = Ellipse {
    centerX: 120
    centerY: 100
    radiusX: bind x
    radiusY: bind y

    effect: DropShadow {
        offsetY: 3
        color: Color.color(0.3, 0.3, 0.3)
        spread:0.4
    }//end effect

    effect:GaussianBlur{
    }
    fill:RadialGradient{
        centerX:0
        centerY:0
        radius:25
        proportional:false
        cycleMethod:CycleMethod.REPEAT
        stops:[
            Stop {
                offset: 0.0
                color: Color.ANTIQUEWHITE },
            Stop {
                offset: 0.3
                color: Color.GREEN },
            Stop {
                offset: 0.8
                color: Color.ANTIQUEWHITE }
        ]

    }//end fill

    onMousePressed: function( e: MouseEvent ):Void {
        x = x - 2;
        y = y - 2;
    }

    onMouseReleased: function( e: MouseEvent ):Void {
        x = x + 2;
        y = y + 2;
    }

}//end ellipse


var t = Text {
    font: Font {
        size: 20
        name:"Verdana"
    }

    x: 90
    y: 100
    content: "Press"
    fill:Color.DARKGREEN
    effect:DropShadow{
        offsetX:10
        offsetY:10
        color:Color.DARKGRAY
    }

}//end Text

Stage {
    title: "Elliptical Button"
    //style:StageStyle.UNDECORATED
    width: 240
    height: 320
    scene: Scene {

            fill:Color.LIGHTGREY
            content: [Group{
                    content:[
                        el,
                        t
                    ]

                }
            ]
        }
}

Tuesday Jan 20, 2009

Sun Tech Days 2008:An insider review.

SUN tech days 08 in hyderabad was fantastic.
A lot of engineers attended and this I guess was one of the largest gathering of Developers in India in 2008.


This was a great platform for Sun to showcase its technology in India which has a large IT base.
Although the center point of focus was java and netbeans, there were also another interesting sessions.
Sessions on the new sun studio, presenters from Intel and AMD were a hit.

The demo booths were always crowded and attractive sessions were buzzing with people.
Around this time SUN had acquired MySQl and that keynote by David Axmark was fanstastic involving a lot of passion.


But there were certain things that were striking otherwise as well.
A lot of in-session advertisement hurts and takes the focus out of technology to Sun marketing.
I am not saying this was not required, but overdoing it or bringing it into the very dynamics of technology is not welcome.
And as for the other keynotes, they were too long and doing overtime.


Another thing was that Sun kept focus on software side, while it's and irony of sorts that Sun makes money more from Systems than from Software alone.

Hardware demonstrations and session need to be added as well. A simple sun machine power needs to be brought out especially in a country like India, where the market is highly price sensitive but huge.
Technology like Sun ray and desktops like the Ultra 24 boxes should also be added to the set of demonstrations.


As for my own liking, the food was best. It is heartning to see that Sun does excellent managing a conference of this stature.
Kudos to those who made the last year a success. Keep the good work going.

Writing Java Script in Java.

A small code that can call JS code from within Java.
This feature is part of the jdk6 version.









01 import javax.script.\*;
02 import java.util.\*;
03
04 class Sample
05 {
06 public static void main(String[] args)
07 {
08
09 ScriptEngineManager manager= new ScriptEngineManager();
10 List<ScriptEngineFactory> factories= manager.getEngineFactories();
11
12 for (ScriptEngineFactory factory: factories) {
13
14 String name = factory.getEngineName();
15 String version = factory.getEngineVersion();
16 String language = factory.getLanguageName();
17
18 System.out.println(name+"("+version+"): "+language);
19
20 for(String n: factory.getNames())
21 System.out.println(n);
22 }
23
24 // JavaScript code in a String
25 String script = "function hello(name) { print('Hello, ' + name); }";
26
27
28 ScriptEngine engine = manager.getEngineByName("JavaScript");
29
30 try
31 {
32 // evaluate JavaScript code from String
33 engine.eval("print('Hello, World')");
34 engine.eval(script);
35 Invocable inv = (Invocableengine;
36 inv.invokeFunction("hello""THE");
37
38 }
39 catch (ScriptException e)
40 {
41 e.printStackTrace();
42 }
43 catch (NoSuchMethodException ee){
44 ee.printStackTrace();
45 }
46
47
48 }
49 }

Friday Jan 16, 2009

SUN tech days 2009 is here

Sun tech days 2009 is happening and registrations are open.


Sun Tech Days 2009 Flier

Getting PermGen information using jmap

The permanent generation is the area of heap that holds all the reflective data of the virtual machine
itself, such as class and method objects.
Configuring the size of the permanent generation can be important for applications that dynamically
generate and load a very large number of classes. If an application loads “too many” classes then it is possible it will abort with an OutOfMemoryError.
The specific error will look like :
“Exception in thread XXXX java.lang.OutOfMemoryError: PermGen space”

In 1 terminal start an application.
I am just using the jfc Notepad application here, since it is pretty passive and doesn't interfere with the screen while I am trying to write this.

bash-3.00$ ./java -jar ../demo/jfc/Notepad/Notepad.jar

Get the pid in this case 2239
bash-3.00$ ./jps
3131 Jps
2239 Notepad.jar
22305
10792 Main

Now in another terminal try running this

bash-3.00$ ./jmap -permstat 2239
Attaching to process ID 2239, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 14.0-b10
finding class loader instances ..Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
done.
computing per loader stat ..done.
please wait.. computing liveness.....done.

class_loader    classes bytes   parent_loader   alive?  type

<bootstrap> 1596 7131080 null live <internal>
0xe6e91088 1 1152 null dead sun/reflect/DelegatingClassLoader@0xb7847 8d0
0xe65142c8 6 17968 0xe6424958 dead sun/reflect/misc/MethodUtil@0xb7d67248
0xe6426528 0 0 0xe6424958 live java/util/ResourceBundle$RBClassLoader@0x b796ec78
0xe6e8a128 1 1152 null dead sun/reflect/DelegatingClassLoader@0xb7847 8d0
0xe656d8a0 1 1184 0xe65142c8 dead sun/reflect/DelegatingClassLoader@0xb7847 8d0
0xe693cd20 1 1792 null dead sun/reflect/DelegatingClassLoader@0xb7847 8d0
0xe641e710 1 8080 null live sun/misc/Launcher$ExtClassLoader@0xb79214 78
0xe6e8ee40 1 1152 null dead sun/reflect/DelegatingClassLoader@0xb7847 8d0
0xe6555668 1 1752 null dead sun/reflect/DelegatingClassLoader@0xb7847 8d0
0xe6e8cd78 1 1768 null dead sun/reflect/DelegatingClassLoader@0xb7847 8d0
0xe6424958 87 810144 0xe641e710 live sun/misc/Launcher$AppClassLoader@0xb7964f d0

total = 12 1697 7977224 N/A alive=4, dead=8 N/A


Typically, we get the output in this form.
For each class loader object, the following details are printed:
1. The address of the class loader object – at the snapshot when the utility was run.
2. The number of classes loaded (defined by this loader with the method(java.lang.ClassLoader.defineClass).
3. The approximate number of bytes consumed by meta-data for all classes loaded by this class loader.
4. The address of the parent class loader (if any).
5. A “live” or “dead” indication – indicates whether the loader object will be garbage collected in the future.
6. The class name of this class loader.

Thursday Jan 08, 2009

Garbage First: 3 Differences between CMS and G1

Garbage first is a new experimental collector introduced in the hotspot VM.
It is experimental since,it is not yet fully supported and still had some bugs which are being fixed.

CMS has been the defacto garbage collector for many for quite some time now.
Here's 3 main reasons why G1 is a bit better than CMS

1. Compacting
CMS in old generation does not undergo any compaction. During a full GC, CMS marks and sweeps (deletes) the objects that are not alive in the old generation. However, it does not collect all the objects towards one end of the heap. This results in fragmentation over time, following which the vm falls back to the serial gc to do a compaction. this is definitely a time consuming and long pause activity.
In G1, since the heap is organized into regions, there is no fragmentation and even if there is for a short period of time, it is limited to particular set of regions. The entire old generation is not affected.

2. Ease of use.
G1 has a lot less number of switches as compared to CMS, tuning your applications VM is simpler.
G1 is already present in jdk7 as of now and one can try it.
to use G1, these 2 switched need to be passed.
-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC

3. Predictable.
An inherent problem with CMS is that, it has a quite long pauses when it goes for a full GC.
The primary reason for this is that it has to scan the entire heap searching for live objects.
With the regions in G1, this scanning can be limited to a particular set of regions and hence the pause times can be made more predictable.

Friday Dec 19, 2008

Getting Java program info on Solaris 10

If you have a Java application running and want to get the program arguments in a simplest possible way on Solaris 10.

for example if your java process id is 18346

bash-3.00$ jps
56286 Jps
18346 Test
bash-3.00$ pargs 18346
18346: ./java -XX:+UseSerialGC Test
argv[0]: ./java
argv[1]: -XX:+UseSerialGC
argv[2]: Test

to get heap usage
Use ptools with egrep.


bash-3.00$ pmap 18346 | egrep "heap|total"
00032000 56K rwx-- [ heap ]
00040000 3840K rwx-- [ heap ]
00400000 86016K rwx-- [ heap ]
total 178056K

Friday Dec 12, 2008

Mixing of heavyweight and lightweight components

A new feature that will be part of jdk 6u12, will be heavyweight components and lightweight components will be allowed to mix in applications.
Until now, this was completely discouraged and not at all supported.
There is a lot of testing that is happening on this right now and not many P1's are being found.

Follow the bug, 4811096

Using jstat to get GC information

With Jdk 6, there is are a bunch of utilities that are useful in debugging and analyzing the application.
I will try to use jstat and try to gather some useful data about the application.

I am using Solaris 10 as my desktop platform, but this will work on any platform as is expected with Java.
A couple of uname gives me the following info.
SunOS 5.10 Generic_127111-07 sun4v sparc SUNW,Sun-Fire-T200


bash-3.00$ ./java -fullversion
java full version "1.6.0_05-b13"

A quick jps shows me this (For those who are not aware jps shows the java processes information running on your machine)


bash-3.00$ jps -l

56850 com.sun.javaws.Main
60209
50540 sun.tools.jconsole.JConsole
50854 sun.tools.jps.Jps

You could also do a jps -v to get detailed screens full of information if you need.


Let's try to monitor jconsole, for fun. This can be done on Java process.


bash-3.00$ ./jstat -gcutil 50540 1000 6
S0 S1 E O P YGC YGCT FGC FGCT GCT
0.00 0.00 16.00 0.00 78.15 0 0.000 0 0.000 0.000
0.00 0.00 16.00 0.00 78.15 0 0.000 0 0.000 0.000
0.00 0.00 16.00 0.00 78.15 0 0.000 0 0.000 0.000
0.00 0.00 16.00 0.00 78.15 0 0.000 0 0.000 0.000
0.00 0.00 16.00 0.00 78.20 0 0.000 0 0.000 0.000
0.00 0.00 18.00 0.00 78.28 0 0.000 0 0.000 0.000

As there is no activity on jconsole when I ran it, GC did not run at all.
Here is a list of acronyms used in the above output.
S0 Survivor space 0
S1 Survivor space 1
E Eden Generation
O Old Generation
P Permanent Generation
YGC Young GC Count
YGCT Young GC time
FGC Full GC Count
FGCT Full GC time
GCT Any GC time

Now, lets try to see some more.


bash-3.00$ jps -l
54327 org.netbeans.Main
56850 com.sun.javaws.Main
60209
55847 sun.tools.jps.Jps
54434 memory.MemoryConsumer
50540 sun.tools.jconsole.JConsole

I will try to monitor application 54434
This application generates new Java objects and also has an explicit call to GC in the code to run GC.


bash-3.00$ ./jstat -gcutil 54434 1000
S0 S1 E O P YGC YGCT FGC FGCT GCT
0.00 0.00 67.80 0.00 45.58 0 0.000 0 0.000 0.000
0.00 0.00 71.80 0.00 45.60 0 0.000 0 0.000 0.000
0.00 0.00 71.80 0.00 45.66 0 0.000 0 0.000 0.000
0.00 0.00 71.80 0.00 45.66 0 0.000 0 0.000 0.000
0.00 0.00 71.80 0.00 45.66 0 0.000 0 0.000 0.000
0.00 0.00 73.80 0.00 45.66 0 0.000 0 0.000 0.000
....
0.00 0.00 0.00 72.86 45.73 1 0.020 1 0.153 0.174
0.00 0.00 69.83 72.86 45.76 1 0.020 1 0.153 0.174
0.00 0.00 69.83 72.86 45.76 1 0.020 1 0.153 0.174
0.00 0.00 71.49 72.86 45.77 1 0.020 1 0.153 0.174
0.00 0.00 71.49 72.86 45.77 1 0.020 1 0.153 0.174
0.00 0.00 72.80 72.86 45.77 1 0.020 1 0.153 0.174
....
0.00 0.00 78.38 91.17 45.80 1 0.020 1 0.153 0.174
0.00 0.00 79.48 91.17 45.80 1 0.020 1 0.153 0.174
0.00 0.00 80.97 91.17 45.81 1 0.020 1 0.153 0.174
0.00 6.25 64.78 89.41 45.81 4 0.035 4 0.429 0.465
0.00 9.38 65.56 89.40 45.49 10 0.173 10 1.274 1.448
0.00 0.00 69.84 89.38 45.52 11 0.203 11 1.595 1.798


We can observe that over some time, the Eden gets filled up.
A GC call is done(in this case, explicitly for demonstration purposes) and the YGC count increases. Also, at some point of time Full gc gets called and the Full GC aslo happens.
It is evident that Young GC happens at a lesser interval than Full GC which is a reason generation GC is used in any virtual machine today.
G1 developers tried to do a new GC algorithm that didn't use generational heap, but soon realized it that it was not very useful.

Another small test, using -XX:+SerialGC gives me this statistics.


bash-3.00$ ./jstat -gcutil 61817 1000
S0 S1 E O P YGC YGCT FGC FGCT GCT
0.02 0.00 71.92 43.10 45.69 2 0.067 0 0.000 0.067
0.02 0.00 71.92 43.10 45.69 2 0.067 0 0.000 0.067
0.00 0.00 57.99 59.97 45.69 4 0.092 1 0.128 0.219
0.00 0.00 42.57 59.98 45.69 6 0.107 2 0.250 0.357
0.00 0.00 43.03 59.98 45.69 6 0.107 2 0.250 0.357
....
0.00 0.17 44.95 94.10 45.70 7 0.126 2 0.250 0.375
0.00 0.17 45.57 94.10 45.70 7 0.126 2 0.250 0.375
0.00 0.17 46.06 94.10 45.70 7 0.126 2 0.250 0.375
0.00 0.17 46.44 94.10 45.70 7 0.126 2 0.250 0.375
0.00 0.17 47.06 94.10 45.70 7 0.126 2 0.250 0.375
0.00 0.17 48.24 94.10 45.70 7 0.126 2 0.250 0.375
....
0.00 0.17 59.66 94.10 45.75 7 0.126 2 0.250 0.375
0.00 0.17 65.32 94.10 45.76 7 0.126 2 0.250 0.375
0.00 0.17 67.29 94.10 45.81 7 0.126 2 0.250 0.375

Wednesday Dec 10, 2008

Applet test

Now it works on Sun blog. Finally got it working.

Tuesday Dec 09, 2008

Java FX is out - javafx.com

Java FX was officially released last week.
The response has been huge in terms of numbers, leaving aside whether it was positive or negative.

I haven't been exploring Java FX much until recently. Of course it has been talked about since Java One 2007 and was also Sun's focus in 2008 J1.

However, now that it is stable, I did give it a try.
My first reaction was a positive one, although I would be the first to agree that there is whole bunch of things that needs improvement. Speed being a major concern, apart from certain other requirements that developers have suggested or criticized for not being included in it already.

A small sample that me and Vaibhav tried is here.
Of course, I did some work on this with Vaibhav that's why I am linking to it.

Wednesday Jun 11, 2008

Jar changes in jdk6

Consider 3 java files in the package pack1.


public class Main {

public static void main(String[] args) {
System.out.println("Main in Main.java");
}
}
public class Main2 {
public static void main(String[] args) {
System.out.println("Main in Main2.java");
}
}
public class Main3 {
public static void main(String[] args) {
System.out.println("Main in Main3.java");
}
}

Compiling all of them gives us the 3 files in the folder pack1.
Main.class Main2.class Main3.class

Now,
bash$jar cvfe main.jar pack1.Main2 pack1
added manifest
adding: pack1/(in = 0) (out= 0)(stored 0%)
adding: pack1/Main.class(in = 533) (out= 329)(deflated 38%)
adding: pack1/Main2.class(in = 537) (out= 331)(deflated 38%)
adding: pack1/Main3.class(in = 537) (out= 332)(deflated 38%)

bash$java -jar main.jar
Main in Main2.java

1 more run for the files without any recompilation or source change
bash$jar cvfe main.jar pack1.Main3 pack1
added manifest
adding: pack1/(in = 0) (out= 0)(stored 0%)
adding: pack1/Main.class(in = 533) (out= 329)(deflated 38%)
adding: pack1/Main2.class(in = 537) (out= 331)(deflated 38%)
adding: pack1/Main3.class(in = 537) (out= 332)(deflated 38%)

bash$java -jar main.jar
Main in Main3.java

Specifying the -e argument to the jar command we can specify which entry point we want in the jar file when running as the standalone application.
This has been added on in JDK 6 onwards.

Also, the timestamps of extracted files are those listed in the archive, instead of the time of extraction.
Eg for the same.
The current time is: 15:54:00.89
And the files information is
06/11/2008 03:46p 533 Main.class
06/11/2008 03:46p 537 Main2.class
06/11/2008 03:46p 537 Main3.class
The file information is the one when I created the class files.

Thursday Apr 03, 2008

Some 'javac' and 'java' options

Normally we use plain javac to compile all java files.

In this I will try to elicit some of the useful options associated with javac.
A normal compilation puts the class files in the same directory as the java files.

project1
|
|--------src
|        |
|        |----Class1.java
|
|--------classes

This seems to be a better structure for the same.
bash$pwd
/project1
bash$javac -d classes src/Class1.java
This will put the class file in the classes directory.
Also, 1 important point is that this command will take care of the package structure automatically.
The last thing about this is that if the classes directory didn't exist we will get a compiler error.
src/Class1.java:1: error while writing Class1: classes/Class1.class (No such file or directory)
public class Class1{
\^
1 error

Running with java
bash$java -cp classes Class1
Hello World

We are all familiar with this run.

Consider this.
bash$java -Dmyprop=myValue -cp classes Class1
Hello World

It still gives the same output. What's the difference?
To state in 1 line -D switch creates a system property with the name myprop and assigns the value myValue to it.
This is useful in running a single app over multiple environments with a little customized start up script.
In case you don't know system properties.
Run this
bash-3.00$ cat src/TestProps.java
import java.util.\*;
public class TestProps {
public static void main(String[] args) {
Properties p = System.getProperties();
p.list(System.out);
}
}

bash-3.00$ java -cp classes/ TestProps
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=/usr/jdk/instances/jdk1.5.0/jre/lib/s...
java.vm.version=1.5.0_14-b03
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=:
java.vm.name=Java HotSpot(TM) Server VM
file.encoding.pkg=sun.io
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/net/jpsesvr/space2/jpse/vikram/files...
java.runtime.version=1.5.0_14-b03
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
java.endorsed.dirs=/usr/jdk/instances/jdk1.5.0/jre/lib/e...
os.arch=sparc
java.io.tmpdir=/var/tmp/
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
os.name=SunOS
myProp=myValue
sun.jnu.encoding=ISO646-US
java.library.path=/usr/jdk/instances/jdk1.5.0/jre/lib/s...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Server Compiler
os.version=5.10
user.home=/home/va203678
user.timezone=
java.awt.printerjob=sun.print.PSPrinterJob
file.encoding=ISO646-US
java.specification.version=1.5
user.name=va203678
java.class.path=classes/
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=/usr/jdk/instances/jdk1.5.0/jre
java.specification.vendor=Sun Microsystems Inc.
user.language=en
java.vm.info=mixed mode
java.version=1.5.0_14
java.ext.dirs=/usr/jdk/instances/jdk1.5.0/jre/lib/ext
sun.boot.class.path=/usr/jdk/instances/jdk1.5.0/jre/lib/r...
java.vendor=Sun Microsystems Inc.
file.separator=/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=big
sun.io.unicode.encoding=UnicodeBig
sun.desktop=gnome
sun.cpu.isalist=sparcv9 sparcv8plus sparcv8 sparcv8-f...

Tuesday Apr 01, 2008

serialver or serialver.exe

In the ${JAVA_HOME}/bin/ folder there is a executable file serialver.

What does this file do?
This file is used to find out whether a class is Serializable or not.

Just use it like a normal java <classname>.

Look at the sample code below.
Obviously the same thing works on any other OS.

bash$pwd
/Java/jdk1.6.0_10/bin

bash$cat Class1.java

public class Class1 {

}

bash$cat Class2.java
import java.io.\*;
public class Class2 implements Serializable{

}

bash$serialver Class1
Class Class1 is not Serializable.

bash$serialver Class2
Class2: static final long serialVersionUID = 8006342768338701979L;

About

I am Vikram Aroskar working in the Java team based out of India.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today