1

I have java class which I'm doing file copy to another directory. I want to call it in 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

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?

1 Answers1

2

Java doesn't run in the web browser. 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).

The answers to this question may also be useful: What is the difference between client-side and server-side programming?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875