创建符号链接
/*- * Copyright (C) 2014 Erik Larsson * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ //package com.nowjava; import java.lang.reflect.Array;/**来 自 nowjava**/ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static void createSymbolicLink(String linkPathString, String targetPathString) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException { Class<?> fileSystemsClass = Class .forName("java.nio.file.FileSystems"); Class<?> fileSystemClass = Class .forName("java.nio.file.FileSystem"); Class<?> pathClass = Class.forName("java.nio.file.Path"); Class<?> filesClass = Class.forName("java.nio.file.Files");/* 来自 时代Java公众号 - nowjava.com*/ Class<?> fileAttributeClass = Class .forName("java.nio.file.attribute.FileAttribute"); Object emptyFileAttributeArray = Array.newInstance( fileAttributeClass, 0); Method fileSystemsGetDefaultMethod = fileSystemsClass .getMethod("getDefault"); Method fileSystemGetPathMethod = fileSystemClass.getMethod( "getPath", String.class, String[].class); Method filesCreateSymbolicLinkMethod = filesClass.getMethod( "createSymbolicLink", pathClass, pathClass, emptyFileAttributeArray.getClass()); /* java.nio.file.FileSystem defaultFileSystem = java.nio.file.FileSystems.getDefault(); */ Object defaultFileSystemObject = fileSystemsGetDefaultMethod .invoke(null); /* java.nio.file.Path linkPath = defaultFileSystem.getPath(linkPathString); */ Object linkPathObject = fileSystemGetPathMethod.invoke( defaultFileSystemObject, linkPathString, new String[0]); /* java.nio.file.Path targetPath = defaultFileSystem.getPath(targetPathString); */ Object targetPathObject = fileSystemGetPathMethod.invoke( defaultFileSystemObject, targetPathString, new String[0]); /* java.nio.file.Files.createSymbolicLink(linkPath, targetPath); */ try { filesCreateSymbolicLinkMethod.invoke(null, linkPathObject, targetPathObject, emptyFileAttributeArray); } catch (InvocationTargetException e) { final Throwable cause = e.getCause(); if (cause instanceof ClassNotFoundException) { throw (ClassNotFoundException) cause; } else if (cause instanceof NoSuchMethodException) { throw (NoSuchMethodException) cause; } else if (cause instanceof IllegalAccessException) { throw (IllegalAccessException) cause; } else if (cause instanceof IllegalArgumentException) {