Search code examples
javashortcutlnk

create shortcuts in java with lib JShortcut


I want to create shortcuts inwindows with code, I use the library here: http://alumnus.caltech.edu/~jimmc/jshortcut/jshortcut/README.html also the corresponding codes:

import net.jimmc.jshortcut.JShellLink;
public class remove {
public static void main(String[] args) throws Exception{
    String path = new String ("/home/test.csv");
    readAndDelete(path, true, StandardCharsets.UTF_8);
}

private static void readAndDelete(String path, boolean ignoreHeader,Charset encoding) throws IOException {
    File file = new File(path);
    CSVParser parser = CSVParser.parse(file, encoding,CSVFormat.DEFAULT.withHeader());
    List<CSVRecord> records = parser.getRecords();
    List<String> docRecord = new ArrayList<String>();
    List<String> shortpath = new ArrayList<String>();
    for (CSVRecord doctype : records){
         docRecord.add(doctype.get(0).toString());
         shortpath.add(doctype.get(1).toString());
    }
    int recordlength = docRecord.size();
    for(String eachdocRecord:docRecord){
        try {
            Path pathtemp=Paths.get(eachdocRecord);
            Files.delete(pathtemp);
        } catch (NoSuchFileException x) {
            System.err.format("%s: no such" + " file or directory%n", path);
        } catch (DirectoryNotEmptyException x) {
            System.err.format("%s not empty%n", path);
        } catch (IOException x) {
            // File permission problems are caught here.
            System.err.println(x);
        }
    }
    for(int i=0; i<recordlength; i++){
         JShellLink link = new JShellLink();
         String pointpath=shortpath.get(i);
         String originalpath = docRecord.get(i);
         String[] parts = pointpath.split("\\\\");
         int partssize= parts.length;
         String name=parts[partssize-1];
         String[] originalparts = originalpath.split("\\\\");
         int originalsize = originalparts.length;
         int lastlength = originalparts[originalsize-1].length();
         String foldername = originalpath.substring(0,originalpath.length()-lastlength);
         link.setFolder(foldername);
         link.setName(name);
         link.setPath(pointpath);
         link.save();
     }
 }
}

I run it in windows command prompt, but always exception:

 Exception in thread "main" java.lang.NoClassDefFoundError:net/jimmc/jshortcut/JShellLink

I compiled the .class successfully ... Anyone could save nme ... thanks a lot


Solution

  • Regardless of anything else that might be wrong (I did not read the entire code) the exception is quite clear - JShellLink is not on your classpath. How to do it best depends on your use case - README suggests editing Manifest file when you build to .jar, it should also be possible to use Maven to take care of that, and any IDE should have been able to take care of classpaths for you. As far as I can tell however your use case looks something like

    javac remove.java
    java remove
    

    (By the way, class names and class file names should start with upper case, that's the standard)

    In that case the simplest way to do that would be to use:

    java -cp .;jshortcut.jar remove
    

    We add current directory (due to JShortcut wanting to have it's dll on classpath, just to be sure) as well as the jar containing classes you use to the classpath. If you're on Unix system use : instead of ;.