Crash testing with Javascript or python on Java - java

I need to 'crash test' a couple of Java applications with mainly Javascript.
By crash test I mean I need to pass the Java class or method all different types of random inputs until it crashed then record it. But I have ran into the problem that I don't know the most efficient way to call a certain Java class or method from Javascript.
Then I also do not know which scripting language would be better for the job, python or javascript. Both languages have to work offline and I don't know if I will have access to the command prompt / admin privileges to use the command line to call the classes or methods. So what do I do?
2 Questions:
How do I call Java classes and Methods in Javascript?
Would python be better for this job?

How do I call Java classes and Methods in Javascript?
Java runs in JVM, Javascript runs in Javascript engine like v8. so first you need a layer which Java and Javascript can both run in. For now there are some ways to run Javascript in JVM, these ways all provide interoperability between Javascript and Java. you can Google 'Javascript JVM' to go ahead.
Would python be better for this job?
'better' depends on much. for programming language, I like Javascript. Javascript and the libraries mainly designed for web development, Python is more like a common programming language. also Python cloud run in JVM with help of JPython

Related

Can Python invoke the Java Framework?

Can Python invoke the Java Framework?
I want to know whether a Python project can invoke a Java Framework, I find a Java Framework in GitHub, whether I can use it in my Python project?
Jython
Jython is one method of calling Java from python -- actually, you run your Python inside Java JVM. This gives you access to almost any Java that runs on JVM, but comes with many limitations.
Because Jython is running python inside the JVM, this gives you acess to almost any Java library. However, you are very restricted in what Python you can use: you can only use Python 2.7, and can import pure Python libraries only (compiled Python libraries with C will not run on Jython).
For an example of a project that uses Jython: Processing.py runs on Jython in order to access the Processing Java API and its ecosystem of Java libraries.
https://github.com/jdf/processing.py
Note that Jython 2 and its docs are quite old, and that the developers are uncertain if / when Jython 3 will be released.
https://github.com/jython/jython3
py4j
py4j is a different approach -- it is "A Bridge between Python and Java" and lets native python code access separate Java running in a separate JVM. Note however that the python and Java code must be running in parallel and communicating through a gateway interface. This is communication between separately running processes -- you are not spinning up a JVM from Python or inside Python.
For example: on the JVM side pass myObject to a new GatewayServer(myObject); on the Python side create a JavaGateway() Python object and use it to communicate with the Java myObject.
Normally Python and Java have their own interpreters/VM's and cannot be shared. It is possible to use Jython but has limitations (fe. python version and support/compatibility with other python packages).
The interpreter and JVM do not match: Java has strict typing, python not. Java compiles and run, python is an interpreter and can change code in runtime (if you want). These are extra challenges why putting all in a same environment is very complex.
There are possibilities like a client/server architecture, but the feasability depends on the level of the framework.
Most of the time low level frameworks are optimized to run directly inside your application process. Any loose coupling will introduce performance and security and compatibility issues. Just think about how reflection will work or multiple inheritance.
If it is a high level framework (fe able to run stand alone) it is more feasable to use some sort of client/server. But still you have to develop a lot for it.
Industry standard is just to implement the framework of your desire in the language you want, then you can get also all the benefits of your platform.

Am I able to use JavaScript with the Java SDK?

I want to create a mod for Minecraft, and I am aware it uses Java. I am currently learning JavaScript, and was wondering if I am able to use JavaScript for the SDK.
Yes and no.
Java is not Javascript...but a Java project can interpret Javascript
Java and Javascript are two completely different languages. However, there is a javascript interpreter created in Java, that you can plug in to java, called Rhino. However, although Rhino makes it easier to embed Javascript into your Java code, its not a simple drop in solution that would allow you to simply script what ever you want with out going through the effort of makin the proper connections. Definitely achievable, but you wont likely be able to start scripting without making that time investment to connect Java and Javascript.
Background story
The relationship between Javascript and Java is a rather shallow one. The similar name comes from a marketing plan back when it was being released by Netscape (which most developers ended up moving to the non-profit Mozilla). They made a deal with Sun (bought by Oracle) to share that similar name, but purely for marketing purposes.
Yes, it is possible to run JavaScript within a Java program, using the Rhino Javascript engine (for example).
However, this would not be a good way develop a Minecraft plugin / mod. None of your knowledge of the Javascript APIs would be relevant. Everything you did to interact with Minecraft would entail using Java classes and methods in the Java or Minecraft libraries.
My advice:
If your aim is to avoid learning Java ... don't be lazy. (You'll end up having to learn the Java APIs anyway. And learning another language will be good for you ... assuming you aspire to be a professional programmer.)
If your aim is to integrate some pre-existing Javascript code-base, it might work. But you might be better off porting the Javascript code to Java.
Sorry, Java and JavaScript are totally different languages. The "Java" in both of them was a marketing decision from ancient times.
They do share some of the same syntax that many languages share, however, so if you have learned JavaScript it might be a little easier to get started with Java. They are definitely NOT interchangeable, though.
Have a look at JDK1.6's ScriptEngine, the interface whose methods provide basic scripting functionality. Using these methods you can execute javascript. Numerous examples can be found on usage of this.
script support is avail from jdk 6 onwards:
reference link
However, this is not a full implementation of Rhino.

