Groovy, Scala, Clojure, etc scripts in Android - java

I am working on some Forms and Report Builder written in Java which can run on Android as well. At least the Forms and Reports executer. Users can enter simple groovy scripts, which can change simple variables. Now my problems is that groovy won't really work on Android. Is any way that I use instead of Groovy another language.
Requirements :
- that language should be compiled and executed in Java
- pass and read variables to the scripts
- the resulted bytecode should run on Android as well, or eventual directly compiled and executed in Android

Besides tim's variant(SL4A), there are several more java vm's for existing scripting languages.
Be carefull, not every one will serve your purposes, as Android hasn't JVM, it has Dalvik VM instead, wich works differently, and pack and optimise java bytecode, when some JVM scripting languages relay on dynamic class files compilation and execution.
If your tasks are simple, consider Lua. It's an extremely lightweight scripting language, and it has a 2 working pure-Java vm implementations, which will go on android:
http://sourceforge.net/projects/luaj/
http://code.google.com/p/kahlua/

Version 2.4.0 of Groovy will have Android support.
At the time I'm writing this, the current version is 2.3 and there's still no public beta of 2.4, but you can try right now by building the snapshot like this:
$ git clone https://github.com/groovy/groovy-core
$ cd groovy-core
$ ./gradlew clean dist
the generated library to be included in your Android Studio project will be target/libs/groovy-test-2.4.0-SNAPSHOT-grooid.jar.
Then you'll need to modify your gradle.build file to include Groovy support; this system looks still in early stages, so please refer to this page for further info: http://melix.github.io/blog/2014/06/grooid.html

Related

Testing for Java SDK

I am writing an application in Java. Does a Java SDK have to be installed to run the application from the command line? If so, can I package the SDK with the application to be installed when installing the application?
From Java 9 onwards you can use jlink to produce a custom JRE for your application. The JRE includes a copy of the java command and (only) the libraries / classes that your application needs. It will be platform specific.
From Java 14 onwards, you can use jpackage to produce (platform specific) native executables for Java applications.
There are also 3rd-party tools that can generate executables, and third party installer generators that (in some cases) can install Java for the end user.
Note: if you take the approach of distributing your application as a self-contained JRE or native executable, the user no longer has the option of updating their Java to address security related issues. It becomes your problem / responsibility ... as the supplier of the software ... to make available in a timely fashion application updates that incorporate the new Java releases with important security patches.
If you use something like GraalVM to compile a native binary, then there is nothing more you should need for a simple application (meaning, nothing is tried to dynamically load classes at runtime with reflection)
Other than that, the Java JRE is required, and can be included as part of an application package; for example, IntelliJ or Eclipse IDE come with their own JRE.
Thanks everyone for your input.
After doing more research I found that by using a jdk greater than 8.?, it is possible to bundle everything an application needs in the deployment process.

Why do we need to install a kit (JDK) to start programming in Java

My first programming language was Pascal and I did not have to install more than just the compiler. Same thing with C++, the environment was all set to write code by just installing Visual Studio.
In the case of Java why do we need to install this Java Development Kit besides having the Eclipse, Netbeans or another compiler.
I think that Python and Perl have also a packet to be installed before writing code in those languages. Otherwise we wouldn't be able to start off. What do these packets contain and why some languages require these files to be installed before compiling any code.
The normal Java package (the JRE) only contains the stuff necessary to run Java programmes. The JDK is the package containing the compiler. Based on your experience with Pascal and C++, you obviously understand why you need the compiler to create your own programmes.
Eclipse and Netbeans are IDEs, Integrated Developement Enviroments. They make it easier for you to program, but they are not strictly needed, in the same way that you can write a program in C++ by just installing a C++ compiler and without using Visual Studio. There are many programmers, especially in the non-windows-world, who just use a text editor to write those programmes.
As for Python and Perl, it's the same thing. You need to install their respecitve interpreters to run programmes written in those languages. Without them, how do you expect the computer to understand what you want from it?
If you want compare, for example, Eclipse and Visual Studio: the installation for Visual Studio contains both the IDE and the compiler. Eclipse is just the IDE. You also need to install the compiler, which is contained in the JDK.
JDK (Java Development Kit) contains the tools required to develop aplications such as the Java compiler.
As seen in the
"Java SE Downloads" page:
Software Developers: JDK (Java SE Development Kit). For Java Developers. Includes a complete JRE plus tools for developing, debugging, and monitoring Java applications.
Java is quite different than the languages you mentioned. I like this slight difference to be honnes, it clears things up a lot. If you want to develop, you have to download the JDK otherwise you download a much lighter package which is the JRE that is included in the JDK.
I hope this answers the question :- )
With Pascal and C++ the compiler and related tools will convert the source code into machine code that will directly run on the hardware when called from the Operating System,
In Java, Python and Perl the tools generate an intermediate code that does not run directly on the hardware, you need a runtime which is the executable that the operating system calls. This executable will read the intermediate code and convert it to machine language. In Java this is the JRE called java.exe, python is python.exe etc (in non Windows/DOS oS the .exe is not there as not required for executables). In Java you see the intermediate code as .cls files or packed into jars/wars etc and have to explicitly compile the Java to these. Python and perl usually do the compile implicitly, python files show as .pyc and others
Writing Java applets and applications needs development tools like JDK. The JDK includes the Java Runtime Environment, the Java compiler and the Java APIs. For Java Developers. Includes a complete JRE plus tools for developing, debugging, and monitoring Java applications.

