我想将我的项目打包在一个可执行的JAR中进行分发.
如何将所有依赖JAR的Maven项目打包到我的输出JAR中?
我正在尝试使用maven为一个名为"logmanager"的小型家庭项目生成一个可执行jar,就像这样:
我将那里显示的代码段添加到pom.xml中,然后运行mvn assembly:assembly.它在logmanager/target中生成两个jar文件:logmanager-0.1.0.jar和logmanager-0.1.0-jar-with-dependencies.jar.当我双击第一个jar时出现错误:
Could not find the main class: com.gorkwobble.logmanager.LogManager. Program will exit.
Run Code Online (Sandbox Code Playgroud)
当我双击jar-with-dependencies.jar时出现稍微不同的错误:
Failed to load Main-Class manifest attribute from: C:\EclipseProjects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar
Run Code Online (Sandbox Code Playgroud)
我复制并粘贴了路径和类名,并检查了POM中的拼写.我的主要类从eclipse启动配置启动.有人可以帮我弄清楚为什么我的jar文件不会运行?另外,为什么有两个罐开始?如果您需要更多信息,请与我们联系.
这是完整的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gorkwobble</groupId>
<artifactId>logmanager</artifactId>
<name>LogManager</name>
<version>0.1.0</version>
<description>Systematically renames specified log files on a scheduled basis. Designed to help manage MUSHClient logging and prevent long, continuous log files.</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<!-- nothing here -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.gorkwobble.logmanager.LogManager</mainClass> …Run Code Online (Sandbox Code Playgroud) 我想使用JDBC 4.1驱动程序连接到PostgreSQL数据库.我在以下内容中声明了以下依赖项pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1101-jdbc41</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这将postgresql-9.3-1101-jdbc41.jar进入我的类路径.我已经读过,Class.forName()如果指定了驱动程序,则不再需要加载驱动程序META-INF/services/java.sql.Driver.driver-jar有这个文件,但我仍然收到以下错误:
No suitable driver found for jdbc:postgresql://localhost:5432
Run Code Online (Sandbox Code Playgroud)
我只是在测试中调用然后使用mvn test以下命令运行测试:
DriverManager.getConnection("jdbc:postgresql://localhost:5432");
Run Code Online (Sandbox Code Playgroud)
即使我Class.forName()在收到错误之前打电话.如何正确加载驱动程序?