java - JS 中的 .Jar 文件

标签 java javascript jar

有谁知道如何在JS中访问.jar文件吗?我已经用 Java 创建了类并作为 jar 文件导入,我想从 JS 文件访问该类。

“大家好,我感谢你们所有人。我尝试在 Firefox XUL 中使用 JS 列出文件夹中的文件,但我做不到,然后我决定使用 JS 中的 JAVA 来完成此操作。如果我找到,我会很高兴一个使用 JS 或 Java in JS 列出文件夹文件的示例。”

谢谢你们。

卡提克

我的一位导师已经完成了,这是代码,但我无法理解!我确信他从所有 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 中访问 jar 文件。

您可以做的是在 javascript 中触发 AJAX 请求,并在 servlet 中使用该 jar 进行处理并返回所需的结果,并且该结果将可以在 javascript 中访问。

<小时/>

编辑

在 servlet 中:

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

在 JavaScript 中:

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

现在在 xmlhttp.responseText 中的 js 中,您将获得文件夹中的文件列表。您可以解析它并根据您的需要显示列表。

关于java - JS 中的 .Jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5907993/

相关文章:

javascript - 从 JavaScript 创建的 HTML 中获取源代码

java - 运行 jar 时出现 NoClassDefFound 错误

java - 让 Java 能够从文本文件中读取一行并打印出来

用于删除 MSWord 生成的 HTML 标记的 Java 类

javascript - Node.js + Express.js。如何减少 css 的渲染?

java - 为什么加载图片时要使用getResourceAsStream?

java - Tink 库 com.google.protobuf.GenerateMessageV3$ 无法解析

java - Java 内存模型顺序一致性与 Leslie Lamport 定义有何不同?

java - new 运算符和 Class.newInstance() 有什么区别?

javascript - document.getelementbyid().style.width 没有值