简体   繁体   中英

calling stand alone java application from a java servlet

I am new to Java. I have been trying to do some Java application development.

  1. I have created a stand alone Java application. Its a basic calculator application which does basic operations like addition, subtraction, multiplication and division. It is developed using Java swings and AWT.Its GUI

  2. I have a Java servlet application which takes two inputs (numbers) from the user and returns back the result. Basically whatever basic calculator does, the servlet can do such operations on the numbers and return back the result to the client which is a web browser. (client is just an HTML file)

Instead of servlet doing the operations, I want to invoke(from the servlet) stand alone application to do the operations and return back the result to servlet. Then servlet returns back the result to client. Is it possible to call stand alone java application from a servlet. If so what are the different ways?

To invoke your application,you have to write bat or sh file based on your operating system. -- invoke the command by using processbuilder -- sample code match with your requirement

if (osname.indexOf("nux") >= 0 || osname.indexOf("nix") >= 0) 
                {
                    //for sending  request to sh file
                    filePath = request.getSession().getServletContext().getRealPath("")+"/WEB-INF/classes/";
                    cmdList.add("sh");
                    cmdList.add("callDesktopApp.sh");
                    cmdList.add(path);
                    }
                else
                {
                    // for windows
                    String jrePath="";
                    String f="C:/Windows/SysWOW64";
                    if(new File(f).exists())
                    {
                        jrePath="C:\\Program Files (x86)\\Java\\jre6\\bin\\java";
                        jrePath="\""+jrePath+"\"";
                    }
                    else 
                    {
                        jrePath="C:\\Program Files\\Java\\jre6\\bin\\java";
                        jrePath="\""+jrePath+"\"";
                    }
                    filePath = request.getSession().getServletContext().getRealPath("")+"\\WEB-INF\\classes\\";
                cmdList.add("cmd");
                    cmdList.add("/c");
                    cmdList.add("callDesktopApp.bat");
                    cmdList.add(path);
                    cmdList.add(jrePath);
                }   

                pb=new ProcessBuilder(cmdList);

                pb.directory(new File(filePath));
                process = pb.start();

                //Read out dir output
                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
                is.close();
                isr.close();

            }

Hope it may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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