JS中的.Jar文件 [英] .Jar file in JS

查看:74
本文介绍了JS中的.Jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道如何访问JS中的.jar文件.我已经用Java创建了类并作为jar文件导入,我想从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.

伙计们,我感谢大家.我试图在Firefox XUL中使用JS列出文件夹中的文件,但后来我不能决定用JS中的JAVA来完成.如果我能找到,我会很高兴一个使用JS或JS中的Java列出文件夹文件的示例."

"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."

谢谢你们.

karthik

我的一位导师已经做到了,这里是代码,但我听不懂!我确定他从完全位于所有JS和XUL文件所在的项目文件夹中调用了jar文件.

One 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)

*/

推荐答案

我认为您需要查看LiveConnect,它允许Javascript在Web浏览器中调用Java,反之亦然.

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

参考:

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

这是否将允许您执行所需的操作(访问用户计算机上的文件系统)是另一个问题.这将取决于Java是否作为已签名的JAR分发...用户/浏览器是否接受签名.如果未对JAR进行签名,则Java代码将被视为不受信任的代码,并在沙箱中执行(默认情况下),从而阻止其访问本地文件系统.

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.

这篇关于JS中的.Jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