From JS to Java? - java

I know GWT beeing able to code in Java and translate the cope on compilation to JS.
What about the other way around? Is there something that can translate JS to Java code?

You may want to take a look at Rhino.
From their site:
Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users. It is embedded in J2SE 6 as the default Java scripting engine.

Aside from Rhino (by Mozilla) there is also Nashorn (by Oracle) (which btw. means "Rhino" in German), which is an official JS implementation in Java.
It will be available OpenSource with Java8 in 2013.

Related

Is it possible to use Nashorn to convert Java code to Javascript?

Oracle recently released Java 8, which includes Oracle Nashorn as a JavaScript engine. Does Nashorn only works one way in that you can convert JavaScript code into Java bytecode? Or is there any way to use it to convert Java code into JavaScript code?
The official page of the Project Nashorn says:
Nashorn's goal is to implement a lightweight high-performance
JavaScript runtime in Java with a native JVM. This Project intends to
enable Java developers embedding of JavaScript in Java applications
via JSR-223 and to develop free standing JavaScript applications using
the jrunscript command-line tool.
So it's not the goal of the Nashorn projet to convert Java code to Javascript.
You can use instead the GWT compiler wich does that.
The GWT SDK provides a set of core Java APIs and Widgets. These allow
you to write AJAX applications in Java and then compile the source to
highly optimized JavaScript that runs across all browsers, including
mobile browsers for Android and the iPhone.
Beware that only a part of the Java Runtime Library can be emulated, you can find the list under this link.
gwt-exporter is also a useful tool for your purpose.
Develop an application or library in GWT and use gwtexporter
annotations to make classes and methods available from javascript.

How can Java code be quickly converted to .NET (specifically, ASP.NET)?

Are there any tools available to simplify the conversion?
What percentage of Java code is converted exactly to .NET code by these tools?
You can try IKVM. Saxon, an extremely popular Java XQuery/XSLT engine by Michael Kay, uses IKVM.
IKVM.NET is an implementation of Java
for Mono and the Microsoft .NET
Framework. It includes the following
components:
* A Java Virtual Machine implemented in .NET
* A .NET implementation of the Java class libraries
* Tools that enable Java and .NET interoperability
http://www.ikvm.net/
You could try this one
http://mayaf.org/janett/
Janett translates Java syntax, constructs and calls to Java libraries to C# / .Net counterparts.
Java is roughly equivalent to c#, so translating to c# should be relatively straightforward.
The path from a Java webserver to ASP.NET is less clear.
You might try to the Java Language Conversion Assistant from Microsoft. I think they may have dropped support, but now ArtInSoft seems to have a related product:
http://www.artinsoft.com/pr_jlca.aspx
There's also Octopus from Remotesoft:
http://www.remotesoft.com/octopus/
The issue with porting from Java is usually not so much the language itself, which is pretty similar to C#, as it is the libraries.
Of course, a web app will present an additional layer of complexity. If the app doesn't have too much code at the page rendering phase, it shouldn't be very difficult. If the page is riddled with code, then you may be better off re-writing it than trying to port directly.
Have you considered using J#? It's no longer supported but if you're just trying to get onto a .NET language, that might be the fastest path.

