Java Path Create nio createSymbolicLink(String linkPathString, String targetPathString)

Here you can find the source of createSymbolicLink(String linkPathString, String targetPathString)

Description

create Symbolic Link

License

Open Source License

Declaration

public static void createSymbolicLink(String linkPathString, String targetPathString)
            throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchFieldException 

Method Source Code


//package com.java2s;
/*-//from  w  w w  .j  ava 2 s. c  om
 * 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/>.
 */

import java.lang.reflect.Array;

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");
        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) {
                throw (IllegalArgumentException) cause;
            } else if (cause instanceof InvocationTargetException) {
                throw (InvocationTargetException) cause;
            } else if (cause instanceof NoSuchFieldException) {
                throw (NoSuchFieldException) cause;
            } else if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else {
                throw e;
            }
        }
    }
}

Related

  1. createRandomClass(String className, Path dir)
  2. createRandomFolder(Path basePath)
  3. createRandomFolders(Path basePath, int numberOfFolders)
  4. createRelativePath(File baseDirFile, File file)
  5. createSourceFile(Path path, String... lines)
  6. createSymlink(Path realFileLocation, Path softLinkLocation)
  7. createSymlink(String targetPathStr, File symlinkFile)
  8. createTempDirectory(final String path, final String prefix)
  9. createTempDirectory(Path dir, String prefix)