2012-08-05 39 views
0

我試圖以編程方式將庫添加到引用庫。這裏是我的代碼:添加庫作爲「引用庫」

String filename = "myfile.jar"; 
InputStream is; 
try { 
    is = new BufferedInputStream(new FileInputStream("D:\\" + filename)); 
    IFile file = project.getFile(filename); 
    file.create(is, false, null); 

    IPath path = file.getFullPath();       
    IClasspathEntry[] cpe = javaProject.getRawClasspath(); 
    List<IClasspathEntry> libraries = Arrays.asList(cpe);      
    libraries.add(JavaCore.newLibraryEntry(path, null, null)); 
    try { 
     javaProject.setRawClasspath(libraries.toArray(new IClasspathEntry[libraries.size()]), null); 
    } catch (JavaModelException e1) { 
     e1.printStackTrace(); 
    } 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} 

但結果似乎是:

enter image description here


更新1.

這裏的類路徑。看起來.classpath沒有改變。

<?xml version="1.0" encoding="UTF-8"?> 
<classpath> 
    <classpathentry kind="src" path="src"/> 
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> 
    <classpathentry kind="output" path="bin"/> 
</classpath> 
+1

此代碼不會爲您的項目更新Eclipse的配置。它只會在運行時添加庫。 – 2012-08-05 19:08:34

+2

@KevinMangold根據有問題的代碼,很明顯代碼片段是Eclipse插件的一部分... – 2012-08-06 15:05:58

回答

0

此解決方案:

IClasspathEntry[] entries = javaProject.getRawClasspath(); 
IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1]; 

System.arraycopy(entries, 0, newEntries, 0, entries.length); 

// add a new entry using the path to the container 
//Path junitPath = new Path("D:\\jarscan.jar"); 
IClasspathEntry junitEntry = JavaCore.newLibraryEntry(new Path("D:\\jarscan.jar"), null, null, false); 
newEntries[entries.length] = junitEntry; 
javaProject.setRawClasspath(newEntries, null); 

更多信息here

+0

這不是真正解決原始問題的方法。您已更改要求。不是將jar複製到項目中,然後以便攜方式引用它,而是爲項目構建路徑添加絕對路徑。 – 2012-08-06 19:33:09

+0

>將庫添加爲「引用庫」。 我沒有寫任何有關將* .jar複製到項目的內容。我只是想讓libarary作爲「引用庫」來投影 – bontade 2012-08-06 19:43:23

0

這裏是一個片段如何自定義庫添加到項目中。我希望它能幫助你理解這個想法。

// configure class path, source dir and output dir 
     final IPath outputpath = project.getFullPath().append(CLASSES); 
     final IClasspathEntry classpath1 = JavaCore.newSourceEntry(project.getFullPath().append(customPath)); 
     final IClasspathEntry[] defaultClasspath = org.eclipse.jdt.ui.PreferenceConstants.getDefaultJRELibrary(); 
     final IClasspathEntry[] newClasspath = new IClasspathEntry[defaultClasspath.length + 2]; 
     System.arraycopy(defaultClasspath, 0, newClasspath, 0, defaultClasspath.length); 
     newClasspath[defaultClasspath.length] = classpath1; 
     javaProject.setRawClasspath(newClasspath, outputpath, null); 
+0

它不起作用。庫只是複製到eclipse項目的根文件夾中。 '.classpath'文件未被修改。 – bontade 2012-08-06 17:01:00

1

我懷疑,問題是這一行:

IPath path = file.getFullPath(); 

試試這個行:

IPath path = file.getProjectRelativePath(); 

調試問題的一個好辦法進行更改時,項目構建路徑查看項目根目錄中的.classpath文件。這個文件會顯示你的代碼的確切效果。然後與手動執行等效操作時得到的效果進行比較。

+0

它不起作用。庫只是複製到eclipse項目的根文件夾中。 '.classpath'文件未被修改。 – bontade 2012-08-06 17:00:17

+0

您是否看到任何異常?如果.classpath文件未被修改,那麼您不訪問您認爲正在訪問的項目。您可能需要更詳細地描述您的設置... – 2012-08-06 17:05:04

+0

我沒有看到任何例外情況。我粘貼了我的班級的源代碼 – bontade 2012-08-06 18:17:05