Advantages of using Rhino (mozilla's rhino)

I've been reading about Rhino as a useful way to implement JavaScript inside my Java code.
After reading for a while, searching in google and here, I couldn't find a real reason for which I would use JavaScript inside Java.
Could you point some real world advantages you find on using Rhino for a regular Java application?
Note that since Java 6, the scripting API is in the standard Java library - see the documentation of the package javax.script. The API in javax.script is an adapted version of Rhino. The scripting API supports not only JavaScript, but many other scripting languages.
See Java Scripting Programmer's Guide
The front page there mentions some reasons you might want to use scripting:
Application extension/customization: You can "externalize" parts of your application - like configuration scripts, business logic/rules and math expressions for financial applications.
"Command line" shells for applications -for debugging, runtime/deploy time configuration etc. Most applications have a web-based GUI configuaration tool these days. But sysadmins/deployers frequently prefer command line tools. Instead of inventing ad-hoc scripting language for that purpose, a "standard" scripting language can be used.
An example: You can script Oracle Weblogic using Python scripts, for example to configure your application server domain, to start or stop the server and to do other administration tasks.
Processing XML with Rhino and E4X is a joy compared to most of the Java XML APIs
You have existing JavaScript that "just works" and you don't want to re-write it. This can happen if you have some calculation or processing happening on the client side and move it to the server.
All kinds of neat caching and code loading tricks.
Your problem is better solved by a more dynamic language then Java.
List comprehensions ;)
The benefit of embedding a script language like javascript into your software is that you can offer others a way to write plugins for your software without giving the source code away.
A reason to write the whole app in another language is that you are able to write an app for a company which deploys it in their Java EE environment without having to learn Java.
We use JS (via Rhino) for a DSL in one of our products. It isn't a great DSL, but that is a fault of how we use JS, rather than Rhino.
If you considering to .Net also, I suggests to get along with Rhino.
Besides Java enviroments, rhino is one of the best performance interpreter on .NET environment (using IKVM). On the other hands, nashorn is very slow on .net.
Porting your own Rhino project to .NET is not so difficult.

scripting in java - javascript from a server-side class file in Java 1.5

