Can groovy run javascript? - javascript

I'm a newbie in groovy and javascript. I'm trying to make a groovy script that runs a javascript in it.
Is it possible for groovy to run a javascript on its class? If it is possible, then how can a groovy script run a javascript?
Some sample syntax will be a big help!

It depends on the javascript code you want to run.
If your javascript only involves some basic operations etc, you could use ScriptEngieManager, http://docs.oracle.com/javase/7/docs/api/javax/script/ScriptEngineManager.html
Just include the library at the beginning of your groovy script:
import javax.script.*
If your javascript code involves DOM manipulation and/or stuff that has to do with a browser to function properly, you'll need a java-based browser like htmlUnit: http://htmlunit.sourceforge.net/

Related

Java/Python Cross-Compiling in JavaScript

I am working on a project that involves having a Java code editor and a Python code editor but I have no way of compiling that code. I've used Ace within my html and I have no idea how to compile the Java code or Python code using JavaScript. I currently have a way of retrieving all of the Java/Python code to a string. From there I need a way of compiling that code so that I can run it on my website and test it. There will be no GUIs involved, all of the Java/Python code will just have console output. However, I need a way I can run the Java/Python code on my website in live speed. Everything must be done on the website, the client shouldn't have to download anything extra. I am basically trying to replicate the website 'codingbat.com'. Thanks for the help in advance.
Python would usually get JITted when you invoke a good Python interpreter (ha, the name does not apply). Java has some great compilers (standard javac or the Eclipse JDT) so I would not even think of compiling with some other language.
Where does the need come from to cross-compile in JavaScript? It sounds like you are searching for a Java compiler implemented in JavaScript, and likewise for Python.
If everything has to be built and deployed to some website, why don't you create a script (shell, JavaScript, or a Jenkins Pipeline) that compiles the java part using javac, precompiles the python part as required and deploys the output directly to your website?

Javascript frameworks just "markup" for javascript?

I am a little curious about how javascript frameworks work. Web development isn't really my area of expertise (I'm more of a c/c++ guy), but do javascript frameworks get translated into vanilla javascript?
Upon inspection of website source, it seems like it is mostly just standard javascript. Do these javascript engines just translate code into javascript on the server side?
Yes, most of JavaScript Frameworks translates the code you write to vanilla JavaScript, however, this does not happen on the Server Side, that would be really slow (Server side code is used to check databases, serve files, authenticate, etc.). This process of translation is done in compilation time (Although it is translation). (Just like when you compile c++ code into binary).
When it's source code to source code like JavaScript and React (JSX) to Vanilla JavaScript (JS), it's translation. When it's source code to binary like C++ source code to an executable (.exe) is compilation.
After you're done writting your JavaScript code with frameworks, you most translate it to Vanilla JavaScript (if you also used other uncommon languages to write styles, you must translate them too, like SASS instead of CSS). It is also common to minify it, so it can load faster.
All this is mainly done by tools like webpack.
When your site is up and running, we can say that is run time.
Looking at the fact that they were written in js they would be resolved to js before running and as Robin said they are executed on client side except Node which is a runtime environment and not a framework

Writing code for JS and Java

I'm trying to write a module of code which can be used with Javascript for the client side browser and Kotlin for a Java desktop application and Android app.
The main logic manipulates a bitmap/png file.
Is there a way which I can write an interface to be used and have different implementations of the interface for JS and Kotlin?
For example, write a wrapper class for an image (load from image, set pixels, get pixels) in Java (using BufferedImage) and JS (using Canvas)?
I'm new here so if anything doesn't make sense or needs more clarification, please let me know!
Yes, it's certainly possible to do that. This answer is a generic Java and JavaScript answer; there may also be a Kotlin-specific approach, see the link in zsmb13's comment. But the generic Java and JavaScript approach would be:
Write your interface around BufferedImage in Java
Write your interface around canvas in JavaScript
Write your manipulation code using that interface in JavaScript
On the browser, your manipulation code would run directly on the browser's JavaScript engine
In the Java app, you'd run your JavaScript code using javax.script to run it in Nashorn (or Rhino on older Java environments).
Whether it's the best solution to your problem is something only you can decide, but yes, you can do it.

Runnning python script on HTML page

I would like to basically call a python script from HTML, after the script is called and it finished running, I would like to execute a javascript file(wich I know how to do.) Now my question is: Can I do this with just pure HTML and javascript or do I need to get a library for python? If I dont need a library, how would I go about doing this?
You can use Two Python libraries.
Django
Flask
I recommend Django. Django is easy and fast to make.
the Flask is more complex but you can make more detail functions.
Can I do this with just pure HTML and javascript?
You might want to execute python on the browser, then look at something like this http://www.skulpt.org/, but the most common use case is that no, you need to execute javascript on the browser client-side and python on server-side
So no, you need something that executes python in a web server (apache/nginx) context, like
gunicorn (standalone/wsgi)
uwsgi (standalone/wsgi)
mod_python (apache module)
Then you would execute like this for example
browser ---http---> server(nginx) ---wsgi---> python_server(uwsgi)+library(Django)
<-------------/ <------------/

how to embed groovy script in html like in javascript?

is it possible to embed groovy script in html like in javascript?
I want to have an onclick button event that use groovy script instead of javascript
Thanks
You can't
You could use AJAX to call the webserver which would run a groovy script, but you can't run groovy in the browser like JavaScript.
(For completeness in this answer, I guess you could roll an applet with groovy support, and then use the applet to execute bits of groovy, but I would just write the script in javascript and miss out this sort of Rube-goldberg machination)
It's a rather old project, and I'm not sure quite how far it actually went, but Andres Almiray has done some work on getting the ability to run Groovy in the browser, via Grapplet.
I haven't used it myself, but it looks promising.

Categories