打包成JAR时出现问题(postgresql)

huangapple go评论56阅读模式
英文:

Issue when packaging to jar (postgresql)

问题

代码错误为:找不到适合的驱动程序

链接有效 - 在IntelliJ IDE中运行正常,但作为jar文件时却不起作用。有人在之前的问题中建议添加maven assembly插件。以下是我可能做错的部分,这是构建部分:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

这里会出现错误:return DriverManager.getConnection(dbUrl);
链接完全正常 - 我甚至手动创建了一个URL,创建了一个测试类,并在IDE中运行。它编译了,我能够打印出连接对象。然而,一旦我将它变成了一个jar文件(构建工件)
注意:如果有关系的话,数据库是使用Heroku创建和托管的。

打包成JAR时出现问题(postgresql)

它会创建一个无适用驱动程序的错误。我认为这可能是因为我在pom.xml文件中输入了错误内容,所以我重新复制粘贴了插件。
如果需要的话,以下是所有内容(链接也包括在内,因为它只是一个测试数据库)

主类

package main;

import java.sql.DriverManager;
import java.sql.SQLException;

public class test {
    public static void main(String[]args) throws SQLException {
        System.out.println(DriverManager.getConnection("jdbc:postgresql://ec2-50-17-90-177.compute-1.amazonaws.com/d7mfkbl2si37vm?sslmode=require&user=tzfelalpgpknzs&password=99cce506d410d89c1ee51f2f8b35eae9c6faacd4b578c29ff18b5e5de3cfcf98"));
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>Database-Test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1203-jdbc4</version>
        </dependency>
    </dependencies>
</project>
英文:

Code errors as: No suitable driver found for

The link works - running it in IntelliJ IDE works fine, but, as a jar file, it doesn't work. Someone suggested, in a previous question, to add maven assembly plugin. I might be doing that wrong, heres the build:

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;single&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

It creates an error here: return DriverManager.getConnection(dbUrl);
The link works perfectly fine - I even created a url by hand, created a test class, and ran that in the IDE. It compiled and I was able to print the connection object. However, once I made it a jar file (build artifacts)
Note: if it matters, the database is made and hosted with Heroku.

打包成JAR时出现问题(postgresql)

it creates a no suitable driver error. I thought this might be because I entered something wrong in my pom.xml file, so I re-copy-pasted the plugin.
If needed, here is everything (link included since its a test database anyways)

MAIN CLASS

package main;

import java.sql.DriverManager;
import java.sql.SQLException;

public class test {
    public static void main(String[]args) throws SQLException {
        System.out.println(DriverManager.getConnection(&quot;jdbc:postgresql://ec2-50-17-90-177.compute-1.amazonaws.com/d7mfkbl2si37vm?sslmode=require&amp;user=tzfelalpgpknzs&amp;password=99cce506d410d89c1ee51f2f8b35eae9c6faacd4b578c29ff18b5e5de3cfcf98&quot;));
    }
}

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;Database-Test&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;single&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
            &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
            &lt;version&gt;9.4-1203-jdbc4&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

答案1

得分: 0

哈哈,结果证明我太蠢了。
多读了一些内容,决定切换到Maven Shade。再次尝试构建 - 没成功。然后,我找到一个网站,告诉我运行“mvn clean package”。运行时没有清单文件。然后,找到另一个网站,复制粘贴了一些其他内容来声明主类(因为基本上这就是我的清单文件的全部内容)。

英文:

Haha, turns out I'm stupid.
Did a bit more reading, and decided to switch to maven shade. Tried building it again - no luck. Then, I found a website that told me to run "mvn clean package." No manifest file when running. Then, found another website and copy pasted some other stuff to delcare the main class (since that's basically all my manifest did anyways).

huangapple
  • 本文由 发表于 2020年9月9日 06:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63802445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定