【发布时间】:2015-10-11 15:24:08
【问题描述】:
有些奇怪的问题,但我在 Windows 7 上使用 mklink 创建符号链接时遇到了问题。由于通过创建符号链接使用 cmd.exe 时存在 260 个字符限制,我正在做一些奇怪的事情在我的 Java 源代码中使用Process。由于我不能完全解释它,这里是代码:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
public class WindowsSymlinkUtility {
private List<String> command, currentSymlinks;
public WindowsSymlinkUtility() {
this.command = this.currentSymlinks = new ArrayList<String>();
this.command.add("cmd.exe");
this.command.add("/C");
}
/**
* Automatically creates a directory junction
* @param String link - the path and name of the symlink
* @param String target - the directory to point the symlink to
* @return boolean
* @see http://ss64.com/nt/mklink.html
*/
public boolean createSymlink(String link, String target) {
return createSymlink("\\J", link, target);
}
/**
*
* @param String flag - the flag for mklink
* @param String link - the path and name of the symlink
* @param String target - the directory to point the symlink to
* @return boolean
* @see http://ss64.com/nt/mklink.html
*/
public boolean createSymlink(String flag, String link, String target) {
this.command.clear();
this.command.add("mklink");
this.command.add(flag);
this.command.add(link);
this.command.add(target);
this.currentSymlinks.add(link);
return this.runner() == 0;
}
public boolean removeSymlink(String link) {
this.command.clear();
this.command.add("RD");
this.command.add(link);
if(this.runner() != 0) {
this.command.clear();
this.command.add("DEL");
this.command.add(link);
} else {
return true;
}
return this.runner() == 0;
}
public boolean removeAllSymlinks() {
for(String link : this.currentSymlinks) {
if(!this.removeSymlink(link)) {
return false;
}
}
return true;
}
/**
* Leave for debugging purposes
* @return String
*/
public String getCurrentCommand() {
String cmd = "";
for(String part : this.command) {
cmd += part + " ";
}
return cmd;
}
private int runner() {
Process process = null;
String message = null;
BufferedInputStream bis = null;
int exitVal = -1;
StringBuilder strBuff = new StringBuilder();
try {
if(this.command.size() < 1) throw new Exception("Length of Windows command cannot be zero");
ProcessBuilder pb = new ProcessBuilder(this.command);
Map<String, String> envVars = pb.environment();
pb.directory();
pb.redirectErrorStream(true);
process = pb.start();
bis = new BufferedInputStream(process.getInputStream());
byte[] bArr = new byte[2048];
while (bis.read(bArr) != -1) {
strBuff.append(new String(bArr).trim());
bArr = new byte[2048];
}
exitVal = process.waitFor();
message = strBuff.toString();
} catch(Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
System.err.println(message);
}
return exitVal;
}
public static void main(String[] args) {
WindowsSymlinkUtility foo = new WindowsSymlinkUtility();
foo.createSymlink("%TEMP%\\foo", "C:\\Users\\djthomps\\Downloads");
}
}
我得到的错误:
java.io.IOException: Cannot run program "mklink": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at WindowsSymlinkUtility.runner(WindowsSymlinkUtility.java:113)
at WindowsSymlinkUtility.createSymlink(WindowsSymlinkUtility.java:56)
at WindowsSymlinkUtility.createSymlink(WindowsSymlinkUtility.java:37)
at WindowsSymlinkUtility.main(WindowsSymlinkUtility.java:134)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Cannot run program "mklink": CreateProcess error=2, The system cannot find the file specified
null
您可能有一些问题:
- 为什么要这样做?
- 由于相关文件和文件夹深深嵌套在文件系统中,完整命令的长度超过 260 个字符。
- 符号链接有什么帮助?
- 我已进行测试以确保符号链接允许我“绕过”260 个字符的限制。
这是我的问题:
- 是否有另一种方法可以在 Java 中创建符号链接,以便在命令超过 260 个字符限制时 Windows 会运行?
- 可以用
SET代替mklink吗? - 即使命令运行超过 260 个字符,是否可以为此使用
java.nio.file?
再一次,我知道这是一个奇怪的问题。如果有问题,请要求澄清。
【问题讨论】:
-
你需要在mklink程序中使用
full path或者设置一个环境变量...即。c:\windows\system32\mklinkjava默认不添加windows路径。 -
@EddieB:什么环境变量?我认为 Java 只是使用 PATH,就像其他所有东西一样?
-
问题是 createSymlink(link, target) 函数中的拼写错误 - 您指定 /J 作为标志的地方是反斜杠而不是正斜杠。
-
@HarryJohnston Java
application使用路径。如果你想运行一个系统进程this application需要它的完整路径。此外,您对 OP 的错误标志是正确的。但这是另一个问题 :) -
@EddieB:实际上再看一遍我认为根本问题是 mklink 是一个内部命令,即没有可执行文件。 OP 需要将
cmd /c放在命令行的开头。
标签: java windows windows-7 cmd