从Java调用的Setx不能正确工作

我正在使用setx命令来修改PATH系统环境variables。 这是我正在使用的命令:

 setx PATH "%PATH%;C:\Python34" /M 

当它作为batch文件和pipe理员运行时 ,它将正常工作并将新条目添加到PATH

但是,如果我试图从我的Java应用程序中运行它,它开始行为怪异。

首先,用于执行batch file的Java代码:

 import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; public class BatchFile { public static void execute(String batchFilePath) throws IOException, InterruptedException { Process process = Runtime.getRuntime().exec("cmd /c " + batchFilePath); process.waitFor(); printResults(process); } private static void printResults(Process process) throws IOException { String standardOutput = getString(process.getInputStream()); String standardError = getString(process.getErrorStream()); if (!standardOutput.equals("")) { System.out.println(standardOutput); } if (!standardError.equals("")) { System.out.println(standardError); } } private static String getString(InputStream inputStream) throws IOException { return IOUtils.toString(inputStream).trim(); } } 

像这样调用:

 BatchFile.execute("MyBat.bat"); 

运行之前,我的PATH如下所示:

 c:\devkitPro\msys\bin;C:\devkitPro\devkitPPC\bin;C:\devkitPro\devkitPPC\powerpc-eabi\bin;C:\Program Files\Java\jdk1.8.0_45\bin;C:\ProgramData\Oracle\Java\javapath;C:\Python27\Lib\site-packages\PyQt4;C:\devkitPro\devkitARM\bin;C:\Python27\Lib\site-packages\PyQt4;C:\Program Files (x86)\Wiimm\WIT;C:\Windows\System32;C:\MinGW\bin;C:\MinGW\libexec\gcc\mingw32\4.8.1;C:\Python34;C:\MinGW\msys\1.0\bin;C:\Program Files (x86)\Git\cmd;C:\Windows\SysWOW64;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Skype\Phone\ 

然而,在运行Java代码之后,它变成这样:

 C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;c:\devkitPro\msys\bin;C:\devkitPro\devkitPPC\bin;C:\devkitPro\devkitPPC\powerpc-eabi\bin;C:\Program Files\Java\jdk1.8.0_45\bin;C:\ProgramData\Oracle\Java\javapath;C:\Python27\Lib\site-packages\PyQt4;C:\devkitPro\devkitARM\bin;C:\Python27\Lib\site-packages\PyQt4;C:\Program Files (x86)\Wiimm\WIT;C:\Windows\System32;C:\MinGW\bin;C:\MinGW\libexec\gcc\mingw32\4.8.1;C:\Python34;C:\MinGW\msys\1.0\bin;C:\Program Files (x86)\Git\cmd;C:\Windows\SysWOW64;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Program Files 

在控制台上输出的信息如下:

 D:\Programs\Portable\Eclipse\workspace\My Application>setx PATH "C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin/server;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/bin;C:/Program Files/Java/jdk1.8.0_45/bin/../jre/lib/amd64;c:\devkitPro\msys\bin;C:\devkitPro\devkitPPC\bin;C:\devkitPro\devkitPPC\powerpc-eabi\bin;C:\Program Files\Java\jdk1.8.0_45\bin;C:\ProgramData\Oracle\Java\javapath;C:\Python27\Lib\site-packages\PyQt4;C:\devkitPro\devkitARM\bin;C:\Python27\Lib\site-packages\PyQt4;C:\Program Files (x86)\Wiimm\WIT;C:\Windows\System32;C:\MinGW\bin;C:\MinGW\libexec\gcc\mingw32\4.8.1;C:\Python34;C:\MinGW\msys\1.0\bin;C:\Program Files (x86)\Git\cmd;C:\Windows\SysWOW64;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Skype\Phone\;D:\Programs\Portable\Eclipse;;C:\Python34" /M SUCCESS: Specified value was saved. WARNING: The data being saved is truncated to 1024 characters. 

正如你所看到的,很多垃圾Java目录已经被添加到了PATH内容中,并且超过了1024个字符的最大限制,所以它最终被截断了。 最后,添加了一个不需要的Eclipse目录,在实际的Pythonpath之前还有两个分号。

如何在使用Java调用batch file时摆脱不必要的行为? 我想要正确的默认行为,如在cmd上正常调用setx命令时。

前一段时间,我陷入了同样的问题。

简单的答案是:不要使用eclipse来启动你的程序。

Eclipse会为你的执行改变你的环境变量。 (包括PATH)你现在可能已经猜到了,但是eclipse用a关闭了PATH; 所以当你打电话

 setx PATH "%PATH%;C:\Python34" /M 

你加另一个; 在python路径的前面。

只需通过更改批次来检查

 echo %PATH% 

然后从eclipse运行,并从命令行(java -cp.MyMainClass)进行另一个testrun查看差异。

关于“警告:正在保存的数据被截断为1024个字符”。 你可能想看看setx的限制: https ://superuser.com/questions/387619/overcoming-the-1024-character-limit-with-setx