I have three types of get requests that are delivered to a class file on web application from a mobile device. Because the mobile device provides no cookies, the log file hit only has
in.ter.nal.ip ser.ver.i.p:port 2009-06-05 09:14:44 GET /applicationname/mobiledevicexml reqtype=login&userid=xx### 200 87 - MercuryMobile/1.0 CFNetwork/342.1 Darwin/9.4.1 cookieArrayLength=0;
If I can instantiate javascript in my class file, and generate a javascript function call to urchinTracker() from inside the class file, I can replace that useless cookieArrayLength=0; with some useful data urchin can read from the log file into analytics reports.
We have been looking at scripting in Java with Rhino; Safari Bookshelf has:
Scripting in JavaTM: Languages,
Frameworks, and Patterns
which helped us immediately demo that we can run javascript in class files --this works out-of-the-box on Java 6.
Anyone know any resources for scripting with Rhino on Java 1.5 or 1.4?
Alternately, any suggestions for running javascript from java 1.5 would be appreciated.
The Java Scripting API (javax.scripting) package was introduced in Java 6, so that will not be available in Java 1.4 or 5. As the default installation, Java SE 6 comes with a stripped down version of Mozilla Rhino which is interfaced through javax.scripting.
However, Mozilla Rhino itself does not require Java 6. From the requirements page:
Recent versions of Rhino have only
been tested with JDK 1.4 and greater.
Older versions support JDKs as early
as 1.1.
Therefore, to use Rhino, it appears that Java 1.4 is actually sufficient.
As for resources, the documentation for Rhino seems to have a lot of information. In particular, the Embedding Rhino section might be useful to see how the scripting will work.
Of course, the lack of the javax.scripting package means that interfacing to Rhino itself is going to require the use of the Rhino API rather than the Java 6 native scripting API, but I would guess that the functionality is going to be fairly similar. The only downside I can see is, if in the future, Java 6 is going to be supported on the target platform and/or using another language, it may necessitate a rewrite to use the Java Scripting API rather than directly supporting Rhino.
[I'm posting in an answer, because I don't have enough points to post a comment on the question itself.]
Are you sure that the urchinTracker() function will operate outside of a web browser? Running the Rhino JavaScript interpreter (which isn't too difficult) won't be enough if the function relies on various browser objects, like the Document Object Model (DOM) or XmlHttpRequest.
I suggest that you at least scan the internals of the urchinTracker() function to see if this is the case.
See Server-side JavaScript for list of projects that runs JavaScript at the server-side.
For your usage, using Rhino seems like the way to go.

.NET, Java to JavaScript compiler

I am interested to create a drag-and-drop layout designer using only JavaScript, HTML and CSS. The designer will allow the user to drag the page elements from one place to another (something like Blogger's layout designer) to create a site layout. But I don't want to hand code everything in JavaScript, I would prefer to write my application in .NET (preferably) or Java and rely on a compiler to compile it to JavaScript and HTML.
What are the .NET or Java to JavaScript compilers that you have used and can recommend? For Java to JavaScript I know GWT is available. What about .NET to JavaScript? Microsoft did come out with Volta, but the project seems to be no longer available.
Look no further, you already mentioned GWT pick that!
It has a very good API and many good applications have use them.
Even JavaScript frameworks like http://extjs.com/ have GWT support.
I use it for an small JavaScript calendar recently.
To be honest, I don't really like JavaScript that much. Most of the times the errors are hard to track (specially for a non JavaScript guy as me) and the workarounds included some plug-ins for the explorer just to get exactly what a compiler should do. Catch silly error early.
In the other hand I'm very familiar with the Java Programming language, and many of the libraries (if not the most important) such as java.lang and java.util have been ported to GWT.
Plus, the guy who wrote relevant parts of java.util is the same behind GWT (google Joshua Bloch.)
Check out Nikhil Khotari's Script# project. It allows you to write C# code and compiles it to JavaScript.
Script# has already been mentioned. It hasn't been updated since August 2008.
Milescript is another, but also has seen very little for 6 months.
Extsharp for the Ext library. Adds Ext support for Script#
Javascript compiler to Java (going the wrong way for you)
Java to script Eclipse plugin
My issues with Script# (a known issue) is it doesn't support jQuery yet. However it comes with a very lightweight library to tie in with the .NET framework, in Nikhil's sscorlib.js file and ssfx.core.js files. And also has support for lots of other Javascript APIs (mostly Microsoft, seeing as he is in the ASP.NET team).
I'd love to see a Script# extension for jQuery (I'm thinking about writing it if it's easy enough). As it stands, most don't provide full compilation yet but they're certainly getting there.
Update: I wrote a small extension to enable JQuery support Script# a few months ago. The project can be found here.
I'm going to second the use of GWT. I've used it for several projects and, when used in combination with a modern editor like Eclipse or IDEA, it really makes the mess manageable.
It's important to note that not only does it allow you to write in Java and have that transformed into optimized and obfuscated Javascript it also comes with a substantial subset of the core Java API. In addition to this they provide lots of additional classes for doing things like parsing and working with JSON and XML and communicating with a server via asynchronous HTTP. You can check out the docs to get an idea of what else they offer.
Another feature that might be of special interest to you for implementing drag and drop functionality is it's integration with javascript libraries like Ext and scriptaculous. Either through pre-built interfaces or via JSNI
Also for Java there is J2S.
Java2Script (J2S) Pacemaker provides
an Eclipse Java to JavaScript compiler
plugin and an implementation of
JavaScript version of Eclipse Standard
Widget Toolkit (SWT) with other common
utilities, such as java.lang.* and
java.util.*. You can convert your
SWT-base Rich Client Platform (RCP)
into Rich Internet Application (RIA)
by Java2Script Pacemaker.
This means that if you use the SWT IDE (drag and drop) you can then convert the generated code to JS + HTML.
I wouldn't hand write any Javascript for UI. This can lead to maintenance disaster. jQuery is what I am using but I still wouldn't use it to write full UI Javascript code. ExtJS is also another good option if you plan to write in Javascript. In general what I am saying here is that it's so much easier to main in Java/C# than Javascript. Check out cappuccino framework and Atlas. Never used GWT. Script# is similar to GWT but for ASP.NET framework. Also depends on the requirement, if your site is public facing then RIA isn't a good option. It's all about which extreme end you pursuit (hand written and web standard, or RAD or libraries like jQuery/ExtJS as the middle option).
Check out Axial, a .NET to JavaScript converter that works well in ASP.NET. It supports WebForms, jQuery and canvas. It's not very mature, but it's worth a look.
http://jsc.sourceforge.net/ is a C# to JavaScript, Java, Flash and PHP compiler.
JscriptSuite offers another free .NET to Javascript compiler. There is a big difference to Saltarelle (jsc, SharpKit# etc.). Developer write down and debug only C# code (or any other .NET langauge), like in GWT. Javascript will be generated für deployment only.

Categories