[英]Get short path with '~' on windows when using java
I have created a java application which downloads files from an online source to my local computer which runs windows 7 我创建了一个Java应用程序,该程序将从在线源下载文件到运行Windows 7的本地计算机。
The code downloads the file but also creates a path for that file so it can be stored in that path 该代码下载了文件,还为该文件创建了一个路径,以便可以将其存储在该路径中。
The file then gets converted to another format 然后将文件转换为另一种格式
The issue that I am having is that it seems that windows don't like it if I navigate to the path using an absolute long path 我遇到的问题是,如果我使用绝对长路径导航到该路径,则似乎Windows不喜欢它
I am using cmd to navigate to the file which means I am creating processes to do that 我正在使用cmd导航到该文件,这意味着我正在创建执行该操作的进程
My code looks like this 我的代码看起来像这样
String[] command =
{
"cmd",
};
Process p;
try {
p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdOut = new PrintWriter(p.getOutputStream(), true);
stdOut.println("cd "+strPath);
As you can see above, the last line is just navigating to the path it works for most cases but not when the path is long and has '-' and other characters. 如上所示,最后一行只是导航到大多数情况下可以使用的路径,但当路径较长且具有'-'和其他字符时,则不会。
If I am on windows explorer and I click on the path at the navigation bar the path shows but with '~' if I embed the '~' path within this code by setting strPath to the path with the '~' everything works fine 如果我在Windows资源管理器上,并且单击导航栏上的路径,则显示该路径,但是如果将“〜”路径嵌入到此代码中(通过将strPath设置为带有“〜”的路径),则显示为“〜”,一切正常
Also if I take the absolute full path from strPath (by printing it to the screen for example) and I open cmd.exe then run the following command 另外,如果我从strPath中获取绝对完整路径(例如通过将其打印到屏幕上)并打开cmd.exe,则运行以下命令
cd <some absolute full path> <- this works but doesn't work from java
So I am not sure what I need to do to fix it 所以我不确定该怎么做才能修复它
You can't (and you don't) use ~
for the home folder (that is a shell expansion). 您不能(也不能)将~
用作主文件夹(这是Shell扩展)。 You can use System.getProperty("user.home")
- which will return the home folder. 您可以使用System.getProperty("user.home")
-这将返回主文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.