从java客户端连接到db2

时间:2015-04-23 07:23:07

标签: maven jdbc db2 maven-shade-plugin

我正在尝试从运行应用程序的Java客户端连接到db2作为jar。 jar是一个超级jar,其中所有依赖项都由shade插件生成。

当我从我的ide(Netbeans)运行程序时,它运行正常,但是当我从命令行运行它时,它会失败: java -jar target/locationImporter.jar

我得到的例外是:

Exception in thread "main" java.lang.NullPointerException
    at com.ibm.as400.access.PortMapper.getServerSocket(PortMapper.java:216)
    at com.ibm.as400.access.AS400ImplRemote.signonConnect(AS400ImplRemote.java:2363)
    at com.ibm.as400.access.AS400ImplRemote.signon(AS400ImplRemote.java:2278)
    at com.ibm.as400.access.AS400.sendSignonRequest(AS400.java:3142)
    at com.ibm.as400.access.AS400.promptSignon(AS400.java:2706)
    at com.ibm.as400.access.AS400.signon(AS400.java:4035)
    at com.ibm.as400.access.AS400.connectService(AS400.java:1184)
    at com.ibm.as400.access.AS400JDBCConnection.setProperties(AS400JDBCConnection.java:3338)
    at com.ibm.as400.access.AS400JDBCDriver.prepareConnection(AS400JDBCDriver.java:1419)
    at com.ibm.as400.access.AS400JDBCDriver.initializeConnection(AS400JDBCDriver.java:1256)
    at com.ibm.as400.access.AS400JDBCDriver.connect(AS400JDBCDriver.java:395)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at dataprep.ConnectionSupplier.getConnection(ConnectionSupplier.java:18)
    at dataprep.SqlFileRunner.runScript(SqlFileRunner.java:65)
    at dataprep.SqlFileRunner.runSqlFile(SqlFileRunner.java:26)
    at dataprep.PrepareDatabase.main(PrepareDatabase.java:26)

失败的代码是:

Class.forName("com.ibm.as400.access.AS400JDBCDriver").newInstance();
return DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);

因为它在我的ide中运行,所以我知道url,用户和密码是正确的。

我的阴影插件在我的pom中配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <shadedArtifactAttached>true</shadedArtifactAttached>
        <shadedClassifierName>launcher</shadedClassifierName> 
        <finalName>locationImporter</finalName>
        <createDependencyReducedPom>false</createDependencyReducedPom>
        <minimizeJar>true</minimizeJar>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

1 个答案:

答案 0 :(得分:0)

经过几个小时的调试,我的一位同事指出了我正确的方向。删除minimizeJar修复了问题,因为shade插件删除了文件“SocketContainerInet”,因为它没有直接实例化,而是通过字符串值...