Skip to content Skip to sidebar Skip to footer

.jar File In Js

does any knows how to access .jar file in JS. I have created class in Java and imported as a jar file and I want to access that class from JS file. 'Hi Guys, i thankful to all of y

Solution 1:

Sorry but currently no such functionality is developed so you can not access jar file in javascript.

What you can do is fire an AJAX request in javascript and in servlet use that jar for processing and return desired result and that result will be accessible in javascript.


Edit

In servlet:

File file = new File("/folderName");
File[] files;
if(file.isDirectory()){
    files = file.listFiles();
    for(File fl : files){
        out.print(fl.getName()+",");
}

In javascript:

var xmlhttp;
if (window.XMLHttpRequest) {
    xmlhttp = newXMLHttpRequest();
} else {
    xmlhttp = newActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", urlToServlet, false);
xmlhttp.send(null);

Now in js in xmlhttp.responseText you will get list of files in a folder. You can parse it and display the list as per your need.

Solution 2:

I think you need to look at LiveConnect which allows Javascript to call Java and vice versa in a web browser.

References:


Whether this will allow you to do what you want (access the file system on the user's machine) is another question. That will depend on whether the Java is distributed as a signed JAR ... and the user / browser accepts the signature. If the JAR is not signed then the Java code will be treated as untrusted code, and executed in a sandbox that (by default) prevents it from accessing the local filesystem.

Solution 3:

Create a trusted applet to use the existing Jars. JavaScript can communicate with (get data from/send dato to) Java applets.

Solution 4:

If you add an applet to your page, doesn't have to have any UI aspect to it, you can then get hold of the applet via the DOM. Once you have that you can call methods on it, so put your api in the applet class of provide a getter to to an object that provides the functionality you want and call that.

Post a Comment for ".jar File In Js"