简体   繁体   English

Java程序在Netbeans中可以正常运行,但是从内置的JAR运行时,它不起作用

[英]Java program runs fine within Netbeans, but when running from a built JAR it does not work

I am a total noob at java at the moment, and all I am trying to do is add MySQL logging to this Java Proxy for a Minecraft Server. 目前,我对Java完全不了解,而我要做的就是将MySQL日志记录添加到此Minecraft服务器的Java代理中。

When the program is run within NetBeans, the program runs perfectly fine without any errors. 在NetBeans中运行该程序时,该程序运行正常,没有任何错误。 However, when a jar is built and the program is run from the jar, when logging in to the Minecraft Server this error is thrown: 但是,在构建jar并从jar运行程序时,登录Minecraft Server时会引发以下错误:

Error with SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql://localhost:3306/userips' @ org.apache.commons.dbcp.BasicDataSource:1452 SQLNestedException错误:无法为连接URL'jdbc:mysql:// localhost:3306 / userips'@ org.apache.commons.dbcp.BasicDataSource:1452创建类'com.mysql.jdbc.Driver'的JDBC驱动程序

Basically what it is supposed to do is Log a users IP, Username, Time and Date of login. 基本上应该做的是记录用户的IP,用户名,登录时间和日期。

I am using Netbeans IDE 7.2.1 and building the jar with Maven to handle all dependencies. 我正在使用Netbeans IDE 7.2.1,并使用Maven构建jar以处理所有依赖项。

I have commons-dbcp-1.4.jar, mysql-connector-java-5.1.22.jar, and commons-pool-1.5.4.jar imported into my Dependencies using maven, and they are in the jar file. 我已经使用maven将commons-dbcp-1.4.jar,mysql-connector-java-5.1.22.jar和commons-pool-1.5.4.jar导入到我的依赖项中,它们都位于jar文件中。

Here is the class where the connection is established: 这是建立连接的类:

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;


public final class ConnectionPooler {

    public static final Configuration config = new Configuration();


    private static final BasicDataSource dataSource = new BasicDataSource();

    static {
        config.load();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(config.sql_url);
        dataSource.setUsername(config.sql_user);
        dataSource.setPassword(config.sql_pass);
        dataSource.setInitialSize(5);
        dataSource.setMaxActive(30);
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    } 

}

And here is the class where the mysql query and all that are performed: 这是mysql查询和所有执行的类:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class IPLogger {



public boolean logip(String player, String ipp) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;


    Calendar now = Calendar.getInstance();
    TimeZone timeZone1 = now.getTimeZone();
    String timeZone = timeZone1.getDisplayName();
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
    Date date1 = new Date();
    Date date2 = new Date();
    String date;
    String time;
    date = dateFormat.format(date1);
    time = timeFormat.format(date2);


    String SQL_EXIST = "INSERT INTO `proxyips` (`username`, `ip`,`date`,`time`) VALUES ('"+player+"', '"+ipp+"','"+date+"','"+time+" "+timeZone+"')";

    try{
        connection = ConnectionPooler.getConnection();
        statement = connection.prepareStatement(SQL_EXIST);
        statement.executeUpdate();

    } finally {
        if (resultSet != null) {
            try { resultSet.close(); } catch (SQLException ignore) {ignore.printStackTrace(); }
        }
        if (statement != null) {
            try { statement.close(); } catch (SQLException ignore) {ignore.printStackTrace(); }
        }
        if (connection != null) {
            try { connection.close(); } catch (SQLException ignore) {ignore.printStackTrace(); }
        }
    }       

    return true;
}

}

Here is the pom.xml in case I might have some errors with building or something like that: 这是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>net.md-5</groupId>
    <artifactId>BungeeCordKC</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>BungeeCordKC</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                  <showDeprecation>false</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.0</version>
                <configuration>

                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>**/*.java</exclude>
                                <exclude>**/*.properties</exclude>
                                <exclude>**/*.SF</exclude>
                                <exclude>**/*.DSA</exclude>
                            </excludes>
                        </filter>
                    </filters>

                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Main-Class>${main.class}</Main-Class>
                            <Implementation-Version>${describe}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <build.number>unknown</build.number>
        <main.class>net.md_5.bungee.BungeeCord</main.class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.11</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>0.11.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>13.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>annotations</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-ext-jdk15on</artifactId>
            <version>1.47</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.22</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <artifactId>commons-pool</artifactId>
            <groupId>commons-pool</groupId>
            <type>jar</type>
            <version>1.5.4</version>
        </dependency>
    </dependencies>
</project>

If anyone knows what is wrong, or if you have any questions regarding how I set up my environment please let me know. 如果有人知道哪里出了问题,或者您对我如何设置环境有任何疑问,请告诉我。

EDIT: here is the full stack trace when I added that catch statement to the try block. 编辑:这是当我将catch语句添加到try块时的完整堆栈跟踪。

org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '
com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql://localhost.com:3306/use
rips'
        at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(Basic
DataSource.java:1452)
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSou
rce.java:1371)
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource
.java:1044)
        at net.md_5.bungee.ConnectionPooler.getConnection(ConnectionPooler.java:
30)
        at net.md_5.bungee.IPLogger.logip(IPLogger.java:44)
        at net.md_5.bungee.InitialHandler.run(InitialHandler.java:77)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
        at com.mysql.jdbc.Util.stackTraceToString(Util.java:355)
        at com.mysql.jdbc.Util.<clinit>(Util.java:120)
        at com.mysql.jdbc.NonRegisteringDriver.parseURL(NonRegisteringDriver.jav
a:764)
        at com.mysql.jdbc.NonRegisteringDriver.acceptsURL(NonRegisteringDriver.j
ava:265)
        at java.sql.DriverManager.getDriver(Unknown Source)
        at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(Basic
DataSource.java:1437)
        ... 11 more
Caused by: java.lang.RuntimeException: Can't load resource bundle due to underly
ing exception java.util.MissingResourceException: Can't find bundle for base nam
e com.mysql.jdbc.LocalizedErrorMessages, locale en_US
        at com.mysql.jdbc.Messages.<clinit>(Messages.java:61)
        ... 17 more
Caused by: java.util.MissingResourceException: Can't find bundle for base name c
om.mysql.jdbc.LocalizedErrorMessages, locale en_US
        at java.util.ResourceBundle.throwMissingResourceException(Unknown Source
)
        at java.util.ResourceBundle.getBundleImpl(Unknown Source)
        at java.util.ResourceBundle.getBundle(Unknown Source)
        at com.mysql.jdbc.Messages.<clinit>(Messages.java:59)
        ... 17 more

The reason for the error is that you are excluding the .properties files when building the JAR with maven-shade. 该错误的原因是,使用maven-shade构建JAR时,您将排除.properties文件。 I would strongly recommend that you don't build big jars, but simply use the classpath mechanism as it is intended and keep drivers etc in their own jar file. 我强烈建议您不要构建大的jar文件,而只需按照预期使用classpath机制,并将驱动程序等保存在自己的jar文件中。

This is demonstrated by the following part of the stacktrace: 堆栈跟踪的以下部分对此进行了演示:

Caused by: java.lang.RuntimeException: Can't load resource bundle due to underly
ing exception java.util.MissingResourceException: Can't find bundle for base nam
e com.mysql.jdbc.LocalizedErrorMessages, locale en_US
        at com.mysql.jdbc.Messages.<clinit>(Messages.java:61)

Resource bundles are also .properties . 资源包也是.properties

So to fix this my first advice would be not to build one big jar, but otherwise, you will need to change the maven-shade exlusion filter by removing <exclude>**/*.properties</exclude> from: 因此,要解决此问题,我的第一个建议就是不要构建一个大罐子,否则,您将需要从以下位置删除<exclude>**/*.properties</exclude> 。properties <exclude>**/*.properties</exclude>来更改Maven阴影排除过滤器:

<filters>
    <filter>
        <artifact>*:*</artifact>
        <excludes>
            <exclude>**/*.java</exclude>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.SF</exclude>
            <exclude>**/*.DSA</exclude>
        </excludes>
    </filter>
</filters>

The reason it does work in your IDE is that there it uses the actual JAR files downloaded using maven. 它在IDE中起作用的原因是它使用了使用maven下载的实际JAR文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Stanford POS标记器实现为.jar的Java应用程序无法运行,在NetBeans IDE中运行良好 - Trouble running Java application implementing Stanford POS tagger as a .jar, runs fine in NetBeans IDE 在netbeans上运行但在运行jar时没有错误? - No errors when runs on netbeans but not when running the jar? Java程序可以在Eclipse中正常运行,但不能作为.jar文件运行 - Java program runs fine in Eclipse but not as a .jar file JavaFX应用程序在IntelliJ中运行Main时运行良好,内置jar无法运行 - JavaFX application runs fine when running Main in IntelliJ, built jar doesn't run 从批处理文件中运行java程序,在IDE中运行正常 - Issue running java program from batch file, runs fine in IDE 类不会在Jar中运行,但可以从Netbeans中正常运行 - Class wont run in Jar but runs fine from Netbeans 程序在Eclipse中运行,但运行的jar无法运行 - Program runs in Eclipse but running jar does not run 我的程序在Eclipse中运行良好,但抛出作为jar文件运行的异常 - My program runs fine in Eclipse but throws exception running as a jar file Java程序在Netbeans中运行顺利,但在Eclipse中慢慢运行,并作为可执行jar - Java program runs smoothly in Netbeans but slowly in Eclipse and as an executable jar 在 a.class 文件上使用 Java 反编译器 (JD-GUI) 在 src 文件上出现多个错误,但它在 a.jar 中运行良好? 它是如何工作的? - Using Java Decompiler (JD-GUI) on a .class file gives me multiple errors on src file, but it runs fine within a .jar? How does it work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM