Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How do I effectively use the "Scripting Engine" inside Java?
What are all the right use-cases to use the scripting engine?
Is there an open source project using "Scripting Engine"?
One thing comes to mind is "Closure, Functional programming" support is possible, but it is more of technical use than "Application Requirement".
Configurable plugins are OK. But still so many patterns (visitor, decorator) on high level can do the same.
I don't know the requirement well... how effectively it could be used in Java EE patterns... where it could complement with the existing patterns.
Moreover I would like to see more answers with some business usecases. Maybe like finding a complex discount for a product during sale based on membership or location. Finding ranking for a complex algorithm. Especially why not Java in some scenario? (or C# in .NET world)
In Java 6, scripting engine support is built in. For example,
// Create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// Create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("JavaScript");
// Evaluate JavaScript code from String
engine.eval("print('Hello, World')");
Why would you use one? Some reasons:
you have a library in a scripting language that you want to use in Java (e.g. a Python library that you could run via Jython)
You want to provide a configurable programming mechanism for customers, such that they can provide short code snippets. For example, I've done this in the past allowing customers to write filters using JavaScript (e.g. is x < 2 and y > 5 and z > 10 ?).
You can implement more complex logic in tools like Ant by scripting directly in the configuration file
You can implement solutions in a language more suited to that domain (e.g. using lambdas via Clojure), but maintain your reliance on the JVM.
Implementations include Rhino (a Java implementation of Javascript), Jython (a Java Python) and many more.
Here are some cases where I've used it.
1) Java wants to call scripting language, example 1. I have a Java app that accepts user comments via the WMD JavaScript widget. (Same widget that StackOverflow uses, actually.) User enters comments in the Markdown format and a JavaScript library called Showdown converts it to HTML in two places: (1) on the client, to support real-time previews; and (2) on the server, since I want the client to send pure Markdown to the server and store that there so the user can edit the Markdown later (instead of having to somehow reverse the HTML into Markdown). When storing the comment on the server, I do run the conversion there as well, and I store the HTML alongside the Markdown so I don't have to dynamically convert the Markdown when displaying comment lists. To ensure that the HTML on the server matches the HTML on the client, I want to use the exact same Showdown library. So I run Showdown server-side inside the Rhino JavaScript engine.
2) Java wants to call scripting language, example 2. I'm working on a deployment automation application that involves stakeholders across different roles, such as developers, sysadmins and release engineers. The overall app (workflow and UI) is a Java app, but at various locations it calls various scripts (e.g. Ruby, bash), such as for pushing packages, verifying configuration, installing packages, smoke testing, etc. This is partly because script is better/more economical for expressing directory creation, copying, moving, wgetting, etc., and partly because the people who own that particular piece of the pie know how to work with scripting languages but not Java. So we invoke scripts here using Java's Scripting API. Admittedly in this case we could just execute the scripts outside of Java but see #3 below.
3) Scripting language wants to call Java. In the aforementioned deployment application, we have web-based deployment logs, and we put a lot of effort into making the deployment logs as easy to read and understand as possible, because a large developer/SQA/release engineer population consumes the logs, and not everybody understands all the details of what exactly goes on with a deployment. Pretty-printing and color-coding are part of the approach. We implemented a pretty-printing deployment log API in Java but we want to be able to have the scripts call that. So for example when the Ruby push script runs, we want it to log its progress to the pretty-printer. Running Ruby inside JRuby allows the Ruby script to see the Java pretty-printer API.
"What are all the right use-case to use scripting engine?" This is a pretty vague question. There are many use cases. Here are just a few I can think of right away:
Plugin/extension system
IDE
Programming tutorial with live demos
I am assuming you are referring to JSR 223 in particular. If so, you should look at scripting.dev.java.net
I haven't used JavaScript specifically, but I've integrated Groovy into my application framework to provide a domain specific language (DSL). I've created functions and classes that hook into my application.
The user is allowed to script common operations within the application (macros) as well as implement lightweight processing to avoid the much heavier code-compiler-jar-deploy solution. If the user has an idea for a plugin to my processing framework they can prototype via Groovy in realtime and move back to Java (maybe even native) when there is time (or when speed is needed). Keep in mind that scripting is typically orders of magnitude slower than Java/C#/C/C++
Related
Is there any Java GUI available to develop java programs without typing single line of java code and using GUI mode?
No GUI in Java but you can use an open source content-management-system (CMS) example : Jahia, Hippo, Liferay, EXo Platform, Ametys, Magnolia, OpenCms ...
As I understand your question, you want the code to be generated without having to know the syntax and libraries of Java in a way similar that you can generate HTML based web pages using some CMS tools.
Let me ask you here: How would you define your business requirements in such a case using a tool?
There are tools which can generate quite a bit of Java code. e.g. IntelliJ can generate Data objects in a Hibernate/JPA based application if you can provide the data schema and a way to connect to the database, all of your favorite IDEs can generate boilerplate code like getters/setters/equals/hashcode etc.
Also, most of the modern IDEs can generate skeleton Java code and required configuration if you provide the frameworks you want to use like Spring, Hibernate, REST etc., but there is no direct way to generate business logic as there is no way for a tool to know what your business goals are.
I fear there isn't way to develop an application whitout coding. You could install a business-oriented applicazion like Liferay or other good CMS, but the question is always "Could this application do all you will need in future?"
I think if you have simple and flexibles needs you could, maybe related with web site world tring to use one of these solution could solve. In general I say You probably need to write some line of code anytime.
If you are thinking to some of "more custom" than a simple web site, or a content based application, try to eplore some scaffolding tool like play framework, that can do easily lot of things, reducing programming load.
I'm a pretty skilled java programmer that has dabbled in web development but I find that I'm much better at doing desktop based stuff than I am at anything related to web development. I've been trying to find an easy way of porting some of my desktop apps to run in browser but can't seem to find anything. I guess what I'm looking for is something similar to an applet but they a largely unsupported and get more buggy by the day. Is there anything similar that would allow me to keep my desktop style mindset and still run in browser or should I just break down and rewrite the whole thing in rails or another common web platform.
Java WebStart has been mentioned by others - It's a technology that aids redistribution of Java applications that then have the full rights of desktop applications, but they also have auto-update support built in. It's basically a launcher that fetches a JAR from the internet and runs it as a desktop application. These don't run within the browser.
Applets are an old technology that can be embedded directly into the web-page. They are not buggy, but they have several security restrictions. Also, the support is steadily declining because of the amount of critical bugs found in the technology. Desktop users that want applet support typically don't have trouble ensuring it, however. Currently, both the Chrome and the Java platform itself issue a warning before an applet is allowed to run - and that assumes the Java Runtime Environment is already installed.
Google Web Toolkit is a framework that allows creating single-page applications in Java, which are then compiled to Javascript. GWT handles multiple things behind the scenes, including server-client communication, localisation and internationalisation, and its own layout engine.
When translating an existing application to GWT, you need to:
separate the code into a part that runs on the client and a part that runs on the server. The server does not have direct access to the user, and the client does not have direct access to the database. If your application does not use centralised storage, it probably can run entirely within the web browser. Since client-server communication happens over the internet, you should reduce it to the minimum.
translate the front-end to GWT widgets. Forget Swing or AWT - they are impossible to compile efficiently to Javascript.
remove dependency on other Java classes that the GWT does not know how to translate into Javascript in the client part of the application. A large part of java.util. is supported but none of javax. (as of Jan 2014). The GWT site hosts the list of supported Java classes. Also, Javascript's regexes are less powerful than those of Java. Lookbehinds, in particular, are not supported. The server-side is a full-blown Java environment, but remember - you want to reduce the server-client communication to the minimum.
But, the most common strategy is to code the client side directly in Javascript.
Javascript is a language very similar in syntax to C/C++ and Java. It uses curly braces to denote blocks of code, and it uses semicolons to separate statements (though Javascript features automatic semicolon insertion, sometimes it understands two lines as a single statement if the first line is not terminated by a semicolon. Its data types include numbers (double-precision floating point), strings, booleans, two types of null, plain objects (which are basically hash-maps [string -> x]), arrays (untyped and dynamically extensible), regexes and functions (named or anonymous), all of which have their own literal syntax.
When coding in Javascript, your mindset should be:
Javascript is single-threaded and event-driven. You don't have to worry about concurrency issues, but you cannot say "now wait for x" either. Since Your Java code should be event-driven as well, this should not be an issue.
Lots of things in Javascript are asynchronous. Want to know something from the user? You should paint a dialog, and attach event handlers to its components. Want to get the user's GPS position? Ask for permission, passing it an event handler for when the user decides if the permission should be granted, from which you ask for the position, which also takes an event handler as an argument. Talking to the server? Asynchronous. Do you want to display something before doing a long calculation? You have to actually wait a little before you start computing. Ecmascript 6 improves the syntax a lot, but it's not yet supported in modern browsers.
Browsers only let you do so much. Disk access? Only to a file or folder the user explicitly points to. Clipboard access? The only reliable way is copy/paste into a textbox. Talking to a foreign webserver? Only if that webserver explicitly lets you (and lot of them don't even know how to). Of course, "foreign" includes a different sub-domain, different port number or a different protocol (http:// or https://). Desktop notifications? Geolocation? Ask for permissions first. Java applets have comparable security restrictions, and for the very same reason.
In Java, everything is a class. In Javascript, you can enjoy bare functions without any class. A typical event handler is just an anonymous function that you pass as an argument to a library function. Also, you can have anonymous objects using a very conscise syntax. This makes Javascript code much denser than that of Java and with very few classes, if any. Object Oriented Programming is still possible in Javascript, but much less pronounced.
When layouting your display, you need to think in terms of HTML and CSS. The best approach is to modify only the document structure (adding/removing elements or HTML classes) using the Document Object Model (DOM), and leave all CSS in an external file. In any case, you need to know CSS enough to be able to layout your page. Modern browsers support canvas, but it has no built-in layouting engine - its closest Java relative is JCanvas - just a blank area where you can draw graphics primitives - or a WebGL canvas - where you can place triangles in a 3D space.
When designing your own API, you need to know which operations might need to be asynchronous. If they are, either take a function as an argument (a callback), or return an object that does (a promise).
Except for the this variable, Javascript is function-scoped and lexically scoped and has closures. If a variable exists in a surrounding scope, it can be read from and written to - even from within a function that is only defined in that scope and called much later. In Java, you can't close over non-final function-local variables.
However, you need to be careful about timing - don't think you can just assign to a variable within a callback and use it outside. When you try to use it, it won't have been assigned to yet. Many have tried to cheat the time this way, and failed.
When the user leaves your page, it's a game over. If you want to remember anything past that point, you need to store it somewhere, be it cookies (very little space, outdated API), localStorage (decent amount of space, not supported by very old browsers) or the server (lots of space, but talking to the server when your page is being shut down is tricky).
the DOM API is often criticised, but there are several frameworks and libraries that ease the usage of it, of which the most popular is jQuery, which also handles browser inconsistencies, improves the AJAX API, event delegation (you can't attach an event handler to an element that doesn't yet exist) and includes an animation engine (though modern CSS is almost as powerful and often easier to use).
I think java web start could help you
http://www.java.com/it/download/faq/java_webstart.xml
I suggest you to take a look at Java Web Start. It offers you a possibility to start your application software for the Java Platform directly from the Internet using a web browser.
For more details see: Java Web Start
Nowadays Web Start is not a good option since, the user needs to have JVM installed and with all the vulnerabilities buzz around Java is more difficult to convince users to download it. The latest versions of JDK 1.8+ include scripts to pack your application along the jvm runtime in just one installer: https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/
For using your application in a browser like an applet, you can use Bck2Brwsr or TeaVM both can run java applications in a browser without Java Plugin. Bck2Brwsr also uses Java Plug in if it is available.
You can also use GWT to compile your Java application to JavaScript. Note: Swing is not supported.
Regards
Let's say I've got a Java/Scala backend and I want to develop middleware in Python for it. I have no experience with developing middleware, but I presume it means that from within Python I would need to issue commands to the running Java/Scala backend programs, and in turn receive feedback from that backend. For this to happen I need to interface the two languages and there are several options for interfacing Python and Java. According to these pages there are some options:
JPI
JNI
JPype
Jepp
Jython
JCC
Unfortunately, JPI, JNI, JPype and Jepp do not seem to be actively developed anymore (last update more than a year ago). Jython is still active, but I read that it is not possible to import all Python modules from within Jython. JCC seems to be some kind of C++ layer between Java and Python, which also seems counter intuitive.
I've also found some questions on SO about the topic, but most seem more than 3 years old, which in internet/Python land is of course a long long time.
Since I've got some experience with the ZeroMQ messaging library I could use that, but I'd have to adjust the Java programs for it (which wouldn't be that big of a trouble, but if there's a better solution..).
So my question is: taking into account that I have control over a Java/Scala backend, what is currently the best way to write a Python middleware layer for that backend?
You haven't given a lot of details about what you're trying to do. If your middleware runs in a separate process then it probably won't make a lot of difference what language your middleware is written in as long as they can talk to each other—and you can do that with ZMQ as you suggested.
If you're actually just making calls into Java/Scala libraries from a layer you're wanting to write in Python, I'd go with Jython. "Not being able to load all the modules" is only an issue if you were planning on using some of the modules that aren't available. Even for those modules that aren't available, you should be able to find suitable Java substitutes you can call.
Finally, if you really want to directly interface between Python and Java, you could look into Babel. I know some people who have used it for interfacing C, Java and Python programs successfully, and it seemed to work quite well for them.
Hi all this is my post on stackoverflow.
I am normally a lurker and find everything I need without posting but was drawing some blanks when trying to picture how this works. Maybe if someone can provide some insight I would greatly appreciate it.
So I understand the software development cycle and know a decent amount about code
(c++ and Java, and visual basic, I am a CIS major with a CS minor in my last year). I can write some programs in elipse using Java and I can even make some basic GUI components in eclipse using Java.
Now onto my 3 part question: I understand one could make a .jar from several .java files and distribute it, but while thinking about a common program like say yahoo messenger (just as an example) I came to the following three part question:
First, being could you write a program like yahoo messenger in just eclipse using Java or would you need other tools like other languages and other development environments, how does the project go from start to finish? (I think I am kind of lost here because I have only wrote command line and simple GUI applications).
Second, Say it was easier to create a feature in yahoo messenger using a different language, how can one write one program in more than one language? Say for example you wanted to write some code in python or c++ and your the majority of your code was in Java, ie your main method is java and you are compiling using JVM. (assuming you cant just stick python or c++ code in a java program) I did some Googling around and saw some things about linking the compiler and including native code to include other language code in a Java project. Links to other reading material is acceptable too if the explanation is too long.
Third- How does deployment work? Say I am done writing the code for my program and want to turn it into an .exe (for windows users) and stick it on my site for people to download. I know windows comes built in with an iexpress utility to create .exe's. Besides distributing a .jar how would one go about turning source into an exe? Thanks again for all your input and time. I am a beginner and trying to wrap my head around these concepts. The answers can be provided in a technical realm or just conceptual either is greatly appreciated.
-Mark
1) I most cases you can write your program in java without needing any other programming language. There are rather rare cases where you need to call a dll from java to interface with some proprietary program, for this you would need to use JNI and C or C++. A perhaps more common case for using multiple languages is for adding scriptability to your application. For example, my company offers a server/client application that is scriptable by users using Groovy, but the server and the rich client itself are written in Java only.
2) The integration of java and another programming language depends on the other language. Integrating Groovy is easy, and I think integrating Python (using JPython) or Ruby (using JRuby) is fairly easy. But it is an effort (not to mention the mental stress of programming in different languages) and I would not recommend doing that unless there's a specific requirement for this.
3) As always, there are several options. See how-can-i-convert-my-java-program-to-an-exe-file for creating a windows executable. Or you can create windows installer using e.g. NSIS. Or use Java Web Start.
If the intent behind this question is getting an idea how some big java rich-client (desktop) applications are written and deployed, I recommend the Eclipse RCP book. This book will walk you through the development and deployment of an XMPP/Jabber messenger client using the Eclipse RCP framework. Be aware though that there is no one true way of creating a big application and other java application frameworks do things differently.
Several IM clients are written in Java, though I would hazard a guess that the mainstream ones would be mostly written in C, C++ or (on the Mac) Objective-C.
Writing the one program in multiple languages has numerous challenges, and the nature of the challenges varies depending on the combination of languages you want to use. In many cases, you will probably not have much luck combining more than two languages. One set of impedance problems is bad enough, three is an almost guaranteed disaster.
You can avoid these problems by splitting a single application across multiple programs, each of which is written in a single language and communicates with the other programs via some kind of IPC mechanism.
Creating an "exe" is also a very language-specific concern. For instance, Java, C#, C/C++, and Python all have radically different deployment stories.
Say I am done writing the code for my program and want to turn it into an .exe (for windows users) and stick it on my site for people to download.
In that case, I'd say you were foolish. ;)
Java Web Start is a better option for deploying a Java based rich client app. from a web site. JWS works for any platform with Java.
What I'm looking for is a scripting engine for Java that would allow users to write simple scripts to control the behavior and events for a game. Something that:
is simple - something easy to pick up, especially for people with some basic programming/scripting experience
provides lots of control - I can easily start/stop/pause scripts and control how much execution time each gets, perhaps how much memory space they can use
is separated from the Java environment itself - No access from scripts to any Java objects or classes, only to those functions I explicitly provide
I've considered the Rhino JavaScript engine, and it would suit my purposes, but from what I've read (example), it's designed to integrate with Java so much that sandboxing it securely would be tricky. I'd rather start with an engine that gives scripts no access to anything by default, than have a fully open one that I have to close up. The scripts might not always be trusted, so the environment should not be easy to break out of.
I'd also consider developing my own language with something like ANTLR, but it's probably more effort than I want to put in.
Any suggestions?
Have you considered Lua?
Google docs preview of a pdf on the subject
Lyrio, G.H.S.O; Seixas, R.B.; Using Lua as Script Language in Games Coded in Java, Proceedings of The North American Simulation and AI in Games Conference - GAMEON-NA, EUROSIS, Montreal, Canada, 2008.
You should give a try to Groovy, a scripting language that easily integrates with the Java platform.
Its syntax is 100% compatible with Java, but it also has a simplified syntax that makes it a suitable language for DSLs implementation.
I don't know for sure if you can stop/pause the execution of Groovy code from a Java program, you should read the Groovy API.
When executing Groovy code from within a Java program, you can specify the context passed to the script and you can query the context modified by the script for output variables. The script can be completely isolated from the underlying Java environment by creating a GroovyShell with an appropriate CompilerConfiguration.
JACL is one such language. It is based on TCL. Whatever you do, don't invent another language. There are plenty of good choices out there.
In my opinion not only language, but way of interfacing is important. JSR 223 is most compatible between different languages, but "native" seems the best (i.e. full object integration of groovy)