我正在用Java开发数据输入UI,该UI已连接到sqlite数据库。在eclipse中工作时,数据库连接良好,但是现在我试图以Jar形式运行它,它将无法工作。 我使用Maven编译/安装来打包我的jar,以便创建一个可执行文件。但是,我系统地得到“找不到适用于jdbc:sqlite:ProductDatabase2.db的合适驱动程序”的结果,导致main中出现NullPointerException。 最终,我希望用户能够单击罐子并开始使用该应用程序。
我已经测试了连接器访问的db文件的相对和绝对路径,但问题不存在。我使用网址“ jdbc:sqlite:ProductDatabse2.db”。
我还尝试过将整个层次结构转移到目录/资源周围。那里也没有
驱动程序IS包含在Eclipse中的项目的类路径中,而相应的jar包含在我的项目的lib文件夹中。
我还在pom.xml中包含了所有依赖关系,您可以在下面看到。
//这就是我连接到数据库的方式(在IDE上有效)
public Connectivity(String databaseName)
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
e.printStackTrace();
}
url = "jdbc:sqlite:"+ databaseName +".db";
}
// 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>
<groupId>loolia.lbi</groupId>
<artifactId>java</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>LBI-ProductEntry</name>
<description>Product Input Loolia LBI</description>
<url>https://looliabrandindex.com</url>
<developers>
<developer>
<id>lana</id>
<name>Lana Abi Rached</name>
<email>lanaabirached@hotmail.com</email>
<organization>Loolia Project</organization>
</developer>
</developers>
<properties>
<project.youtube.version>v3-rev182-1.22.0</project.youtube.version>
<project.youtube.analytics.version>v1-rev63-1.22.0</project.youtube.analytics.version>
<project.youtube.reporting.version>v1-rev10-1.22.0</project.youtube.reporting.version>
<project.http.version>1.20.0</project.http.version>
<project.oauth.version>1.20.0</project.oauth.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>google-api-services</id>
<url>http://google-api-client-libraries.appspot.com/mavenrepo</url>
</repository>
</repositories>
<dependencies>
<!-- YouTube Data V3 support -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtube</artifactId>
<version>${project.youtube.version}</version>
</dependency>
<!-- This dependency is only used for the Topics API sample, which requires the Jackson JSON parser -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>${project.http.version}</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>${project.oauth.version}</version>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>newDT.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/dependency-jars/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
一旦我的jar导出,我将使用以下命令从命令行运行它:
lanas-MacBook-Pro:目标Lana $ java -jar test19.jar
我的第一步是正确运行jar,然后通过简单的双击将其启动。
谢谢您的帮助!