Category Archives: JavaScript

Cappuccino and Objective-J

Today, I came up with 280 Slides, a web presentation design editor. I was shocked by its pretty looking Mac-like UI at the first glance.

From an interview to 280 Sliders developer, I learned to know that Cappuccino is the framework behind the product. And Objective-J language is introduced so they can port Apple’s Cocoa framework. Cocoa is a framework based on Apple’s own language Objective-C. In fact, 280 Slides’ UI looks like Apple’s Keynotes. And the 280 Sliders’ developers are former Apple developers. Cappuccino parses Objective-J files, which are with file extension .j, convert to JavaScript codes and executes them on the fly.

So, another cross compiler, Objective-C to JavaScript. It is quite worthy for further digging.

And digging into sources, I find that it uses lots of images to make up the whole UI, includes button, scrollbar, window borders and other components. And besides *.j and *.js files, there are some *.plist and *.sj files. *.sj use the same syntax as normal JavaScript. But from the sources, I guess they are compiled and mixed from *.j. Besides the core Objective-J.js, AppKit.sj, Foundation.sj and SlideKit.sj are three important files.

And it is great to hear the news that Cappuccino and Objective-J will be open-sourced at http://objective-j.org/ in the near future.

Posted in AJAX, JavaScript | Tagged , | Leave a comment

Java and JavaScript

Today, I came up with the idea of “Orto: Java in JavaScript“. Well, it is not “Java to JavaScript”, it is “Java in JavaScript”. It is said that java.lang.Thread is supported. But as there is no downloads available, and it is not open source, so I could not test it.

Supporting Thread programming in JavaScript would be cool. Once I spent some time trying to figure out how to support Thread in Java2Script project, but I failed. But I managed to program execute codes asynchronously by using SWT’s Display#timerExec(int, Runnable) or Display#asyncExec(Runnable) instead. So even though I can not use Thread#sleep, I still can do lots of thread things.

And Orto seems to be in a different way to support Thread, the codes it generated look like these:

case 117:orto246[orto247-2]={high:(~orto246[orto247-2].high)
&0xffffffff,low:(~orto246[orto247-2].low+1)&0xffffffff};
if(orto246[orto247-2].low==0){orto246[orto247-2].high++;
orto246[orto247-2].high&=0xffffffff;
orto246[orto247-2].low=0;
}break;

Compiling to Java code into byte-code like script, and loading and running these scripts in “JVM” should be a way to support all Java things. In my opinions, all locks and threads are re-implemented. Performance should be a problem. I know that there is another project called XML11, which is also trying to compile Java codes into JavaScript byte-code.

And recently, I also noticed that there was a debate about GWT Ext or Ext GWT. You may already know that GWT is about compiling Java to JavaScript and Ext JS is a HTML/CSS+JavaScript based UI library. It is interesting to see people combine these two technologies together. And it is more interesting that putting GWT before Ext JS and putting Ext JS before GWT is two different things. You may want ot read Didier Girard’s discussion for a clear idea.

About 2 years ago, when I started Java2Script project, I had to think about its programming model. At that time, people began to wrap existed JavaScript library in Java way. I called it “JavaScript for Java (JavaScript4Java)”, which is quite different from my “Java to JavaScript (Java2Script)”. So “2” and “4” is totally different, right?

In fact, I do know that Java and JavaScript are two different language. The reason that I chose Java as the source and JavaScript as the target is that Java is most beautiful and most used language while JavaScript is a world wide supported language. And I am familiar and love Java language. So I created Java2Script. I do believe that C#-to-JavaScript, Ruby-to-JavaScript or Other-to-JavaScript is not a big deal for me. But I may not have energy to make a similar compiler.

Java and JavaScript may be hotter and hotter. Good to see that more and more people is linking Java and JavaScript together.

Posted in Java, JavaScript | Tagged , , , | 1 Comment

Java2Script Gtalk on Opera Mini 4

It’s amazing to see that Java2Script’s Google Talk get run on Opera Mini 4 inside a mobile phone.

