简体   繁体   English

Java Inter进程通信

[英]Java Inter process communication

Good Day Everyone, I have a program (lets call it 'A'), which is called from an ANT script using java . 美好的一天每个人,我有一个程序(让我们称之为'A'),这是使用java从ANT脚本调用的。 This program uses Runtime.getRunTime.exec("batFile.bat") . 该程序使用Runtime.getRunTime.exec("batFile.bat") The .bat file in-turn calls another java file (lets call it 'B'). .bat文件又调用另一个java文件(让我们称之为'B')。 Now, here comes the problem. 现在,问题来了。

Is there a way in which B can access instance variable of A ? 有没有办法让B可以访问A的实例变量?

No. Because the .bat file is creating a new jvm process. 不。因为.bat文件正在创建一个新的jvm进程。 May be you want to use DB to share the data. 可能是您想使用DB来共享数据。

If you know the value of the variable in process A before it launches process B, then you could share that value in a number of ways. 如果您在启动流程B之前知道流程A中变量的值,那么您可以通过多种方式共享该值。

Pass it as a command line argument, eg: 将其作为命令行参数传递,例如:

String[] cmd = {"batFile.bat", variableValue};
Runtime.getRunTime.exec(cmd);

Set it as variable in the environment of B's process, eg: 在B的过程环境中将其设置为变量,例如:

String cmd = "batFile.bat";
String[] envp = {"VARIABLE="+variableValue};
Runtime.getRunTime.exec(cmd, envp);

Write the value to a file in process A, read the file in process B. 将值写入进程A中的文件,读取进程B中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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