Converting JavaScript server to Java server - java

Well I don't know anything about JavaScript, so I ran into a small problem:
WorldServer = require("./worldserver"),
the previous code is JavaScript and I don't know what I should put for that in Java? Is it somekind of import or something?
what's that require("");

this sounds like node.js. in node.js, require() loads external scripts called "modules". In Java, you can use other Classes and packages by using import.

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/

Calling SSJS from Java?

I have created an xPages application which uses a lot of server side javascript code functions located in a server side javascript library.
Now I have some java code located in the java design element which I would like to use to call the javascript functions.
I do understand that it is not logical to call javascript from java, but I guess that all server side javascript is compiled to java, so I was thinking that it might be possible to get a handle to the compiled java class that was generated.
any ideas?
You can create a value binding,
ValueBinding vb = FacesContext.getCurrentInstance().getApplication().createValueBinding("#{javascript:getData()}");
System.out.println(vb.getValue(FacesContext.getCurrentInstance()).toString());
This would call the getData() method from your SSJS library.

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.

Execute javascript function from 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."

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