Java 应用程序转换为 Java 小程序并从 HTML 传递参数

发布于 2024-12-07 16:48:32 字数 1610 浏览 2 评论 0原文

我是 Java 新手,尝试创建一个在执行时运行系统命令的应用程序。 我通过以下代码完成了这一点:

package printtest;
import java.io.*;
import java.util.*;
public class PrintTest {
 public static void main(String args[]) 
 throws InterruptedException,IOException 
    {

    List<String> command = new ArrayList<String>(); 
    command.add(System.getenv("programfiles") +"\\Internet Explorer\\"+"iexplore.exe");
    command.add("http://www.google.com");        
    ProcessBuilder builder = new ProcessBuilder(command);
    Map<String, String> environ = builder.environment();
    builder.directory(new File(System.getenv("programfiles")+"\\Internet Explorer\\"));

    System.out.println("Directory : " + System.getenv("programfiles")+"Internet Explorer\\");
    final Process process = builder.start();
    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);
    }
    System.out.println("Program terminated!");
  }

}

如果我运行该应用程序,它会按以下语法运行系统命令“iexplore.exe http://www.google.com”。这太棒了。

我遇到的问题(我想寻求帮助)是这样的:

我想从 HTML 页面将变量传递到该应用程序,以便可以通过更改 PARAMS 将可执行文件后面的参数传递到 java 应用程序中在 HTML 中。为此,我明白这个应用程序需要是一个小程序。

我不知道如何修改它来编译以包含在 HTML 中。

你能帮我解决这个问题吗?我已经寻找了两天的答案。

更新:

抱歉,我想我没有做出应有的解释。以下是需要完成的工作: 1. 用 PHP 编写的订单管理界面需要一种方法来运行带有额外参数的系统命令来打印运输接收。要以某种方式做到这一点,网页应该通过小程序或任何其他解决方案触发打印。如果您有解决此问题的想法,请告诉我。谢谢

I'm new to Java and tried to create an application which runs a system command when executed.
I accomplished just that with the following code:

package printtest;
import java.io.*;
import java.util.*;
public class PrintTest {
 public static void main(String args[]) 
 throws InterruptedException,IOException 
    {

    List<String> command = new ArrayList<String>(); 
    command.add(System.getenv("programfiles") +"\\Internet Explorer\\"+"iexplore.exe");
    command.add("http://www.google.com");        
    ProcessBuilder builder = new ProcessBuilder(command);
    Map<String, String> environ = builder.environment();
    builder.directory(new File(System.getenv("programfiles")+"\\Internet Explorer\\"));

    System.out.println("Directory : " + System.getenv("programfiles")+"Internet Explorer\\");
    final Process process = builder.start();
    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);
    }
    System.out.println("Program terminated!");
  }

}

If I run the application, it runs a system command in the following syntax "iexplore.exe http://www.google.com". This is great.

The problem I'm having, and I would like to ask for help in it, is this:

I would like to pass variables to this application from a HTML page so the arguments after the executable can be passed in the java application by changing PARAMS in HTML. For this I understood that this app needs to be an applet.

I don't know how to modify this to compile for inclusion in HTML.

Can you help me with this issue?! I've been searching for 2 days now for an answer.

UPDATE:

I'm sorry, I think I'm not explaining as I should. Here's what needs to be done: 1. An order management interface written in PHP needs a way to run a system command with extra parameters to print transport recepts. To do this somehow a webpage should trigger the printing via an applet or any other solution. If you have a an idea about solving this please do tell me. Thanks

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

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

发布评论

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

评论(1

凯凯我们等你回来 2024-12-14 16:48:32

我想从 HTML 页面将变量传递到此应用程序,以便可以通过更改 HTML 中的 PARAMS 将可执行文件后面的参数传递到 java 应用程序中。为此,我明白这个应用程序需要是一个小程序。

不需要。只要服务器端有可以动态生成文档的东西(例如 HTML 或 JNLP 中的参数) launch 文件),您可以使用该功能为 Java Web 创建唯一的(包括该用途的参数)启动文件开始启动。

当然,无论是小程序还是JWS应用程序,都需要GUI。


顺便说一句 - 如果您没有意识到:

  • 正在使用的代码将在 Windows 上打开 IE。
    • 我使用 Windows,但我的默认浏览器是 FireFox。
    • 它在 Mac、Linux、Unix 上会完全失败..
  • 有 3 种内置的打开网页的方法。
    1. Applet 可以访问“AppletContext”类,该类提供 AppletContext.showDocument(URL)
    2. Java Web Start 应用程序。可以访问 JNLP API,它提供 BasicService.showDocument(URL)
    3. Java 6+ 应用程序。可以使用 桌面.浏览(URI)

最后两种方法都优于 Applet 方法,因为它们要么返回一个布尔值来指示成功,要么抛出一系列有用的异常。用于小程序或应用程序中。使用 JWS 启动,无论是 Desktop 类还是使用 Process 都需要经过数字签名和可信的代码。

I would like to pass variables to this application from a HTML page so the arguments after the executable can be passed in the java application by changing PARAMS in HTML. For this I understood that this app needs to be an applet.

No. As long as you have something on the server side that will generate documents dynamically (e.g. for parameters in HTML or a JNLP launch file), you can use that functionality to create an unique (includes parameters for that use) launch file for a Java Web Start launch.

Of course, whether it is an applet or JWS app., it will require a GUI.


BTW - in case you do not realize:

  • The code being used will open IE on Windows.
    • I use Windows but my default browser is FireFox.
    • It will fail completely on Mac, Linux, Unix..
  • Java has 3 built-in ways to open a web page.
    1. An Applet has access to the 'AppletContext' class, which offers AppletContext.showDocument(URL).
    2. Java Web Start apps. have access to the JNLP API, which offers BasicService.showDocument(URL).
    3. Java 6+ apps. can use Desktop.browse(URI).

Either of the last two is superior to the Applet method, since they either return a boolean to indicate success, or throw a range of helpful exceptions. For use in an applet or an app. launched using JWS, either of the Desktop class or using a Process would require digitally signed and trusted code.

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