What is Rhino in JDK useful for?

Rhino is a JavaScript interpreter running on top of JVM. I guess it was useful for server-side programming in JavaScript. Now we have Node.js. So I wonder if Rhino is still relevant and what it is useful for.
It simply provides a means to run Javascript in the JVM. As such it gives you another implementation choice on top of the JVM in preference to the Java language (in a similar fashion to JRuby, Scala, Groovy etc.). You can write complete solutions in Javascript, or mix/match with Java (or any other JVM language)
Note that this isn't specific to 'server-side' or any such deployment choice.
Speaking from personal experience, I've used it to provide trivial scripting in Ant deployments, and to provide an out-of-the-box simple scripting language for customers using Java applications/toolsets I've written.
Rhino is also used by the the HttpUnit library, which can be used in unit tests to emulate a web browser, for testing web sites. It gives the library JavaScript support.
I use Javascript with Rhino as a scripting language for my MMORPG server written in Java. It allows me to implement NPC scripts, item use scripts, event triggers and other server-sided interactive functionality in Javascript without having to touch the Java part of my server.
Implementing these features in Javascript makes the syntax for them much easier to write and read. Especially because I let the Java part of the server automatically add any trivial boilerplate code to the script sourcecode before sending them to the script engine. This further simplifies the javascript syntax.
It also allows a much clearer separation of the engine and the content.
I also added a Javascript admin shell, which allows me to run any Javascript code on the running server. This has proven to be a very useful testing and debugging tool and I expect it to be a very powerful administration tool later.
I could, of course, also have used any other scripting language, like for example Lua which is frequently used as a scripting language in game development. But Rhino is supported out-of-the-box so I don't need any additional 3rd party dependencies. Also, my client is a HTML5/Javascript application, so I don't have yet another programming language in the project. Note that although both the client and the server content use JS, there is no code shared between them - they are literally on the opposite ends of the system architecture and interact with each other through several layers of indirection, so there is no reason to share code.

Java and Macros project

Basically, for a project i'm thinking of making a macro recording application, with a gui. It doesn't have to be brilliant, just basically functional. Are there macro api's for java? Ideally i'd like to use java, its where most of my limited experience lies. If not then what other languages could be recommended?
I'm using linux myself and was alerted to the xMacro terminal app. what id like is the basic record/play of this with a gui. Any ideas?
For capturing actions outside the java application, you will need to use JNI, since Java doesn't supply the appropriate tools to do that. So you should consider doing that in another language.

Java Wrapper to Perl/Python code

I have to deploy some Web Services on a server that only supports the Java ones, but some of them will be done using perl or python. I want to know if is possible to develop a Java wrapper to call a specific code written in perl or python. So, I want to have all the Web Services in Java, but some of them will call some code using other languages.
Thanks in advance.
Regards,
Ukrania
This depends heavily upon your needs. If Jython is an option for the Python code (it isn't always 100% compatible), then it is probably the best option there. Otherwise, you will need to use Java's Process Builder to call the interpretters directly and return the results on their output stream. This will not be fast (but then again, Jython isn't that fast either, relative to regular Java code), but it is an extremely flexible solution.
For the Python part of it you can use Jython to run Python code right from your Java virtual machine. It'll integrate fully with your Java code as a bonus.
For Perl, use Inline::Java. There are several options for integrating the code; you can call a separate process or you can use an embedded interpreter.
For Python you can use the Java Scripting API.
A Perl implementation is sadly still missing.
There's something I used a while back called Jython which allows you to execute Python code from Java. It was a little quirky, but I got it to do what I needed.
http://www.jython.org

Categories