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 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

OK. Can I call method directly or should I use servlet?
@MustafaBerkanDemir - You can't call a method directly, so using a servlet or other similar technology is your only real option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.