简体   繁体   English

如何在JavaScript中调用Java方法

[英]How to call a java method in javascript

I have java class which I'm doing file copy to another directory. 我有Java类,正在将文件复制到另一个目录。 I want to call it in javascript. 我想用javascript来称呼它。 I wrote something like this. 我写了这样的东西。

                 for(var i=0;i<arrayExtensions.length;i++){
                        if(arrayExtensions[i]==value.extType){
                            var x=new Package.org.solr.copyImages();
                            var y=x.main(value.FileName,value.FilePath);
                            document.getElementById(showImages).src=y;
                            $(this).find("#showImages").fadeIn();
                        }
                        else{
                            $(this).find("#showImages").fadeOut();
                        }

But when I run my project it gives me this error in console. 但是,当我运行我的项目时,它在控制台中给了我这个错误。

Uncaught ReferenceError: Package is not defined
    at HTMLAnchorElement.<anonymous> (index.jsp:216)
    at HTMLDocument.dispatch (jquery-1.12.4.js:5226)
    at HTMLDocument.elemData.handle (jquery-1.12.4.js:4878)

My java codes like this 我的java代码是这样的

public static String main(String name,String path) {
        // TODO Auto-generated method stub
        File original=new File(path);
        File dest=new File("T:\\Temp\\");
        try {
            FileUtils.copyFileToDirectory(original, dest);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String newPath="T:\\Temp\\"+name;
        return newPath;
    }

What am I doing wrong? 我究竟做错了什么?

Java doesn't run in the web browser. Java无法在网络浏览器中运行。 When using Java and JavaScript together, typically you do an ajax request to the server, which runs the Java code and produces a result, which is then sent back to the browser to be processed by the JavaScript code that did the ajax request (specifically, its success handler). 一起使用Java和JavaScript时,通常会向服务器发出ajax请求,服务器将运行Java代码并生成结果,然后将结果发送回浏览器,由执行ajax请求的JavaScript代码处理(具体来说,其成功处理程序)。

The answers to this question may also be useful: What is the difference between client-side and server-side programming? 这个问题的答案也可能有用: 客户端编程和服务器端编程之间有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM