Java maven可执行jar不';找不到资源

Java maven可执行jar不';找不到资源,java,maven,Java,Maven,这就是我的结构: org ---app ---sqlite ---jfreechart META-INF ---services ---maven ---MANIFEST.mf com ---keypoint db.sqlite 我尝试将db.sqlite放在每个可能的文件夹中,但程序总是找不到它 还有my pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20

这就是我的结构:

org
---app
---sqlite
---jfreechart
META-INF
---services
---maven
---MANIFEST.mf
com
---keypoint
db.sqlite
我尝试将db.sqlite放在每个可能的文件夹中,但程序总是找不到它

还有my 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>

连接字符串
jdbc:sqlite:db.sqlite
未指定绝对位置。我假定,由于sqlite是基于文件的,它将查找相对于当前目录的文件。因此,将可执行JAR放在sqlite数据库文件的同一文件夹中,然后运行它。

我假设您使用的是JDBC驱动程序

驱动程序包含一个实现数据库本身的C++共享库。C++对java资源没有概念,所以没有办法创建一个路径来告诉C++“数据库在这个罐子里面”。


解决方法是使用Java代码将JAR中的资源提取到普通文件中。之后,您可以将JDBC驱动程序指向这个新的(普通)文件。

向我们展示您用于打开数据库的代码。我已经编辑了这个问题。是的,我正在使用JDBC驱动程序,但请原谅,我没有理解您在第二段中的意思,请您再具体一点。@VivianMaya:SQLite无法访问JAR中的文件。你必须把文件放在外面。请始终使用两个文件(
Stak with dependency.jar
db.sqlite
),或者在Stak启动时,使用
getClass().getResource(“db.sqlite”)
获取
InputStream
并将内容复制到硬盘上的文件中。之后,您可以将sqlite指向该新文件。
{
        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");
            }
    }