[英]How to use environment variables in CMD using Java?
I am new to using the ProcessBuilder
object to run commands inside of Java.我是使用ProcessBuilder
object 在 Java 内运行命令的新手。 My issue is that when I put my environment variables into the builder.command(...)
method, it doesn't work.我的问题是,当我将环境变量放入builder.command(...)
方法时,它不起作用。 However, when I hardcode the strings of the environment variables, it works perfectly.但是,当我对环境变量的字符串进行硬编码时,它可以完美运行。 Here is my code below along with an explanation to help make clear on what I am doing:下面是我的代码以及有助于弄清楚我在做什么的解释:
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.基本上我有一个JButton
,当用户单击它时,它会触发ActionListener
。 I am trying to useffmpeg
to convert an audio and video file together into one file if they desire.如果他们愿意,我正在尝试使用ffmpeg
将音频和视频文件一起转换为一个文件。 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.通过使用builder.directory(f)
,我可以将builder
的当前目录更改为用户的目录。 I then created a map calledenvironment
in which I could add two environment variables calledWINDOW
andAUDIO
.然后我创建了一个名为environment
的 map ,我可以在其中添加两个名为WINDOW
和AUDIO
的环境变量。 The two env.两个环境。 variables were assigned file names such that the file names were assigned to two variablesw
anda
which are of type string.变量被分配了文件名,使得文件名被分配给两个变量w
和a
,它们是字符串类型。 I did check to see if they were in the map and they were.我确实检查了它们是否在 map 中,它们是。 I then attempt to make my set of instructions using thebuilder.command(...)
method and then start the process usingbuilder.start()
.然后我尝试使用builder.command(...)
方法制作我的指令集,然后使用builder.start()
开始该过程。
Conclusion:结论:
However, the single
output.mp4
file was not created and when I checked my Process using thewaitFor()
method I get a "1".但是,没有创建单个output.mp4
文件,当我使用waitFor()
方法检查我的进程时,我得到一个“1”。 On the contrary, when I don't use env.相反,当我不使用 env. variables at all and hardcode the file names in between the parantheses where the env.变量和硬编码文件名在 env 的括号之间。 variables were, it works correctly.变量是,它工作正常。 So, exactly what am I doing wrong in mybuilder.command(..)
?那么,究竟我在builder.command(..)
中做错了什么? Thanks.谢谢。
It strikes me that the simplest solution is:令我震惊的是,最简单的解决方案是:
builder.command("ffmpeg", "-i", w ,"-i", a, "-vcodec", "copy" ,"output.mp4");
There is no need to set environment variables if you are only going to use them to inject command line arguments.如果您只打算使用环境变量注入命令行 arguments,则无需设置环境变量。
However, if you do want to do it via environment variables, then the simple way is to use a subshell to do all of the command line parsing and expansion;但是,如果您确实想通过环境变量来实现,那么简单的方法是使用子shell 来完成所有的命令行解析和扩展; eg例如
builder.command("/bin/sh", "-c",
"ffmpeg -i $WINDOW -i $AUDIO -vcodec copy output.mp4");
You could also use quoting, globbing, pipelines, redirection and all of the other fancy shell features.您还可以使用引用、通配符、管道、重定向和所有其他花哨的 shell 功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.