Sunday Dec 13, 2009

Graphs, GXL, dot and Graphviz

Sometimes you may want to quickly generate graphs programmatically and view/analyze those. Examples include, inheritance/type relation diagrams of an object oriented program, function call graphs and any other domain specific graphs (reporting chain of your organization chart for example). I find GXL very useful for this. GXL stands for Graph eXchange Language. It is a simple XML format to specify graphs. A simple graph stating that "JavaFX" language is related to "Java" language is as follows:

File: Test.gxl

<gxl>

<!-- edgemode tells this is directed or undirected graph -->
<graph id="langs" edgemode="directed">

<!-- write your nodes -->
<node id="java"/>
<node id="javafx"/>

<-- now write your edges -->
<edge from="java" to="javafx"/>

</graph>
</gxl>

You can also add number of "attributes" to nodes and edges - like color of the edge, style of the edge and so on. For example, "red" color can be specified for an edge as follows:


<edge from="java" to="javafx">
  <attr name="color"><string>red</string></attr>
</edge>

Now that we have written a simple graph with two nodes and a single edge between them, we may want to view it. There are number of tools/libraries to view GXL documents -- I've used Graphviz. Graphviz displays it's own native format called ".dot". Graphviz comes with a set of command line tools. One such tool is "gxl2dot", which as you'd have guessed, can be used to convert a .gxl file to a .dot file.


    gxl2dot Test.gxl > Test.dot

Once converted the .dot file can be opened in Graphviz GUI and we can export it to .pdf/.jpg/.png and so on. This way you can email the graphs to others and/or publish in your blogs/webpages easily.

The converted .pdf file for the above simple graph is here: test.pdf

I've used GXL graphs in a recent debugging tool related to JavaFX compiler. More on that later...

Wednesday Oct 21, 2009

Look Ma, javac tells me I am overriding static method wrongly!!


// File: SuperClass.java

public class SuperClass {
    public static int func() {
        return 0;
    }
}


// File: SubClass.java

public class SubClass extends SuperClass {
    public static boolean func() {
        return false;
    }
}

$ javac -fullversion
javac full version "1.6.0_15-b03-226"

