从 Java 程序运行批处理文件,然后运行 exe 文件
我有两个必须从 java 程序调用的文件:
- 一个设置环境的批处理文件。
- exe 文件
为了运行 exe,我需要设置一些环境变量,这是使用批处理文件来完成的。
set MSDEV_HOME=C:\Program Files\Microsoft Visual Studio 8\VC
set FOO_ROOT=D:\UGS\Support_Dev\2005SR1
set FOO_DATA=X:
call %FOO_DATA%\FOO_profilevars
set FOO_BIN=B:
set FOO_LIB=L:
set FOO_INCLUDE=I:
上面的批处理文件中的 FOO_profilevars
是存在于不同硬盘上的另一个批处理文件(这也只是设置环境)。
设置环境后,我将调用 exe。
由于某些原因,我必须单独执行此操作 - 我无法将这两个文件合并到另一个批处理文件或其他文件中并完成工作。
我尝试执行:
ProcessBuilder pb = new ProcessBuilder("D:\\newlogin\\setup.bat");
Process p = pb.start();
int exitValue = p.waitFor();
Map<String, String> env = pb.environment();
System.out.println("exitStatus > " + exitValue);
ProcessBuilder pb2 = new ProcessBuilder("d:\\newlogin\\tcelogin.exe",
"Eid123", "Eid123");
Process p2 = pb2.start();
int exitValue2 = p2.waitFor();
Map<String, String> env2 = pb2.environment();
System.out.println("exitStatus > " + exitValue2);
这不起作用 - 可能是因为在执行第二个进程时未使用第一个进程中设置的环境。 有什么方法可以在设置为运行时的环境中执行文件。
更新: 为什么分开意味着: 1. 我有一些 70env 需要设置。因此,批处理文件是首选。 2. EXE文件会返回需要处理的数据。
...
System.out.println("Process completed");
BufferedReader reader = null;
int exitValue = p.exitValue();
//System.out.println("Exit Value" + exitValue);
if(exitValue == 0)
{
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
}
else
{
reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
}
StringBuffer sb = new StringBuffer();
String temp = reader.readLine();
while(temp != null)
{
sb.append(temp);
temp = reader.readLine();
}
reader.close();
System.out.println( sb.toString());
I'm having two files that I've to invoke from a java program:
- a batch file that sets the environment.
- an exe file
For running the exe, I need to have some environment variables to be set up, which am doing it with the batch file.
set MSDEV_HOME=C:\Program Files\Microsoft Visual Studio 8\VC
set FOO_ROOT=D:\UGS\Support_Dev\2005SR1
set FOO_DATA=X:
call %FOO_DATA%\FOO_profilevars
set FOO_BIN=B:
set FOO_LIB=L:
set FOO_INCLUDE=I:
FOO_profilevars
in the above batch file is another batch file(this also merely sets the environment) that exists on a different hard drive.
Once the environment is set, I'll be calling the exe.
For some reasons, I've to do this separately - i cannot club these two in a another batch file or something and get the things done.
I tried executing:
ProcessBuilder pb = new ProcessBuilder("D:\\newlogin\\setup.bat");
Process p = pb.start();
int exitValue = p.waitFor();
Map<String, String> env = pb.environment();
System.out.println("exitStatus > " + exitValue);
ProcessBuilder pb2 = new ProcessBuilder("d:\\newlogin\\tcelogin.exe",
"Eid123", "Eid123");
Process p2 = pb2.start();
int exitValue2 = p2.waitFor();
Map<String, String> env2 = pb2.environment();
System.out.println("exitStatus > " + exitValue2);
This is not working - may because the environment that is set in the first process is not used while executing the second process.
Is there any way that I execute a file in the environment that is set runtime.
Update:
Why separately means:
1. I've some 70env that needs to be set. So, a batch file has been preferred.
2. The EXE file will return data that needs to be processed.
...
System.out.println("Process completed");
BufferedReader reader = null;
int exitValue = p.exitValue();
//System.out.println("Exit Value" + exitValue);
if(exitValue == 0)
{
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
}
else
{
reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
}
StringBuffer sb = new StringBuffer();
String temp = reader.readLine();
while(temp != null)
{
sb.append(temp);
temp = reader.readLine();
}
reader.close();
System.out.println( sb.toString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,批处理文件在 Windows 命令解释器
cmd.exe
的新实例中运行。无论您在环境中设置什么,都会保留在该流程中。您启动的下一个流程将不会看到任何这些更改,因为创建这些更改的流程那时早已死亡。您可能需要在批处理文件末尾运行该程序,以确保它获得正确的环境。
或者您可以只更改 Java 应用程序中的环境,然后生成该进程。该环境是从调用进程继承的,因此您启动的程序将继承您在Java中预先设置的环境。
预计到达时间:这是来自从未使用 Java 生成进程的人。这就是进程及其环境在 Windows 中的工作方式。如果重新使用 ProcessBuilder 效果更好,那么就使用它。尽管我仍然认为从 Java 内部设置环境比使用批处理文件达到相同目的“更干净”。
Remember that the batch file gets run in a new instance of
cmd.exe
, the Windows Command Interpreter. Whatever you set up in the environment there stays in that process. The next process you start will not see any of those changes since the process where they were made is long-dead by then.You may want to either run the program at the end of the batch file, which ensures that it gets the correct environment.
Or you can just alter the environment in your Java application and then spawn the process. The environment gets inherited from the calling process, thereby the program you start will inherit the environment you set up beforehand in Java.
ETA: This comes from someone who never used Java to spawn a process. It's just how processes and their environments work in Windows. If re-using the
ProcessBuilder
works better, then use that. Though I still think that setting the environment from within Java is "cleaner" than using a batch file for the same purpose.难道不能重用第一个
ProcessBuilder
吗?或者手动附加第一个环境中尚未存在于第二个环境中的所有值
Can't you just reuse the first
ProcessBuilder
?Or manually append all values from the first environment which aren't already in the second environemtn