2012-07-12 51 views
0

我想在我的java項目中使用jGit。我使用的版本是「2.0.0.201206130900-r」,我的操作系統是Windows。一切正常,直到我試圖做一個推:jGit推動產生異常

public static void main(String[] args) { 

    File gitDir = new File("c:\\new-repo\\"); 

try { 

     String localPath = "c:\\new-repo\\"; 
     Repository localRepo = new FileRepository(localPath + ".git"); 
     localRepo.create(); 
     Git git = new Git(localRepo); 

     git.add().addFilepattern("c:\\test.txt").call(); 

     git.commit().setMessage("testcommit").call(); 

     git.push().call(); 

    localRepo.close(); 
} catch (IllegalStateException ise) { 
     System.out.println("The repository already exists!"); 
} catch (IOException ioe) { 
     System.out.println("Failed to create the repository!"); 
} catch (NoFilepatternException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (GitAPIException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
    } 

我所得到的是這樣的:

Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jsch/JSchException 
at org.eclipse.jgit.transport.Transport.<clinit>(Transport.java:111) 
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:141) 
at Application.main(Application.java:76) 
Caused by: java.lang.ClassNotFoundException: com.jcraft.jsch.JSchException 
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:248) 
... 3 more 

可以請別人告訴我什麼是錯在我的代碼?

回答

1

看來你錯過了推廣SSH所需的jsch庫。將它添加到你的類路徑中(如果你使用Maven或將jar添加到你的classpath中,將它添加到你的pom中)。