Here is Opera Mini 4’s simulator for you to test its CSS/JavaScript/AJAX compatibilities:
http://www.operamini.com/beta/simulator/

Here are screenshots:
opera-mini-j2s-gtalk-small.png

Zoom in to get a clear look:
opera-mini-j2s-gtalk-zoom-in.png

Login and connect:
opera-mini-j2s-gtalk-connecting1.png

Now we are in:
opera-mini-j2s-gtalk-logined-small.png

Zoom in to get a clear look on friend list:
opera-mini-j2s-gtalk-friend-list.png

But Java2Script Gtalk does not get run as totally correct. It fails to maintain normal connection back to Gtalk proxy server. So after a few seconds, the proxy server closes the connection. Maybe Java2Script Gtalk may fix this problem later. And also I think Opera Mini team may improve its AJAX supports in the near future so nothing need to be modified.

Posted in JavaScript, Opera Mini, Screenshot, Sharing | Leave a comment

Java2Script 1.0.0 Final Release

Here comes Java2Script 1.0.0 final release!

Please visit http://j2s.sourceforge.net/ for more information.

Feature List
==========

Java2Script Core
—————-
1. Object oriented JavaScript simulator is introduced
2. JavaScript ClassLoader is built inside
3. Eclipse JDT based Java to JavaScript compiler, support Java 1.4 and Java 5 features, including generic, enums, static imports, enhanced loops
4. Support incremental building for Java projects
5. Support Hotspot JavaScript swapping for debugging
6. Support most classes in java.lang.*, java.utils.* and others packages
7. Support native JavaScript through @j2s* Javadoc in Java sources
8. Support creating Java2Script projects by wizard, including Servlet project
9. Support loading *.js from Firefox Add-on.
10. Support Eclipse 3.1.*, 3.2.* and 3.3.*

Java2Script SWT
—————
1. Eclipse SWT 3.1 API is ported for JavaScript in modern browsers, Firefox, Internet Explorer, Opera, Safari
2. Window manager is built inside
3. Support UI blocking fow SWT’s Shell and Dialog
4. Support SWT deffered layout
5. Support detecting font size change in Firefox

Java2Script AJAX
—————-
1. HttpRequest/AJAX is introduced for both Java and JavaScript
2. Simple RPC API is introduced to call back Java server, which supports Cross Site Scripting
3. Support dynamic class loading for both Java and JavaScript
4. Support Java reflection (early implementation)

Java2Script JUnit
—————–
1. Support JUnit tests both Test Case and Test Suite in text mode.

