Use Clojure to create a symlink

I recently focused on Clojure as a possible functional language for system scripting. Until it seemed to me that having a JVM below means that I am limited by the capabilities of Java.

So how do I create a symlink? Or a hard link? I mean without (sh "ln" ...) .

+4
source share
5 answers

In the interest of anyone who finds this question now, in Java SE 7, you can use the java.nio.file.Files package to create links. I think this is what Michal Marchik mentioned in his commentary on the future.

createLink

 public static Path createLink(Path link, Path existing) throws IOException 

createSymbolicLink

 public static Path createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs) throws IOException 

This page from the java docs tutorial contains these examples.

Creating a symbolic link

 Path newLink = ...; Path target = ...; try { Files.createSymbolicLink(newLink, target); } catch (IOException x) { System.err.println(x); } catch (UnsupportedOperationException x) { // Some file systems do not support symbolic links. System.err.println(x); } 

Create a hard link

 Path newLink = ...; Path existingFile = ...; try { Files.createLink(newLink, existingFile); } catch (IOException x) { System.err.println(x); } catch (UnsupportedOperationException x) { // Some file systems do not // support adding an existing // file to a directory. System.err.println(x); } 
+3
source

Ah, that’s really pain. I'm not sure that the sh parameter is so bad, actually ... Having said that, Ant provides a symlink task that you could use in a reasonable Clojure friendly form through Lancet, an Ant wrapper originally introduced by Stuart Halloway in his book "Clojure Programming" and currently used internally by Leiningen. If you want to see the symlink task in action in Leiningen, see this line in version 1.3.0.

+2
source

You can use Java Native Access to access native libs on the target host.

A simple example:

 (ns jna-test (:import (com.sun.jna Native Function))) (defn symlink [oldpath newpath] (-> (Function/getFunction "c" "symlink") (.invoke Integer (to-array [oldpath newpath])))) 

There are also Clojure wrappers: clj-native and clojure-jna

+2
source

This is something remarkable:

when using Clojure for system commands, if you use cake instead of leiningen, it can use a constant JVM so that you don’t use it, you have to wait three seconds for the JVM to start every time you run the command.

* The cake is not very stable, so on some days it works much better than others. (as of September 2010)

0
source

You can use this library https://github.com/ToBeReplaced/nio.file which works for me

 (require '[org.tobereplaced.nio.file :refer [create-symbolic-link! ] :as nio]) (defn createSimLink [ targetPath newLink ] (let [theName (.getName (File. targetPath))] (try (nio/create-symbolic-link! (str newLink "/" theName) targetPath ) (catch Exception e (prn "error " e))))) (createSimLink "c:/tmp/test.txt" "c:/tmp/myFolder") 

Record-related privilege required. For instance. to run it in Windows 7 in the Emacs / Cider environment, emacs must be run from the administrator command window, otherwise it will complain "FileSystemException java.nio.file.FileSystemException: c: \ tmp \ myFolder \ test.txt: A required privilege is not held by customer "

0
source

All Articles