2011-09-15 42 views
3

嗨請有人可以幫我這個簡單問題我相信...我已經問了8個java聊天網站上的專家,但沒有人可以幫助我:(。 我已經下載了從 http://pdfbox.apache.org/download.html jar文件 我已經打開BlueJ的IDE和加載的罐子當我Java - pdfbox不能導入jar嗎?

import org.apache.pdfbox.*; 
import org.apache.pdfbox.pdmodel; 
import org.apache.pdfbox.pdmodel.PDPage; 

型我得到一個錯誤信息:

error has occured cannot find org.apache.pdfbox 

我已經嘗試的NetBeans也匆匆親ject屬性並添加了罐子,我也去了NetBeans上的側邊菜單,並嘗試這種方式。我仍然得到同樣的錯誤。有人可以幫忙嗎?我已經在3臺不同的電腦上試過了。

好男人給我更多的信息。我下載了罐子,並把它們放在blueJ的文件夾中,我去了選項並選擇了他們說的「加載」的jar文件。我也在Netbeans中做過同樣的工作,我向IDE展示了Jars是否仍然無法工作的IDE是完整代碼,它只是從我嘗試的PDFBOX網站獲取的示例代碼。

import org.apache.pdfbox.exceptions.*; 
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 

/** 
* This will create a blank PDF and write the contents to a file. 
    */ 
public class CreateBlankPDF 
{ 

/** 
* This will create a blank PDF and write the contents to a file. 
* 
* @param file The name of the file to write to. 
* 
* @throws IOException If there is an error writing the data. 
* @throws COSVisitorException If there is an error while generating the document. 
*/ 
public void create(String file) throws IOException, COSVisitorException 
{ 
    PDDocument document = null; 
    try 
    { 
     document = new PDDocument(); 
     //Every document requires at least one page, so we will add one 
     //blank page. 
     PDPage blankPage = new PDPage(); 
     document.addPage(blankPage); 
     document.save(file); 
    } 
    finally 
    { 
     if(document != null) 
     { 
      document.close(); 
     } 
    } 
} 

/** 
* This will create a blank document. 
* 
* @param args The command line arguments. 
* 
* @throws IOException If there is an error writing the document data. 
* @throws COSVisitorException If there is an error generating the data. 
*/ 
public static void main(String[] args) throws IOException, COSVisitorException 
{ 
    if(args.length != 1) 
    { 
     usage(); 
    } 
    else 
    { 
     CreateBlankPDF creator = new CreateBlankPDF(); 
     creator.create(args[0]); 
    } 
} 

/** 
* This will print the usage of this class. 
*/ 
private static void usage() 
{ 
    System.err.println("usage: java org.apache.pdfbox.examples.pdmodel.CreateBlankPDF <outputfile.pdf>"); 
} 

}

+0

當你說你已經「去項目屬性並添加JAR」,你的意思是你已經將它添加到構建路徑? – user937146

+0

是的,我已經將它添加到Netbeans的編譯時間庫 –

回答

0

你做了這些jar文件,一旦你下載了嗎?你是如何將它們添加到你的項目中的? Netbeans無法猜測您的計算機上的罐子位於何處,這就是爲什麼當您導入時無法正常工作....將罐子添加到您的Netbeans項目中:

假設jar文件位於c:\ downloads

在NetBeans中選中項目後,轉到Properties-> sources並選擇Compile Tab,然後轉到jar的位置並添加它們。現在您的導入錯誤應該被清除。

+2

並且不要使用blueJ,它很糟糕。嘗試Eclipse for Java SE開發人員: –

+0

針對Eclipse的+1。 Eclipse是我最喜歡的IDE,而且我討厭blueJ。 – user937146

+0

我實際上開始使用blueJ開發dev(我甚至不知道它仍然存在:))。這並不是那麼糟糕,我的意思是,對於一個超級大的Eclipse來說,它的35463按鈕可能會讓人感到恐懼...這就是爲什麼我建議Eclipse for Java SE用於初學者(而不是JAva EE) –

0

我找不到這個「Pdfbox」產品的Javadocs,但我確實找到了一些示例代碼,它們似乎沒有使用org.apache.pdfbox中的任何類,而是org.apache.pdfbox.pdmodel等子包。現在,知道這一點,我可以在導入語句中看到兩件事情錯誤:如果org.apache.pdfbox中實際上沒有任何類,並且您不需要導入該包,則第一行會顯示您顯示的錯誤;第二行會給出一個錯誤,因爲`org.apache.pdfbox.pdmodel本身就是一個包,但是你試圖導入它,就好像它是一個類。我確信這兩個問題之一 - 或者兩者 - 都是你的實際問題。

+0

是否可以再次編寫import語句我確實從pdfbox.pdmodel中更改了它,因爲原本沒有工作,我認爲導入整個軟件包會更容易 –

1

這是整理。我錯誤地下載了JAR文件。我檢查了文件大小,發現當它超過9MB時它只有20kb。謝謝大家!