如何使用 Java 在 CMD 中使用环境变量?
How to use environment variables in CMD using Java?
我刚开始使用 ProcessBuilder
对象来执行 Java 中的 运行 命令。我的问题是,当我将环境变量放入 builder.command(...)
方法时,它不起作用。但是,当我对环境变量的字符串进行硬编码时,它可以完美运行。下面是我的代码以及有助于弄清楚我在做什么的解释:
ProcessBuilder builder = new ProcessBuilder();
builder.directory(f);
System.out.println("The user's chosen directory is: "+builder.directory());
Map<String, String> environment = builder.environment();
environment.put("WINDOW",w);
environment.put("AUDIO",a);
environment.forEach((key,value)->System.out.println("key is "+key+" "+"value: "+value));
builder.command("ffmpeg", "-i", "$WINDOW","-i", "$AUDIO", "-vcodec", "copy" ,"output.mp4");
Process pr= builder.start();
Explanation/Objective:
Basically I have a JButton
in which an ActionListener
is being fired off when the user clicks it. I am trying to use ffmpeg
to convert an audio and video file together into one file if they desire. This code above will be executed in which I am trying to get the directory of the file they chose in my application to store the two files mentioned previously. Through using builder.directory(f)
, I am able to change the current directory of builder
to that of the user's. I then created a map called environment
in which I could add two environment variables called WINDOW
and AUDIO
. The two env. variables were assigned file names such that the file names were assigned to two variables w
and a
which are of type string. I did check to see if they were in the map and they were. I then attempt to make my set of instructions using the builder.command(...)
method and then start the process using builder.start()
.
结论:
However, the singleoutput.mp4
file was not created and when I checked my Process using the waitFor()
method I get a "1". On the contrary, when I don't use env. variables at all and hardcode the file names in between the parantheses where the env. variables were, it works correctly. So, exactly what am I doing wrong in my builder.command(..)
? Thanks.
我觉得最简单的解决方案是:
builder.command("ffmpeg", "-i", w ,"-i", a, "-vcodec", "copy" ,"output.mp4");
如果您只打算使用它们来注入命令行参数,则无需设置环境变量。
但是,如果你确实想通过环境变量来完成,那么简单的方法就是使用一个subshell来完成所有的命令行解析和扩展;例如
builder.command("/bin/sh", "-c",
"ffmpeg -i $WINDOW -i $AUDIO -vcodec copy output.mp4");
您还可以使用引用、通配符、管道、重定向和所有其他奇特的 shell 功能。
我刚开始使用 ProcessBuilder
对象来执行 Java 中的 运行 命令。我的问题是,当我将环境变量放入 builder.command(...)
方法时,它不起作用。但是,当我对环境变量的字符串进行硬编码时,它可以完美运行。下面是我的代码以及有助于弄清楚我在做什么的解释:
ProcessBuilder builder = new ProcessBuilder();
builder.directory(f);
System.out.println("The user's chosen directory is: "+builder.directory());
Map<String, String> environment = builder.environment();
environment.put("WINDOW",w);
environment.put("AUDIO",a);
environment.forEach((key,value)->System.out.println("key is "+key+" "+"value: "+value));
builder.command("ffmpeg", "-i", "$WINDOW","-i", "$AUDIO", "-vcodec", "copy" ,"output.mp4");
Process pr= builder.start();
Explanation/Objective:
Basically I have a
JButton
in which anActionListener
is being fired off when the user clicks it. I am trying to useffmpeg
to convert an audio and video file together into one file if they desire. This code above will be executed in which I am trying to get the directory of the file they chose in my application to store the two files mentioned previously. Through usingbuilder.directory(f)
, I am able to change the current directory ofbuilder
to that of the user's. I then created a map calledenvironment
in which I could add two environment variables calledWINDOW
andAUDIO
. The two env. variables were assigned file names such that the file names were assigned to two variablesw
anda
which are of type string. I did check to see if they were in the map and they were. I then attempt to make my set of instructions using thebuilder.command(...)
method and then start the process usingbuilder.start()
.
结论:
However, the single
output.mp4
file was not created and when I checked my Process using thewaitFor()
method I get a "1". On the contrary, when I don't use env. variables at all and hardcode the file names in between the parantheses where the env. variables were, it works correctly. So, exactly what am I doing wrong in mybuilder.command(..)
? Thanks.
我觉得最简单的解决方案是:
builder.command("ffmpeg", "-i", w ,"-i", a, "-vcodec", "copy" ,"output.mp4");
如果您只打算使用它们来注入命令行参数,则无需设置环境变量。
但是,如果你确实想通过环境变量来完成,那么简单的方法就是使用一个subshell来完成所有的命令行解析和扩展;例如
builder.command("/bin/sh", "-c",
"ffmpeg -i $WINDOW -i $AUDIO -vcodec copy output.mp4");
您还可以使用引用、通配符、管道、重定向和所有其他奇特的 shell 功能。