0

我一直在和朋友一起构建一个小程序。我们一直在为程序使用 JavaFX,并且来不及使用 OpenJFX,因此使我们使用 JDK 1.8.0。我们正在使用 Maven 编译所有内容并成功编译。

问题是,当我们转到 的文件夹时.jar,当我们尝试用这一行打开它时java -jar Medical-Application.jar,我们得到一个没有找到合适的驱动程序jdbc:sqlite:(my path to DB)

我们已经尝试添加Class.forName("org.sqlite.JDBC");,但没有成功。

这是POM文件。

<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>TP3.projet-session</groupId>
  <artifactId>medical-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>medical-project</name>
  <url>http://maven.apache.org</url>
  <developers>
    <developer>
        <id>aville</id>
        <name>ArnaudVillemaire</name>
        <email>villemaire.arnaud@gmail.com</email>
    </developer>
  </developers>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <javafx.version>2.2</javafx.version>
  </properties>
  <build>
        <plugins>
            <plugin>
                <!-- copy all dependencies of your app to target folder-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <configuration>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <configuration>
                            <target>
                                <!-- define the deploy ANT task-->
                                <taskdef name="jfxdeploy" classname="com.sun.javafx.tools.ant.DeployFXTask"
                                    classpathref="maven.plugin.classpath" />
                                <!-- define the JarSing ANT task-->
                                <taskdef name="jfxsignjar" classname="com.sun.javafx.tools.ant.FXSignJarTask"
                                    classpathref="maven.plugin.classpath" />
                                <jfxdeploy width="1024" height="768"
                                    outdir="${project.build.directory}/deploy" outfile="${project.build.finalName}"
                                    nativeBundles="all">
                                    <info title="${project.name}" />
                                    <!-- set the main class of your applcation; I had to create a Main.class (class Main extends MyMain) otherwise it will return an error on start-->
                                    <application name="${project.name}" mainClass="TP3.projet_session.App" />
                                    <resources>
                                        <fileset dir="${project.build.directory}" includes="*.jar" />
                                        <fileset dir="${project.build.directory}/dependency"
                                            includes="*.jar" />
                                    </resources>
                                    <!-- set your jvm args-->
                                    <platform javafx="${javafx.version}+">
                                        <jvmarg value="-Xms512m" />
                                        <jvmarg value="-Xmx1024m" />
                                    </platform>
                                </jfxdeploy>

                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ant-javafx</artifactId>
                        <version>${javafx.version}</version>
                        <systemPath>${java.home}/../lib/ant-javafx.jar</systemPath>
                        <scope>system</scope>
                    </dependency>
                    <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>javafx</artifactId>
                        <version>${javafx.version}</version>
                        <systemPath>${java.home}/lib/ext/jfxrt.jar</systemPath>
                        <scope>system</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <finalName>Medical-Application</finalName>
    </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>javafx</artifactId>
      <version>${javafx.version}</version>
      <systemPath>${java.home}/lib/ext/jfxrt.jar</systemPath>
      <scope>system</scope>
    </dependency>
      <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.7.2</version>
    </dependency>

  </dependencies>
</project>

我读过使用 IDE 进行编译,Maven 也管理运行时的类路径。当我们尝试用命令行打开它时,有没有办法可以正常运行它?

有任何想法吗?

4

1 回答 1

-1

您需要在类路径中包含 JDBC 驱动程序。调用 java -jar Medical-Application.jar 时,jvm 在您的当前目录中查找 sqlite 库。你需要做类似的事情

java -cp path/to/sqlite.jar -jar Medical-Application.jar

这样java可以找到jdbc驱动的正确位置。

于 2019-07-25T20:07:20.483 回答