2015-08-14 18 views
0

我想inwindows的代碼,我在這裏使用的庫創建快捷方式: http://alumnus.caltech.edu/~jimmc/jshortcut/jshortcut/README.html 也相應代碼:在Java中創建快捷方式以lib JShortcut

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(); 
    } 
} 
} 

我在Windows運行命令提示符下,但始終例外:

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

我編譯的.class成功... 任何人都可以節省NME ...非常感謝

+0

是完整的代碼?我沒有看到從shell運行java程序所需的'public void main(String args []){}''。 –

+0

您是否將JShellLink添加到類路徑中? – Deltharis

+0

@Deltharis ....不,如果文件系統是這樣的:H:\ src \ TRUST在這個文件夾中有jshortcut.jar,remove.class,remove.java。類路徑應該是什麼? JSHEllLink.class中的代碼表示將路徑設置爲.dll文件,但.dll文件位於jar中.... thx很多 – printemp

回答

1

不管其他什麼東西可能是錯的(我沒有讀完整個代碼),異常很清楚 - JShellLink不在你的類路徑中。如何做到最好取決於你的用例 - 自述文件建議在編譯爲.jar時編輯Manifest文件,也應該可以使用Maven來處理這個問題,任何IDE都應該能夠處理類路徑您。至於我可以告訴但你的使用情況看起來像

javac remove.java 
java remove 

(順便說一句,類名和類文件名應以大寫字母開始,這就是標準)

在這種情況下,最簡單的辦法做到這一點是使用:

java -cp .;jshortcut.jar remove 

我們添加當前目錄下(由於JShortcut希望有它的類路徑的dll,只是要確定)以及包含您使用到classpath類的jar。如果你在Unix系統上使用:而不是;