A few questions about Java2Script class loading

28 views
Skip to first unread message

Jacob Beard

unread,
May 26, 2009, 7:55:16 PM5/26/09
to java2...@googlegroups.com
Hi all,

I set up my j2s environment today using j2s from SVN as Zhou
suggested. My goal has been to determine to what extent GC might
already work in Java2Script (I'm assuming it will just work as a
stubbed-out API).

After trying out some simple examples, I decided to attempt to use
some of the simple Draw2d examples. I set this up by pulling
org.eclipse.draw2d and org.eclipse.draw2d.examples into my workspace
from CVS. I made them both Java2Script projects, and then put Draw2d
on the Java2Script build path for Draw2d examples. Happily,
Java2Script didn't seem to have any trouble building either of the two
projects, as I could see .js files corresponding to each .class file
in the build dir.

Unfortunately, when I attempted to run the HelloWorld example in
org.eclipse.draw2d.examples, it complained with the following error
message:

[Java2Script] Error in loading ../bin/org/eclipse/draw2d/FigureCanvas.js!
[Java2Script] Error in loading ../bin/org/eclipse/draw2d/Label.js!

I opened the top-level html file in Firefox using the file URI
handler, in order to debug it in Firebug, and it did indeed appear
that org.eclipse.draw2d.FigureCanvas was undefined. However,
FigureCanvas.js did appear to have been built correctly. It was my
suspicion that for some reason the j2s class load was not able to find
FigureCanvas.js. Unfortunately, when using the file handler, Firebug
does not active the Net panel, so I turned on a local HTTP server and
set the server root to my workspace.

What I discovered was that the class loader was attempting to load the
following path:

http://localhost/bin/org/eclipse/draw2d/FigureCanvas.js

Where it should have been trying to load:

http://localhost/org.eclipse.draw2d/bin/org/eclipse/draw2d/FigureCanvas.js

So, for some reason it was leaving off the project name. I assume it
was doing the same thing for the file URI. Does anyone have any idea
why it might be doing this?

I temporarily worked around this problem by setting a symlink in the
root of my workspace to org.eclipse.draw2d/bin. After that, it still
failed to pull in a dependency, but I believe this other problem was
different. Draw2d requires java.beans.PropertyChangeListener, and it
attempted to pull it in from j2slib, so:

http://localhost/apps/eclipse/plugins/net.sf.j2s.lib_2.0.0/j2slib/java/beans/PropertyChangeListener.js

But j2slib does not emulate this API. This is fine, and it shouldn't
be too difficult to wrap this by hand, but I'm wondering why the j2s
compiler did not catch it. Does the j2s compiler not do static
analysis on the code to catch unmet dependencies?

Also, when implementing the java.beans API, will it be necessary for
me to put this code in j2slib, or is there a way to put this in my own
project? I ask, because, GWT gives you a mechanism for implementing
your own JRE emulation (the "super-source" tag for their GWT modules).
They use this mechanism internally, and also expose it to users. This
allows you to build on the JRE emulation that they provide, in such a
way that it is not necessary to hack on the core. Does j2s provide a
similar mechanism to add to the jre emulation provided by j2s?

I'd appreciate any guidance you can offer on the above three questions. Thanks,

Jake

Zhou Renjian

unread,
May 26, 2009, 9:18:14 PM5/26/09
to java2...@googlegroups.com
Hi,

Adding codes similar to the following to the *.html may help:
        ClazzLoader.packageClasspath ("java.bean", "../your/folder/"); // load your own JRE emulation
        ClazzLoader.setPrimaryFolder ("http://your.absolute.path/"); // your bin folder

Or try to run your application using "Debug" button, or adding:
window["j2s.script.debugging"] = true;
may help you to identify which "*.js" is loaded or not loaded inside <HEAD> element.

Also the post
http://groups.google.com/group/java2script/browse_thread/thread/c157d8c8d66fd666
may help for your future debugging.

Regards,
Zhou Renjian