Java2Script Example
——————-
1. Google Talk in JavaScript ( http://demo.java2script.org/gtalk/ )
2. SWT Control Examples ( http://demo.java2script.org/controls/ )

For more feature detailed description, please visit:
http://j2s.sourceforge.net/features.html

Posted in AJAX, Java, Java2Script News, JavaScript, RPC, Simple RPC, SWT | 2 Comments

Compressed JavaScript on Apache HTTPD Server

As Java2Script’s library size is getting huger and huger, loading *.js is a critical job for Java2Script. And I decide to output compressed *.js to save loading bandwidth.

In httpd.conf or .htaccess file, add the following:

<IfModule mod_deflate.c>
# Netscape 4.x or IE 5.5/6.0
BrowserMatch ^Mozilla/4 no-gzip
# IE 5.5 and IE 6.0 have bugs! Ignore them until IE 7.0+
BrowserMatch \bMSIE\s7 !no-gzip
# Sometimes Opera pretends to be IE with "Mozila/4.0"
BrowserMatch \bOpera !no-gzip
AddOutputFilterByType DEFLATE text/css text/javascript application/x-javascript
Header append Vary User-Agent
</IfModule>

You can read more about mod_deflate document.

In my experiments, I found that IE 6.0 had a strange bug in loading compressed *.js, so IE 6.0 was fed with huge raw *.js.

In normal bandwidth connection with Firefox 2.0, it takes about 16 seconds for the first time loading and running Java2Script application, and it takes about 6 seconds for later running (Cached *.js are applied). From the log file, there are about 20~30 *.js/*.css/*.png/*.gif hits, which counts up to about 150k. Thanks for the mod_deflate of Apache HTTPD Server. It saves a lot of bandwidth on those maybe 600k+ *.js files. And I considered such Java2Script application loading or running acceptable.

By the way, long waited Java2Script Milestone 5 release is coming. Lots of new features are added.

Update: For more details, please also read article “Compressing and Deploying JavaScript“.

Posted in Apache, JavaScript | Leave a comment

Reload:A Link An Application

Copy following as a link and paste in your browser address bar (A new tab or a new window, or other existed tab page[Maybe a Google search result page, not this blog page]):

javascript:if(a="$wt.examples.controlexample.ControlExample@http://demo.java2script.org/org.eclipse.swt.examples/bin/",window["ClazzLoader"]!=null)$w$(a);else{var d=document,t="onreadystatechange",x=d.createElement("SCRIPT"),f=function(){var s=this.readyState;if(s==null||s=="loaded"||s=="complete"){$w$(a);}};x.src="http://archive.java2script.org/1.0.0-v20061220/j2slib.z.js";(typeof x[t]=="undefined")?x.onload=f:x[t]=f;d.getElementsByTagName("HEAD")[0].appendChild(x);void(0);}

According to your connection speed, it may take a few seconds before some Java2Script things come up. Enjoy it.

Replace the link’s “$wt.examples.controlexample.ControlExample @ http://demo.java2script.org/org.eclipse.swt.examples/bin/” with your Java2Script application URL. It’s class’s full name + @ + the class’s *.js bin root folder. If the URL is not very long (total javascript url string length < 500), it will be OK for modern browsers to load your application.

Posted in A Link An Application, AJAX, ALAA, JavaScript, Sharing, SWT | Leave a comment

Extending Java2Script Compiler

Now, the java 2 javascript compiler of the java2script can be extended. There is a tutorial for demonstrating the extension mechanism.
This extension mechanism can be used to override default behavior of java2script compiler. So, why do we need to override the default behavior of the java2script compiler? Let’s have a simple example. Suppose an application that uses a library that can not be converted to java script, an ejb invocation:

EJBX x = EJBUtil.newInstance(EJBX.class, jndiName);

x.methodA();

Now if we use the default java2script compiler the code would be something like:

var x = foo.bar.EJBUtil.newInstance(….);

x.methodA();

But, we now that this will not work. It seems to be unsolvable before the extension mechanism is introduced. But now the java2script compiler can be extended to convert such these codes to another java script code that does work on a browser and also does not use the library.
Now if we extend the java2script compiler we can generate a stub invocation like this, that sould work without any ejb import or invocation:

ServletFacade.runEJBMethod(“foo.bar.EJBX”, “methodA”, jndiName);
//And the ServletFacade could be an stub for calling a servlet that can run EJBs

So, with this mechanism, one can convert any source code in his own way.

Posted in AJAX, Java2Script News, JavaScript | Leave a comment

Java-JavaScript Compiler Discussion

Ash mentioned Java2Script in the thread of “Java-JavaScript Compiler” in GWT’s group forum. And I joined in the discussion. I was not intended to post another GWT v.s. JavaScript post there (But it seems it was a v.s. thing actually). I just wanted to share things, correct my understanding and learn new things.

In those discussion, Bruce from Google told us that GWT’s motivation is “to be as small and as efficient as possible” and its two separate goals are:
(1) Create highly optimized JS output
(2) Optionally, publish selected parts of your code with a JS-compatible API
I thought that wais a very clever design. And I admired GWT team for its “excellent 100k ~ 200k final *.js files with very good performance”.
OK, if you are interested in Java to JavaScript technology, just go to GWT’s forum and ask GWT team for more details.

By the way, it seems that Java2Script website provides no detailed documents about Java to JavaScript compiler technology. I posted some details there in the discussion. Just check it out there. Or you can check sources from SVN for more details.

Posted in Java, JavaScript, Sharing | Leave a comment

LZ77 JavaScript Compressor Reloaded

Updated: June 4, 2007, LZ77 JavaScript Compressor http://demo.java2script.org/lz77js/ is rebuilt using Java2Script Simple RPC technology.

In early of this year, I had a post “LZ77-JS-Compressor: Another way of compressing JavaScript“. It seemed that lots of people were seeking pure LZ77 JavaScript Compressor, and some of them clicked the above post. And the above link is on the top 2 position for key world “js compressor” in Google.

As mentioned in the above post, pure JavaScript decoder’s performance was very poor for large *.js file (>200k)! So I stopped focusing on further development of LZ77 JS compressor after the post. So no technical details were found there but pure LZ77 JS compressor’s poor performance was told there.

But as top 2 position link for key world “js compressor” in Google, I decide not to disappoint visitors with no discrete implementations any more. Here now, I give an implementation (Thanks for J2S 1.0.0 M3, so such a solution does not require much time and pains):

LZ77 JavaScript Compressor“: This is a J2S/SWT application, sending the raw JavaScript sources back to server to do LZ77 compressing (Java servlet). (Mentioned: the server s eems being unstable with low bandwidth.)

J2S SWT LZ77 JavaScript Compressor

If you uncheck “RegExp trimming before LZ77” option in the above application, you can compress any plain text (HTML/CSS/SVG/…) besides JavaScript sources.

This LZ77 implementation was considered as buggy. I used in J2S Clazz.

BTW: Developing the above J2S/SWT application (including wrapping the old LZ77 utilities into a servlet) took me about 2 hours.

For JS compressor, I recommend you to use Dean’s Packer.

PS: “LZ77 JavaScript Compressor” is now using LZ77-JS-Comopresser compressed JavaScript: http://bl.ognize.com/j2s-ajax-rss-reader/net.sf.j2s.lib_1.0.0.v320/j2slib/j2slib.lz77.z.js (30k) rather than earlier http://bl.ognize.com/j2s-ajax-rss-reader/net.sf.j2s.lib_1.0.0.v320/j2slib/j2slib.z.js (55k)

Posted in AJAX, JavaScript, Sharing | 4 Comments

Java2Script 1.0.0 Roadmap

Road to Java2Script 1.0.0 release:

  • 1.0.0 M3 will be released at Aug 20, 2006.
    In this release, main features include

    • Full support of Eclipse 3.2.0 beside Eclipse 3.1.*
    • No more need of extra JDT core jar enhancement
    • Dynamic JavaScript loading
    • Early Java reflection implementation
    • JUnit support
    • CTabFolder, Table, Combo, Spinner, ProgressBar, Scale improved
    • Early key navigation support
    • Lazy layout improved
    • Support more look and feels
  • 1.0.0 M4 will be released at about Oct 9, 2006. In this release, all features should be locked.
    Coming features include

    • CoolBar, ToolBar, Menu, ColorDialog, FontDialog, more custom widgets improvement
    • Look and feel manager
    • Key navigation system
    • DND system improvement
    • Windows manager
    • Extension points for RPC calls (only interfaces with a very simple reference implementation)
    • Bug-fix of M3
  • There is no 1.0.0 M5 but will be 1.0.0 RC release at Nov 11, 2006
    Works to be done include

    • Dynamic JavaScript loading performance tunning
    • Packing system tunning
    • J2SClazz core packing and zipping
    • J2S SWT performance tunning
    • J2S SWT packing tunning
    • Benchmark system for Java2Script
    • More integration tunning
  • The final release of 1.0.0 will be released in later December, 2006, about one year after Java2Script is open sourced. All thing may depend on feedback of 1.0.0 RC.
    Works to be done include

    • More tutorials
    • White paper
    • Demo projects using Java2Script
    • Documents
    • Lots of XXX v.s. J2S to tell developers about advantages and disadvantages of J2S.
Posted in Java, Java2Script News, JavaScript, Sharing | Leave a comment