Java compilation problem, Linux, project from Eclipse re-compile on Linux

I am trying to recompile an existing Java project exported from Eclipse. It is necessary to recompile this because I am running simulations remotely on other machines where a different (older) version of Java is installed. I have tried recompiling my .java file which specifies the simulation in question. However, it appears that it is necessary to recompile all other classes etc as well. Has anyone got an idea how to do this WITHOUT using Eclipse (I am not the Admin on the other machines and thus Eclipse is unavailable to me) and not manually because the project is quite huge?
Thanks a lot for any suggestions!
I recommend you to always have an command line way to build an application. The usual way to do this in Java is using ANT (or Maven).
As #Santiago Lezica says, Eclipse can generate an Ant file.
I believe that Eclipse allows you to build for an older target platform than the one you are currently running. That way you can do all of your builds locally.
The second approach has the advantage that you can fix any problems arising from compiling for the older platforms (e.g. use of new language features, use of new classes / methods) from the comfort of your own ... workstation.
There is another option that you should consider: Tell Eclipse to generate code for the old Java version (see the compiler options). That way, you can create code that runs on Java 1.3, even if Eclipse uses Java 5.
Not sure what your requirements are, but you could set the compiler level for your projects at the (older) level of your Linux installs. This would cause Eclipse to recompile it at that version, instead of a newer version.
At my company we use IBM's Rational Application Developer (instead of pure Eclipse), but I am assuming the option is in the same spot. If you right-click on your project, you can go to the Java Compiler options and then set the compatibility to the level of that on Linux (1.3, 1.4, etc.).
Since compile Java byte-code is supposed to be portable (for the most part), this should get you past most of your problems.
Otherwise, the other option is to use something like Ant or Maven scripts (which can be kicked off by Eclipse) and then just use a property to set the compiler right before you run it. This way you don't have to switch properties on your projects all the time, if you truly do need "newer" compiled code and can't live with "older" code on both systems.

Can I distribute a JDK with my application?

I am working on an application called Enchanting. The application, based on Scratch, emits Java source code and compiles it for uploading onto LEGO Mindstorms NXT Robots.
While the application is very early, users have a hard time installing it.
Right now Windows users have to:
download and install a Java Developer Kit
download and install LeJOS (a java library for the NXT)
possibly tweak environment variables
then they can download, install, and run Enchanting itself
If I could provide an installer that would include the JDK, and LeJOS, I could figure out the environment variables at run time, and the process becomes:
Download, install, and run Enchanting
Is there a way to redistribute a JDK?
(Incidentally, Processing (a simplified text-based programming environment) seems to offer a version that comes with the JDK, so it appears that there is a legitimate way to do so).
Addendum: I would like a Windows user who does not have java installed to be able to run a single .exe file to install the JDK, LeJOS, and Enchanting.
The information regarding redistribution is here for Java 10 JDK and here for Java 8 JDK. Currently Java 8's is substantially more detailed than Java 10's.
and you can use PackJacket, to package all the files you need and create an installer.
Assuming you satisfy all the legal terms required to distribute stuff, you can use izpack to install all the prerequisites, including a JDK/JVM and configuration of environment variables.
Quite a number of IBM Eclipse based tools have JDKs with them.
Or you could just emit bytecode directly. You could bundle a much smaller (than the JDK) JVM dynamic language then use it to compile to bytecode or use libraries made for that purpose.
(I got the following from the Projects using Kawa page)
App Inventor for Android uses Kawa to translate its visual blocks language.
...The Nice compiler (nicec) uses Kawa's gnu.expr and gnu.bytecode packages to generate Java bytecode. ...
It's this last one is the one that uses the Kawa language framework to generate bytecode.
Don't forget about Groovy, Jython, Clojure, and Ruby. Interesting fact about Groovy, the interpreter can compile Java code since Groovy is (more or less) a superset of Java.

Jython Netbeans with JavaSE

I want to develop a Java application mixing java with jython. I am using the IDE Netbeans with the python plugin. How do i work on this? (There is a built in support for Groovy with javaSE from IDE call Groovy classes from Java code, and Java classes from Groovy code but not for jython)
ref: http://www.netbeans.org/features/groovy/index.html
Netbeans 6.5 supports both Python and Jython.
http://www.netbeans.org/features/python/
Assuming you are using that version with the Python plug-in, it's just a matter of setting the runtime you wish to use via the platform manager (here's where you would choose Jython).
alt text http://img15.imageshack.us/img15/1586/platformmanager.png
I would also want to add that since 6.5 release, the Python bits have been vastly improved upon. So, if you want to try the new but unstable builds, please grab one from http://deadlock.netbeans.org/hudson/job/python/
Also, please refer to various documents linked from http://wiki.netbeans.org/Python and my blog posts at http://amitksaha.blogspot.com/search/label/nbpython
In case of problems, please let us know on the mailing list.
If you want to develop a Java application that blends with Jython and works outside NetBeans, then NB's ability to use Jython runtime doesn't help much. Instead, you have basically two choices:
You can compile your Python to Java classes using jythonc.
Or: you can embed the Jython interpreter inside your Java app.
To embed, you need to create a Jython library to be included in your Java app. Do this by going to Tools -> Libraries, select New Library, and add the stuff at NetBeans' Jython directory there (C:\Program Files\NetBeans 6.5\python1\jython-2.5 in my machine). You need jython.jar and at least most of the stuff at the javalib directory.

Categories