从外部运行程序获取字符串

发布于 2024-10-09 10:47:36 字数 178 浏览 2 评论 0原文

我有带有文本字段和按钮的 jsp 页面。我还有另一个项目的罐子。 当我单击按钮时,将调用 jar 的 MAIN.class 并且程序在独立窗口(JFrame)中运行。 用户完成程序并退出后,我需要获取退出时生成的程序字符串并将其粘贴到文本框中(该字符串是 HTML 代码)

还有其他人遇到此问题并有解决方案吗? 谢谢 亚历克斯

I have jsp page with text field and button. Also i have jar of another project.
When i click on the button the MAIN.class of the jar is called and program is running in independent window(JFrame).
After user is finished with the program and exits, I need to get the String of program that is generated on exit and paste it into the textbox (The String is HTML code)

Is anyone else having this problem and has a solution?
Thanks
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-10-16 10:47:36

如果您的“来自另一个项目的 jar”将其输出打印到标准输出。您可以使用下面的代码在 servlet 中获取输出。

          try {
                Runtime rt = Runtime.getRuntime();
                //Process pr = rt.exec("cmd /c dir");
                Process pr = rt.exec("{EXTERNAL_JAR_EXECUTE_COMMAND}");

                BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

                String line=null;

                while((line=input.readLine()) != null) {
                    System.out.println(line);
                }

                int exitVal = pr.waitFor();

            } catch(Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }

If your "jar from another project" prints its output to standart output. You can use below code to take output in your servlet.

          try {
                Runtime rt = Runtime.getRuntime();
                //Process pr = rt.exec("cmd /c dir");
                Process pr = rt.exec("{EXTERNAL_JAR_EXECUTE_COMMAND}");

                BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

                String line=null;

                while((line=input.readLine()) != null) {
                    System.out.println(line);
                }

                int exitVal = pr.waitFor();

            } catch(Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }
最舍不得你 2024-10-16 10:47:36

这在生产环境中永远行不通。 Java 代码在 Web 服务器(服务器端)执行,HTML 代码在 Web 浏览器(客户端)执行。基本上,Swing 的东西只会在网络服务器上执行,而不是在网络浏览器上执行。仅当网络服务器和网络浏览器同时在同一台计算机上运行时(就像您在本地开发时一样),它才会起作用。

将其包装在与 HTTP servlet 通信的 Applet 中,或者只是转换 Swing内容转换为 HTML,这样您就无需为此烦恼。

This ain't ever going to work in a production environment. Java code is executed at webserver (server side) and HTML code is executed at webbrowser (client side). Basically, that Swing thing would only be executed at the webserver, not at the webbrowser. It would only work when both the webserver and webbrowser runs by coincidence at physically the same machine (like as when you're developing locally).

Wrap it in an Applet which communicates to a HTTP servlet or just transform the Swing thing to HTML so that you don't need to hassle with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文