略微怪异的一系列问题,但我使用的时候运行到建立在Windows 7上我做得有点怪异使用mklink
一个符号链接,由于存在260个字符的限制问题cmd.exe
通过使用Process
创建我的Java源代码中的符号链接。因为我不能完全解释,下面的代码:JVM运行时在Windows上创建一个符号链接
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
使用吗? - 是否有可能使用
java.nio.file
这个即使在运行此命令超过260个字符?
同样,我明白这是一个奇怪的问题。如有问题要求澄清。
您需要使用mklink程序的'完整路径'或设置环境变量...即。 'c:\ windows \ system32 \ mklink' java默认不添加windows路径。 –
@EddieB:什么环境变量?我认为Java只是使用PATH,就像其他的一样? –
问题是createSymlink(链接,目标)函数中的拼写错误 - 反斜杠而不是正斜杠,您在其中指定/ J作为标志。 –