开发者

.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 you. I tried to list files from a folder using JS in Firefox XUL but I couldn't then i decided to do it with JAVA in JS. I would be happy if i find one example either with JS or Java in JS to list folder files."

Thanking u guys.

karthik

One开发者_Python百科 of my tutor had done it and here is the code but i couldn't understand! I'm sure he called the jar files from the same project folder where exactly all the JS and XUL files are located.

// This function will be called to give the necessary privileges to your JAR files

function policyAdd (loader, urls) {
    //alert("debut charge java");
    try {
        //If have trouble with the policy try changing it to 
        //edu.mit.simile.javaFirefoxExtensionUtils.URLSetPolicy        
        var str = 'edu.mit.simile.javaFirefoxExtensionUtils.URLSetPolicy';
        var policyClass = java.lang.Class.forName(
               str,
               true,
               loader
        );
        var policy = policyClass.newInstance();
        policy.setOuterPolicy(java.security.Policy.getPolicy());
        java.security.Policy.setPolicy(policy);
        policy.addPermission(new java.security.AllPermission());
        for (var j=0; j < urls.length; j++) {
            policy.addURL(urls[j]);
        }
    }catch(e) {
       alert(e+'::'+e.lineNumber);
    }
}

//Get extension folder installation path...
var extensionPath = Components.classes["@mozilla.org/extensions/manager;1"].
            getService(Components.interfaces.nsIExtensionManager).
            getInstallLocation("pde@ghorbel.com"). // guid of extension
            getItemLocation("pde@ghorbel.com");

// Get path to the JAR files (the following assumes your JARs are within a
// directory called "java" at the root of your extension's folder hierarchy)
// You must add this utilities (classloader) JAR to give your extension full privileges
var classLoaderJarpath = "file:///" + extensionPath.path.replace(/\\/g,"/") + "/java/javaFirefoxExtensionUtils.jar";
// Add the paths for all the other JAR files that you will be using
var myJarpath = "file:///" + extensionPath.path.replace(/\\/g,"/") +
"/java/encryption.jar"; // seems you don't actually have to replace the backslashes as they work as well

var urlArray = []; // Build a regular JavaScript array (LiveConnect will auto-convert to a Java array)
urlArray[0] = new java.net.URL(myJarpath);
urlArray[1] = new java.net.URL(classLoaderJarpath);
var cl = java.net.URLClassLoader.newInstance(urlArray);

//Set security policies using the above policyAdd() function
policyAdd(cl, urlArray);

/*


//loading Encryption Class
var myClass = cl.loadClass('Encryption'); // use the same loader from above
var encryptionObj = myClass.newInstance();
//var res = encryptionObj.encrypt("test message to crypt"); // Pass whatever arguments you need (they'll be auto-converted to Java form, taking into account the LiveConnect conversion rules)

*/


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 = new XMLHttpRequest();
} else {
    xmlhttp = new ActiveXObject("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.


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

References:

  • http://en.wikipedia.org/wiki/LiveConnect
  • https://developer.mozilla.org/en/LiveConnect
  • http://jdk6.java.net/plugin2/liveconnect/

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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