javac SuperClass.java  SubClass.java 
SubClass.java:2: func() in SubClass cannot override func() in SuperClass; attempting to use incompatible return type
found   : boolean
required: int
    public static boolean func() { 
                          \^
1 error

The subclass uses a different return type for the same named method with same argument types. So, it is overloading SuperClass.func() and the overloading SubClass.func() differs only in return type. But, I am not sure of the error message....

Thursday Sep 17, 2009

"helloworld" with Maxine JVM on Mac

Last night, I listened to this nice podcast on Maxine JVM from Software Engineering Radio. Maxine is a Java Virtual Machine implemented in the Java programming language.

I wanted to try out Maxine on my MacBook Pro running Mac OS X 10.5.8. I followed these steps to try it out:

  • Get the Maxine sources
    
        hg clone https://kenai.com/hg/maxine~maxine maxine
    
    
  • Setup the environment
    
        export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
        export JUNIT4_CP=<full-path-of junit-4.4.jar>
    
    
  • Build Maxine
    
       cd $MAXINE_HOME/bin
       ./max build
       ./max image   
    
    
  • Run the "helloworld"
    
        ./max helloworld
    
    

Wow! Maxine VM printed "Hello World!". Now, it is time to explore Maxine's inspector and other cool stuff ...

Wednesday Sep 02, 2009

java.lang.NoClassDefFoundError: com/sun/javafx/runtime/FXBase

I created a simple JavaFX applet and compiled it with "javafxc" and created a jar "HelloApplet.jar". Then, I created a simple HTML file as follows:


<script src="http://dl.javafx.com/dtfx.js"></script>
<script>
           javafx(
           {
               archive: "HelloApplet.jar",
               draggable: true,
               width: 150,
               height: 100,
               code: "hello.HelloApplet",
               name: "HelloApplet"
           }
       );
</script>

When viewing the HTML, I did not see the expected applet content in it - just a gray rectangle panel :-( Then, I turned on the "Show Java Console" option. I saw the following exception trace in Java console:


JNLPAppletLauncher: static initializer
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: com/sun/javafx/runtime/FXBase
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
[.. more stack frames deleted for brevity ..]

Aha! com.sun.javafx.runtime.FXBase is new since JavaFX 1.2. Yes, I had compiled my applet with JavaFX 1.2 (full version "1.2.0_b233"). So, the JavaFX runtime used turned out to be old one! I changed the HTML script URL from http://dl.javafx.com/dtfx.js to http://dl.javafx.com/1.2/dtfx.js. The applet worked as expected :-)

Tuesday Jul 28, 2009

My twitter updates..

In the past, I blogged about Cricket (the sport and not the insect!) in this space. Once angry reader questioned me why I was writing that here. I guess people read my Sun blogs only tech. stuff. That is fine. So, I've revived my twitter login. I'll be posting my one liners (tech as well as non-tech) there. Longer posts on tech. (mostly Java, JavaFX) topics will continue here.

Tuesday Jul 21, 2009

Teaching programming to kids

I've been experimenting/playing with programming tools for kids. I've two kids - 8 and 5 year old.


Scratch

Great multimedia rich introduction to programming for kids. My son keeps playing with it! If you are after ease-of-use, this is probably the best choice. Available for download on Mac and Windows. If you have Squeak installed, you can get Scratch running on Linux as well (possibly with some audio problems). It is possible to export your Scratch projects as applets from your website

  • Website: http://scratch.mit.edu
  • Tutorials:
  • Examples/Repository: Register @ scratch.mit.edu to download lots of great samples! You can view scratch programs online if you have Java plugin installed.

EToys

EToys is an tool to teach children a multimedia rich authoring environment and visual programming system. If you have OLPC laptop (or emulation environment for OLPC), it comes pre-installed with EToys. Or else you can download EToys for your platform. It is avaliable for Linux, Mac and Windows.

StarLogoTNG

Although StarLogoTNG is described as "tool to create and understand simulations of complex systems", it can be used as a tool to teach programming. It supports 3D and it is cross-platform a (Java) tool.

Alice

This is an environment to teach programming in a 3D environment. This is a cross-platform (Java) tool.

Turtle Art

Turtle Art activity is bundled with OLPC. If you don't have OLPC, you can use an emulator environment. For example, you can download VirtualBox and run OLPC inside it following the steps here. I run VirtualBox on Mac, Ubuntu and Windows XP.

Want to work with source code?

So far, we looked at visual programming tools. But, if you think it is better to expose to source code ("they have to look at code at somepoint anyway, let them see syntax errors sooner"), then you may want to try these:


Greenfoot

From the site: "Greenfoot as a combination between a framework for creating two-dimensional grid assignments in Java and an integrated development environment (class browser, editor, compiler, execution, etc.) suitable for novice programmers". This is a cross-platform (Java) tool.

BotsInc

With Bots Inc we can learn how to program robots interactively. Language used to program is Smalltalk (BotsInc is built with Squeak). Since BotsInc is a Squeak image, it possible to slowly graduate to full Smalltalk. It is available on all platforms.

Alice 3 Beta

Alice 3 beta is hosted at http://kenai.com/projects/alice/. Alice 3 beta seems to have support to emit NetBeans projects. I've played with Alice 3 Beta - but not yet with NetBeans project support.

Tuesday Jul 14, 2009

BTrace "unsafe" mode

We kept saying BTrace is a safe, dynamic tracing tool for Java. To ensure safety, BTrace does not permit many constructs of Java language and also allows calls only to BTraceUtils class.But, not everyone likes safety -- sometimes developers want "unsafe" facility ("I know what I am doing"-style). We have got comments of this nature about BTrace - for example here and here. We have added "unsafe" mode to BTrace - you can edit "btrace" script to change -Dcom.sun.btrace.unsafe=false to -Dcom.sun.btrace.unsafe=true. In "unsafe" mode BTrace permits all Java constructs and also allows calls to arbitrary code. Like every other unsafe facility in programming, you are on own your own with this facility! You may get weird errors while instrumenting target classes and/or running transformed code. But then you know what you are doing, right?

Monday Jul 13, 2009

javafxdoc-style docs for Java code

You may have browsed JavaFX API docs generated by javafxdoc tool. javafxdoc tool is implemented as a doclet. It is possible to use javafxdoc's doclet to generate API docs for Java code.

Note: The XMLDoclet implemented as part of javafxdoc tool is an implementation detail and not part of official JavaFX/tool API and so please do not depend on this. This is more for fun/learning/personal use!)

I generated javadoc for BTrace source using the following command:


javadoc -docletpath $JAVAFX_HOME/lib/shared/javafxdoc.jar  -doclet com.sun.tools.xmldoclet.XMLDoclet -sourcepath .  -subpackages com.sun.btrace

where JAVAFX_HOME is the directory where JavaFX SDK lives.

A screenshot from generated docs:

Monday Jul 06, 2009

BTrace project moved to kenai.com

We have recently moved BTrace project to kenai.com. If you are interested in BTrace, please continue to use it and help develop it from kenai.com.

Thursday Feb 26, 2009

Prizes for having fun! Hurry....

Do you live in India? What do you want? Sony Ericsson phone or Canon Camera or iPod? If so, have fun writing JavaFX code and win any of these prizes!

Monday Feb 23, 2009

JavaFX interactive shell

JavaFX compiler has a built-in script shell - Per Bothner has implemented a read-eval-print loop facility for JavaFX. The script shell class is com.sun.tools.javafx.script.ScriptShell. Note:This is in the openjfx-compiler repository and not in the JavaFX 1.0 binary.

A sample JavaFX session is as follows:


$ java -cp dist/lib/shared/javafxc.jar com.sun.tools.javafx.script.ScriptShell 
/\*fx1\*/ "hello"
hello
/\*fx2\*/ 2 + 4
6
/\*fx3\*/ function greet (name) { println("Hello, {name}") }
/\*fx4\*/ greet("Sundar")
Hello, Sundar
/\*fx5\*/ var s = [ "Sunday", "Monday", "Tuesday" ]
[ Sunday, Monday, Tuesday ]
/\*fx6\*/ for (i in s) println(i) 
Sunday
Monday
Tuesday
/\*fx7\*/ class Person { public var name: String; }               
/\*fx8\*/ var p = Person { name: "Sundar" }
fx7$Person@13fba1
/\*fx9\*/ p.name
Sundar
/\*fx10\*/ import javax.swing.\*;
/\*fx11\*/ var f = new JFrame("hello");
fx11:1: cannot find symbol
symbol  : class JFrame
location: class fx11
/\*fx12\*/   
/\*fx13\*/ var f = new javax.swing.JFrame("hello")
javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=hello,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
/\*fx14\*/ f.setSize(300, 300)
/\*fx15\*/ f.setVisible(true)

While there is a generic shell called "jrunscript" for languages that implement jsr-223, it does not work very well for statically typed languages (which can be said about jsr-223 itself). But, the above mentioned JavaFX script shell is independent of jsr-223 interface.

Besides, jrunscript crashes for JavaFX!


$ jrunscript -cp dist/lib/shared/javafxc.jar -l javafx
fx> "hello"
Exception in thread "main" java.lang.NoSuchMethodError: com.sun.tools.javac.main.RecognizedOptions.getJavacFileManagerOptions(Lcom/sun/tools/javac/main/RecognizedOptions$OptionHelper;)[Lcom/sun/tools/javac/main/JavacOption$Option;
	at com.sun.tools.javac.util.JavacFileManager.(JavacFileManager.java:973)
	at com.sun.tools.javafx.api.JavafxcTool.getStandardFileManager(JavafxcTool.java:102)
	at com.sun.tools.javafx.script.JavaFXScriptCompiler.(JavaFXScriptCompiler.java:87)
	at com.sun.tools.javafx.script.JavaFXScriptContext.(JavaFXScriptContext.java:45)
	at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.getJavaFXScriptContext(JavaFXScriptEngineImpl.java:61)
	at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.getJavaFXScriptContext(JavaFXScriptEngineImpl.java:55)
	at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.parse(JavaFXScriptEngineImpl.java:220)
	at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.eval(JavaFXScriptEngineImpl.java:173)
	at com.sun.tools.javafx.script.JavaFXScriptEngineImpl.eval(JavaFXScriptEngineImpl.java:164)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
	at com.sun.tools.script.shell.Main.evaluateString(Main.java:280)
	at com.sun.tools.script.shell.Main.processSource(Main.java:249)
	at com.sun.tools.script.shell.Main.access$100(Main.java:19)
	at com.sun.tools.script.shell.Main$1.run(Main.java:165)
	at com.sun.tools.script.shell.Main.main(Main.java:30)

This is because JavaFX compiler code depends on modified javac classes. The code for jrunscript lives in tools.jar along with the unmodified javac classes! These unmodified javac classes are loaded which results in exception from javafxc code! We have to use the bootstrap path workaround as shown below:


$ jrunscript -J-Xbootclasspath/p:./dist/lib/shared/javafxc.jar -l javafx
fx> 233 + 334
567
fx> for (i in [0..4]) println(i)
0
1
2
3
4
fx> 

But, now we have a much better ScriptShell for JavaFX :-) We don't need to resort to this hack...

Friday Feb 20, 2009

A JavaFX compiler debug trick

There is an unsupported (read - can be removed in future without notice!) command line option with JavaFX compiler. If you run javafxc as


    javafxc -XDdumpjava Test.fx

the compiler generates intermediate Java code for your JavaFX program in "./dumpjava" directory (compiler expects you to create ./dumpjava directory before invoking javafxc). This is meant to be a debugging option for JavaFX compiler developers. But, you can look at generated Java code to see what happens behind the scenes - much like C++ programmers used to look at intermediate C code and/or preprocessed code.

JavaFX for Java, JavaScript programmers

I missed attending and speaking at Sun Tech Days at Hyderabad due to a personal reason :-( In fact, I prepared slides for a talk titled "JavaFX for Java, JavaScript programmers". This is much like my earlier language comparison blog entries such as Java, JavaScript and Jython, Java, Groovy and JRuby etc. The idea is to learn a language by language comparison - and not to conclude "better"/"worse" language and so on. So, no politics please :-) Although I could not attend Sun Tech Days, I am posting the slides here : slides in a .pdf file

Tuesday Dec 23, 2008

Installed Ubuntu 8.10 on my PS3

Ubuntu on PS3

I wanted to install Ubuntu on my PlayStation 3.


My Setup

  • PS3 is NTSC 60 GB hard disk version - updated with firmware version 2.42.
  • PS3 is connected to 32 inch 720p Sony LCD TV via HDMI.
  • PS3 is connected to wireless network.

Stuff needed in addition to the above

  1. USB keyboard.
  2. USB mouse.
  3. CD burned with Ubuntu powerpc iso (ubuntu-8.10-alternate-powerpc+ps3.iso).

Preparing PS3

  1. Backup your PS3 hard disk using [Settings] -> [System Settings] -> [Backup Utility] menu. I didn't bother to backup the hard disk.
  2. Go to [Settings] -> [System Settings] > [Format Utility] menu.
  3. Select [Format Hard Disk] and click [Yes].
  4. Choose [Custom] and [Allot 10GB to the Other OS].
  5. Select [Quick Format] and confirm with [Yes].

Installing Ubuntu

  1. Connect USB keyboard and mouse to PS3.
  2. Insert the disk with Ubuntu iso image into PS3.
  3. Go to [Settings] -> [System Settings] > [Install Other OS]. PS3 will detect the install CD and copy files and instruct you to ..
  4. Select [Settings] -> [System Settings] -> [Default System] -> [Other OS]. This will boot PS3 with other OS. From then onwards, follow the Ubuntu installation instructions.

Small hiccup

The installation was smooth except for one small issue - the installation seemed to hang in "Select and install software" step. After 6% the progress bar did not increase at all! Fortunately, this seems to be a known issue with text mode installer. Please refer to Ubuntu 8.10 release notes and bug 290234. I pressed Alt-F4 and Alt-F1 to toggle between logging console and main screen to check the progress. Eventually, the installation completed! While installing I configured network as well -- i.e., giving WEP password etc. -- not sure if this is mandatory, but in my case I have wireless connectivity and so I supplied the configuration values for the same.


Switching between Operating Systems

  • From Ubuntu to Game OS, use the command
    
        sudo boot-game-os
    
    
  • From Game OS to Ubuntu, use the menu
    
       [Settings] -> [System Settings] -> [Default System] -> [Other OS]
    
    

Few Screenshots

Wednesday Dec 17, 2008

Debugging option for javac and javafxc

I work on JavaFX compiler these days. The command line (debugging) option that I often use is -doe ("dump on error"). This option prints stack trace of the compiler when error message is printed. NOTE: This is an internal option and can be removed any time without notice! But, it is useful for debugging. This option works for javac as well as javafxc. When I misspelled "class" as "clas" and run compiler with -doe option, I got the stack trace below:


$ javac -doe t.java
t.java:1: class, interface, or enum expected
clas t {}
\^
java.lang.RuntimeException
	at com.sun.tools.javac.util.Log.writeDiagnostic(Log.java:565)
	at com.sun.tools.javac.util.Log.report(Log.java:523)
	at com.sun.tools.javac.util.Log.error(Log.java:404)
	at com.sun.tools.javac.parser.Parser.reportSyntaxError(Parser.java:282)
	at com.sun.tools.javac.parser.Parser.syntaxError(Parser.java:267)
	at com.sun.tools.javac.parser.Parser.classOrInterfaceOrEnumDeclaration(Parser.java:2206)
	at com.sun.tools.javac.parser.Parser.typeDeclaration(Parser.java:2180)
	at com.sun.tools.javac.parser.Parser.compilationUnit(Parser.java:2126)
	at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:509)
	at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:550)
	at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:801)
	at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
	at com.sun.tools.javac.main.Main.compile(Main.java:353)
	at com.sun.tools.javac.main.Main.compile(Main.java:279)
	at com.sun.tools.javac.main.Main.compile(Main.java:270)
	at com.sun.tools.javac.Main.compile(Main.java:69)
	at com.sun.tools.javac.Main.main(Main.java:54)
1 error

About

sundararajan

Search

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
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder