Vivian Maya
Vivian Maya

Reputation: 518

maven executable jar doesn't find resources

Well, this is my structure :

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

I try to put db.sqlite every possible folder,but always the program cannot find it.

Also 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>

Actually, maven adds files to jar package but when I click It doesn't find it. If I do like :

java -cp target/Stak-with-dependency.jar org.lorde.Stock.App

It works,It finds it so what might be the problem ?

Thanks in Advance

Edit : relevant part of code.

{
        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");
            }
    }

Upvotes: 0

Views: 323

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328780

I'm assuming you're using https://bitbucket.org/xerial/sqlite-jdbc as JDBC driver.

The driver contains a C++ shared library which implements the database itself. C++ has no idea of Java resources, so there is no way to create a path which will tell C++ "the database is inside of this JAR over there".

The workaround is to extract the resource from the JAR into a normal file using Java code. Afterwards, you can point the JDBC driver to this new (normal) file.

Upvotes: 2

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16152

The connection string jdbc:sqlite:db.sqlite does not specify an absolute location. I presume that, as sqlite is file-based, it will look for the file relative to the current directory. So, place the executable JAR in the same folder ar your sqlite database file, then run it.

Upvotes: 0

Related Questions