--
http://webuzz.im/ Web IMs (G/A/M/Y)
http://j2s.sourceforge.net/ Java2Script

Jake B

unread,
May 26, 2009, 9:28:37 PM5/26/09
to java2...@googlegroups.com
Cool, thanks for the tips regarding debugging and adding custom JRE classes.

I'm not quite clear on my other questions, though. Do you have any
ideas as to why j2s might be not be correctly loading cross-project
classes, as I mentioned in my example?

Also, does the j2s compiler do static dependency checking?

Please let me know what you think. Thanks,

Jake

Zhou Renjian

unread,
May 26, 2009, 9:37:06 PM5/26/09
to java2...@googlegroups.com
O, I missed some questions

For cross-project classes, you need to configure Java2Script's project references: ->Properties->Java2Script Builder->Add Projects

Class dependencies are not checked by compiler. ClassLoader do the checking job in the last step.


Regards,
Zhou Renjian

--
http://webuzz.im/ Web IMs (G/A/M/Y)
http://j2s.sourceforge.net/ Java2Script


Jake B

unread,
May 26, 2009, 10:05:49 PM5/26/09
to java2...@googlegroups.com
Hi Zhou,

Thanks for the quick response. You'll see in the example that I
gave that I set up the Java2Script Builder to reference the
org.eclipse.draw2d project in the way that you described.
Unfortunately, j2s manifested the unexpected behaviour I also
described. For your convenience, I've copy-pasted this description
here:


>> >> What I discovered was that the class loader was attempting to load the
>> >> following path:
>> >>
>> >> http://localhost/bin/org/eclipse/draw2d/FigureCanvas.js
>> >>
>> >> Where it should have been trying to load:
>> >>
>> >>
>> >> http://localhost/org.eclipse.draw2d/bin/org/eclipse/draw2d/FigureCanvas.js
>> >>
>> >> So, for some reason it was leaving off the project name. I assume it
>> >> was doing the same thing for the file URI. Does anyone have any idea
>> >> why it might be doing this?

I've drilled down on this a bit more, and I believe I've found
something interesting. Here's a snippet from the onload function
generated by j2s:

ClazzLoader.packageClasspath (["org.eclipse.draw2d.parts",
"$.widgets", "org.eclipse.draw2d", "org.eclipse.draw2d.text",
"$.internal", "$.geometry", "$.graph"], "../bin/");

I believe this is the code that sets up the classpath for
org.eclipse.draw2d. It appears as though it registers the classpath to
"../bin", where it should be "../org.eclipse.draw2d/bin". It seems
likely that this is what is causing the unexpected behaviour I
mentioned before.

To me, this looks like a bug, but, being unfamiliar with j2s, I'd
appreciate it if you could confirm this. If it is a bug, would it be
worthwhile to try a previous version of j2s?

Please let me know what you think. Thanks,

Jake

Zhou Renjian

unread,
May 26, 2009, 10:39:40 PM5/26/09
to java2...@googlegroups.com
Hi,

It is a bug. And there is no such bug on 2.0.0 release from my test.

I suggest you just modifying the "../bin/" to "../*/bin" instead of trying previous version, which is 7 months old.



Regards,
Zhou Renjian

--
http://webuzz.im/ Web IMs (G/A/M/Y)
http://j2s.sourceforge.net/ Java2Script


Jake B

unread,
May 27, 2009, 12:37:33 AM5/27/09
to java2...@googlegroups.com
Hi Zhou,

Thanks for confirming this. I'll try switching to the 2.0.0 tag
tomorrow and see if the bug persists.

Also, would you it be useful to you if I filed a bug report for this
on Sourceforge?

Please let me know. Thanks,

Jake

Zhou Renjian

unread,
May 27, 2009, 2:07:35 AM5/27/09
to java2...@googlegroups.com
Yes, we will fix the bug later.


Regards,
Zhou Renjian

--
http://webuzz.im/ Web IMs (G/A/M/Y)
http://j2s.sourceforge.net/ Java2Script


Reply all
Reply to author
Forward
0 new messages