0

我必须检查某些应用程序是否存在于不同的地方以ProcessBuilder在 Windows 上运行。问题是我需要使用环境变量,直到我运行cmd.exe. 我有类似这样的非工作代码。

private static final String WIN_APP = "%USERPROFILE%/AppData/Local/App/app.exe";
...
File f1 = new File(WIN_APP);
if(f1.exists()) { ... };
...

你有什么提示吗?谢谢。

4

2 回答 2

3

您不能期望环境变量的值userprofile直接在WIN_APP变量值中。

您应该明确调用System.getenv("userprofile")并应与所述变量的其他文本一起使用。

String userProfile = System.getenv("userProfile");

// hoping user profile is not null
String Win_App = userProfile + "/AppData/Local/App/app.exe";
于 2013-09-05T11:43:52.830 回答
1

尝试 :

private static final String WIN_APP =
       System.getenv("userprofile") + "/AppData/Local/App/app.exe";
...
于 2013-09-05T11:38:17.417 回答