【发布时间】:2011-05-06 07:13:17
【问题描述】:
有没有人知道如何在 JS 中访问 .jar 文件。我在 Java 中创建了类并作为 jar 文件导入,我想从 JS 文件中访问该类。
“嗨,伙计们,我感谢你们所有人。我尝试在 Firefox XUL 中使用 JS 列出文件夹中的文件,但我不能,然后我决定在 JS 中使用 JAVA。如果我发现我会很高兴在 JS 中使用 JS 或 Java 来列出文件夹文件的一个示例。”
谢谢你们。
卡提克
我的一位导师已经完成了,这是代码,但我无法理解!我确定他从所有 JS 和 XUL 文件所在的同一个项目文件夹中调用了 jar 文件。
// 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)
*/
【问题讨论】:
-
“javascript”中的“java”与“java”中的“java”无关。即这不能完成
-
@ampersand - 在某些情况下,它可以。
-
@Stephen,来自 OP 的单行代码,我的印象是他想在使用类定义实例的正常意义上使用它。当然,OP 现在已经为他的单线提供了更多的上下文,所以我的 cmets 变得无关紧要了。
标签: java javascript jar