我可以创建可以在其中运行其他 Java 程序的 Java GUI 应用程序吗? [英] Can i create java GUI application which can run other java programs in it?

查看:28
本文介绍了我可以创建可以在其中运行其他 Java 程序的 Java GUI 应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 java swing 应用程序,其中我想使用两个文本框,一个用于输入,另一个用于输出过程.当我点击按钮时,我想在输入文本框中运行代码,输出应该在输出文本框中.

I am trying to create a java swing app in which I want to use two text boxes one for input and other for output procedure. When i click the button I want to run the code in input text box and the output should be in output text box.

我尝试使用流程构建器.我使用进程构建器打开 cmd 并在其中运行程序.如果程序只有打印行,它工作正常.但如果程序要求输入,它就不起作用.

I try with the process builder. I open cmd using process builder and run program in it.it works fine if the program has only printing lines.But if the program ask for input it is not working.

我们可以在 eclipse IDE 中做什么,其中程序在控制台窗口中运行,我们可以在那时提供输入.

Which we can do in eclipse IDE in which the program is running in console window and we can give input at that time.

然后我将输入的程序转换成jar文件并尝试运行它代码:

Then I convert the input program to jar file and try to run it code:

 String demo = "javaw -jar D:\\x.jar";             
Process proc = Runtime.getRuntime().exec(demo);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String f;

while((f=br.readLine()) != null)
{
    System.out.println(f);
}

proc.waitFor();
br.close();

或者有另一种不使用cmd的方法请告诉我.我怎样才能为它编写合适的代码?

Or there is another way without using cmd plz tell me. How can I write appropriate code for it ?

推荐答案

是的,还有另一种方法.

Yeah, there is another way.

不完全确定您要实现的目标以及原因,第二个进程是否应该解析输入并为第一个进程提供结果?

Not entirely certain what you are trying to achieve and why, should the second process parse input and make result available for the first one?

如果在同一个应用程序中不需要这种父子进程关系,你也可以有两个不同的进程与套接字通信.

You could also have two different processes which communicate with sockets, if you don't need this parent child process relationship within same application.

您可以避免(并且应该)运行时 - 阅读有关此内容的更多信息:

You can avoid (and should) Runtime - read more about this:

http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

另一种方式:

//对于 java,您需要传递:"java.exe","-cp","bin","package.class")

// for java you need to pass: "java.exe","-cp","bin","package.class")

 ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2") 

您可以使用环境变量传递值,子进程可以通过名称访问它们.

You can pass values with environment variables, they will be accessible by name to child process.

Map<String, String> environementVariable = processBuilder.environment();
environementVariable.put("parameters", "value");

您可以像这样访问它们:

You can access them like this:

Map<String, String> env = System.getenv();

开始进程:

Process process = processBuilder.start();

这篇关于我可以创建可以在其中运行其他 Java 程序的 Java GUI 应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