我使用Spring启动应用程序,可交付成果是一个JAR文件。该文件需要使用命令java -cp "parser.jar" com.ef.Parser
运行,并执行一些超出此问题范围的操作。在这种情况下,我有两个问题。
当我把命令放的时候
java -jar target/parser-1.5.7.RELEASE.jar com.ef.Parser
,我可以运行该应用。但是,当我输入命令java -cp target/parser-1.5.7.RELEASE.jar com.ef.Parser
时,我收到错误
Error: Could not find or load main class com.ef.Parser
这是什么问题?
我的Parser.java
包名称为com.ef
下面提供了代码,
@EnableJpaRepositories("com.ef.repository")
@SpringBootApplication(scanBasePackages = {"com.ef"}, exclude = JpaRepositoriesAutoConfiguration.class)
public class Parser implements CommandLineRunner {
@Autowired
private IpAddressService ipAddressService;
public Parser() {
}
public static void main(String[] args) throws Exception {
SpringApplication application = new SpringApplication(Parser.class);
application.run(args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("\nHello, Spring Boot!");
exit(0);
}
}
提供了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>
<artifactId>parser</artifactId>
<name>Log-Parser</name>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- <version>RELEASE</version> -->
<version>1.5.7.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--HSQL database -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<!--auto refresh using the dev tool-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--tomcat server-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--live reload of the Sprign boot project-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--H2 database-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<!--Commons CLI-->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--for the live update with the JSP file-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/static</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
第二个问题是当我运行命令mvn clean package
时,我在parser-1.5.7.RELEASE.jar
文件夹中得到target
。如何使用parser.jar
配置将JAR文件重命名为pom.xml
?
我曾尝试手动重命名,但是,这并没有成功。
答案 0 :(得分:2)
第二个问题的答案:
如果您认为最终的jar文件应该命名为parser.jar
,请将其放入您的pom中:
<build>
...
<finalName>parser</finalName>
...
</build>
来自文档:
文件名(不包括扩展名,没有路径信息) 生成的工件将被调用。默认值为 $ {的artifactId} - $ {版本}
。
你的第一个问题的答案有点复杂。在一个非常蹩脚的话语中,我会尝试解释它:&#34; uber jar&#34;有点特殊,并不包含您期望它们的类。它有一个BOOT-INF目录,您的类位于其中,spring提供所谓的&#34;发射器&#34;在标准位置内。我建议你打开罐子看看它。您可以以类似的方式运行应用程序:
java -cp target/parser.jar -Dloader.main=com.ef.parser.Parser org.springframework.boot.loader.PropertiesLauncher
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.7.RELEASE)
详细信息可供here。
jar的结构如下:
BOOT-INF
|->classes
|---com.ef.Parser
|->lib
META-INF
org.springframework...
答案 1 :(得分:0)
1)您确定文件中的命名空间是否正确?它没有显示在您的代码中。另外,检查清单文件 - 使用-jar命令运行它时运行的类
2)你需要添加
<finalName>${project.artifactId}</finalName>
到pom.xml
中的构建部分