Execute javascript function from java - java

I want to know if it's possible to execute a javascript function from java. Basically I have a javascript function to which I want to pass a string and then get the output from that function.
If it's possible, how do I accomplish it?
EDIT -
I couldn't find a solution to this, what I did was re-write all the javascript code in Java. But I am guessing, Rhino ported on Android should do the trick. If someone has tried it out, please post a solution.

You probably want to take a look at the ScriptEngine. There are plenty of samples out there on how to use it. Works on anything but Mac where they for some reason selected to include AppleScript instead of JavaScript by default.
Edit: Take a look at this page, there seems to be a Rhino port for Android out there.

Javascript is not natively supported in java. If you need it, you may implement the Rhino javascript engine to do this.
"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."

Related

Run and Save output of JavaScript from Java

I'm trying to figure out how to run some JavaScript from my Java class. I know that javax.script can be used to do this, but here's the kicker:
The JavaScript I want to run returns and displays a PDF in the browser. I'd like to store the PDF that's generated in a byte array or something like that so my class can do something with it later such as save it to disk.
So I guess the best approach would be to take a String that I have that contains the HTML and JavaScript. Then emulate a browser page, run it, then store the source? I'm not sure what library I would use to do something like this. How might I achieve this?
Focusing on your problem with javascript and java you can you this lib called Direct Web Remoting:
DWR is a RPC library which makes it easy to call Java functions from
JavaScript and to call JavaScript functions from Java (a.k.a Reverse
Ajax).
http://directwebremoting.org/

How to interact with a .aspx using Java?

I'd like to write a java program that has the ability to input information onto an asp, and then read back the data that is returned from the asp.
Does anyone have any suggestions on how to go about doing this?
Is there an easier/more elegant solution than web scraping?
In order for Java code to work in ASP, it has to be compiled byte-code that can be read by the CLR. A quick Google search of "Java" + ".NET" yielded this: http://www.janetdev.org/
Another thing you could do is just learn C#. If you already know Java, there hardly is a learning curve.

Android: Call Python Script (via SL4A) from Java code

Is it possible to call/run a python script file from Java code on the Android platform using SL4A? Basically I have a full blown Java Android app and I have several Python scripts that scrape some information from various web pages. I would like to be able to call these python scripts with the web page and get the results back. Is this possible? If so, can anyone point me in the direction of an example or two?
Thank you,
Harry
run an SL4A script from my application?
http://code.google.com/p/android-scripting/wiki/FAQ#Can_I_run_an_SL4A_script_from_my_application?
Here you go, a solution on how to make Android applications using SL4A and Python. I have developed a functional Python application following this tutorial so I can vouch for it. I assume you can add the Python code and do your data exchange with JSON.
I personally have never tried to mix these two together, though I will probably try after seeing your post.
Also, if you have any problems/questions, related to starting that example, post them here, I might help.

What is the best language to code something like a macro which connect to a website and do some operations?

i would like to know what is the best way to code something like macro which will connect to a website and do some operation like typing the username, password and then click something in the webpage.
through java or other language.
Thank you
If this is for testing, Selenium or Watir would work, if you are just trying to screen scrape any scripting language especially Python/Perl would work.
Actually the answer would vary from person to person and greatly depends on why you are asking this and what language you are most comfortable with. In general I wouldn't write such a macro (tool) in C/C++.
Try http://seleniumhq.org/ . You can easily write a script to do this with the inbuilt firefox plugin - very little coding required.
Yes Java can be used for that. You can also use c# or vb.net or php. Does that answer your question?
You could also use curl.
You can easily setup a curl script to logon to web sites, download pages etc.

how to import a .Jar file in Java Script

I want to import a .Jar file in Java Script,
there is a Class in the Jar file which i want to use in Java script,and here is a code i found but couldn't get it work yet !
var cl = new Packages.java.net.URLClassLoader(
[
new Packages.java.net.URL('http','My Ip', '8080', 'Hello.jar')
]
);
var aClass = Packages.java.lang.Class.forName("PackageAddress.Hello", true, cl);// Hello is a public class name
var aStaticMethod = aClass.getMethod("SayHello", []);//SayHello is a public method returning a string
var greeting = aStaticMethod.invoke(null, []);
alert(greeting);
Any Suggestions would be appreciated
You don't.
JavaScript is not Java.
And trying to use Java code within your JavaScript ExtJS code will a complete overkill, requiring users to have Java plugin installed in their browsers, and slowing down their browsers because of Java plugin. Of course, supposing it might work (and I'm not sure about that).
You want to call webservices from your JavaScript code? (Re)Write the webservice-calling in JavaScript. Not only this is the best solution, but I'm pretty sure it will require less work than trying to set up a Java applet and trying to interact with it.
There's another way to think of this. If you are trying to indeed write a JavaScript application with a Java backend, you probably would struggle to get all the support you need from the JavaScript community. But if you think of it as a Java application with a JavaScript frontend, all of a sudden, you have lots of options. I'm sure SpringBoot alone could give you many complete solutions. Here's one for React.js.
Furthermore, feel free to look at some of these new frameworks I found at Raygun.com. I haven't had the opportunity to use any of these myself but I believe one in particular seems to have the make up of what you're looking for. It was listed as Vaadin.

Categories

Resources