2015-01-13 33 views
0

好了,這是我的結構:行家可執行的JAR沒有找到資源

org 
---app 
---sqlite 
---jfreechart 
META-INF 
---services 
---maven 
---MANIFEST.mf 
com 
---keypoint 
db.sqlite 

我試圖把db.sqlite每一個可能的文件夾中,但總是程序無法找到它。

也是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

<build> 
    <resources> 
      <resource> 
       <directory> src/main/resource </directory> 
      </resource> 
    </resources> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.1</version> 
      <configuration> 
     <archive> 
        <manifest> 
        <mainClass>org.lorde.StockModule.App</mainClass> 
        </manifest> 
      </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 

      </configuration> 
      <executions> 
       <execution> 
        <id>make-assembly</id> <!-- this is used for inheritance merges --> 
        <phase>package</phase> <!-- append to the packaging phase. --> 
        <goals> 
         <goal>attached</goal> <!-- goals == mojos --> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
      </configuration> 
      </plugin> 
     <plugin> 
     <artifactId>maven-remote-resources-plugin</artifactId> 
     <version>1.5</version> 
     <executions> 
      <execution> 
      <goals> 
       <goal>bundle</goal> 
      </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <includes> 
      <include>**/*.sql</include> 
      </includes> 
     </configuration> 
     </plugin> 
    </plugins> 
</build> 

    <groupId>org.lorde.StockModule</groupId> 
    <artifactId>StockApp</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>StockApp</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.xerial</groupId> 
     <artifactId>sqlite-jdbc</artifactId> 
     <version>3.8.7</version> 
    </dependency> 
    <dependency> 
     <groupId>jfree</groupId> 
     <artifactId>jfreechart</artifactId> 
     <version>1.0.13</version> 
    </dependency> 
    </dependencies> 
</project> 

其實,行家將文件添加到jar包,但是當我點擊它沒有找到它。 如果我不喜歡:

java命令的目標/ STAK與 - dependency.jar org.lorde.Stock.App

它的工作原理,它發現它如此可能是什麼問題?

在此先感謝

編輯:相關部分的代碼。

{ 
     try 
      { 
       Class.forName("org.sqlite.JDBC"); 
      }catch(ClassNotFoundException e) 
      { 
       e.printStackTrace(); 
       JOptionPane.showMessageDialog(null,"error code = 0000"); 
      } 
    } 

    public dbConnect() 
    { 
     try 
      { 
       connect = DriverManager.getConnection("jdbc:sqlite:db.sqlite"); 
      }catch(SQLException ex) 
      { 
       JOptionPane.showMessageDialog(null,"error = 0001"); 
      } 
    } 
+0

向我們展示您用於打開數據庫的代碼。 –

+0

我已編輯該問題。 –

回答

2

我假設您使用的是https://bitbucket.org/xerial/sqlite-jdbc作爲JDBC驅動程序。

該驅動程序包含實現數據庫本身的C++共享庫。 C++不知道Java資源,所以沒有辦法創建一個路徑來告訴C++「數據庫在這個JAR裏面」。

解決方法是使用Java代碼將資源從JAR中提取到普通文件中。之後,您可以將JDBC驅動程序指向這個新的(普通)文件。

+0

是的,我使用該jdbc驅動程序,但原諒我我沒有得到它你的意思是在第二段,你能成爲一個請稍微特別說明一下。 –

+0

@VivianMaya:SQLite無法訪問JAR內部的文件。您必須將文件保存在外面。要麼總是使用兩個文件('Stak-with-dependency.jar'和'db.sqlite'),或者當Stak啓動時,使用'getClass()。getResource(「db.sqlite」)'獲得'InputStream'並且將內容複製到硬盤上的文件中。之後,您可以將sqlite指向該新文件。 –

0

連接字符串jdbc:sqlite:db.sqlite未指定絕對位置。我認爲,因爲sqlite是基於文件的,它會查找與當前目錄相關的文件。因此,將可執行JAR放在您的sqlite數據庫文件所在的文件夾中,然後運行它。

+0

正如我所說我試圖把每個possbile文件夾已經不起作用... –