how to import a .Jar file in Java Script - java

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.

Related

Converting JavaScript server to Java server

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.

Web UI for existing Java application

I am trying to build a search engine using java and the lucene API as part of a project. For the last step, we plan to build a web UI (a local host would do) for the same. Are there UI softwares/plugins for eclipse which will allow me to call the functions present in the java classes?
Essentially I would want to have a search box and a search key, pressing which will throw up the search results(which is computed from the java program). javascript cannot call java code I understand. So using that is eliminated?
Any suggestions on what to use will be greatly appreciated. I have pretty poor knowledge in front end design!
Cheers!
AB
If all you have is a simple screen with a entry field and a button and you simply want to return an html table. I would go with a servlet and two jsps. Your servlet can call your search engine and then have the jsp format the data into the table. If you do not know web apis this is probably the easiest entry.
I think, If your using JAVA, that you should look into JSF.
It's a rather easy to maintain and work with library for just the uses you describe.
I recommend these tutorials to get you started: http://www.coreservlets.com/JSF-Tutorial/jsf2/#Tutorial-Intro
There are lots of options to achieve this.
you can create web-ui using jsp.
I have also created same type of project using Lucene, here i have used spring mvc.i have provided all the back-end process as REST api which any web-ui can use.
Please do not look into JSF; it is an overengineered pile for your task.
Sure you can call your java code from javascript, you can make it really simple with something like DWR.
However, for your project I would suggest GWT as then you only deal with Java and it will generate javascript, html and css for you.
For your project you dont really need an "enterprise" level framework like spring or a fullstack JavaEE, you could keep it real oldschool with only JSPs and html/javascript. However thats a bit too flaky for my taste, so go with GWT.
With GWT you basically set it up, define your module, entrance point (look at the hello world), and then you add a layout to your page like something to place the searchbox into and the resultbox to. Then you call your other Java code and classes from there like you normally would.
I would suggest you to use GWT in your application because GWT enables you to call java methods and it will also convert Javascript and css for your Java modules after GWT compile.
GWT reference :- http://code.google.com/webtoolkit/gettingstarted.html
If you're going to use GWT, you could aslo check Vaadin.
Creating a search UI is really simple, and the tutorial show a criteria /result table application taht could be adapted.

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 can an Applet call a method in a different frame (from the same web site)?

Given an Applet in one frame in a multi-frame web application where all frames are loaded from the same web server, how can that Applet invoke JavaScript methods in other frames? I tried something like this:
jsobject.call("parent.otherFrame.methodToCall", new String[] {"argument"});
and I get a complaint that the function does not exist. However, if I invoke it like this:
jsobject.eval("parent.otherFrame.methodToCall('argument')");
then it works. I'm trying to avoid use of eval. Is calling code via call in a different frame from an Applet something that is likely to have different behavior in each browser and JVM combination? Is eval safer as it is evaluated in the JavaScript engine rather than partly on the Applet side?
My (limited) experience with a combination of Java applets, JavaScript and (the dreaded) frames, is that it is best to:
Include a script in the page/frame with the applet in it. Define a function intended to be called with 0 or more arguments (one of which might be the name of the target frame).
Call the function from the applet.
Let the JavaScript do the heavy lifting of finding the correct frame & doing whatever needs to be done.
This has a number of advantages.
It is easier to debug JS using the (for example) FF error console, than using the Java console.
JS can be updated more quickly and easily.
Each language encodes what it is good at doing. You can go into a Java editor to see the Java nicely formatted, and a JS editor to see the JS nicely formatted. As opposed to trying to read "JS written in Java" - a scarey experience.
When you ask for help on getting the script working, you have a neat example that the JS gurus can easily read!
The equivalent code without eval is probably something like this:
jsobject = (JSObject)jsobject.getMember("parent");
jsobject = (JSObject)jsobject.getMember("otherFrame");
jsobject.call("methodToCall", new String[] {"hello!"});

Categories

Resources