略微怪異的一系列問題,但我使用的時候運行到建立在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作爲標誌。 –