gpt4 book ai didi

java - java -cp 与 java -jar 之间的区别

转载 作者:行者123 更新时间:2023-11-29 08:32:39 26 4
gpt4 key购买 nike

我使用 Spring Boot 应用程序,交付物是一个 JAR 文件。该文件需要使用命令 java -cp "parser.jar" com.ef.Parser 运行并执行一些超出本题范围的操作。在这种情况下,我有 2 个问题。

当我输入命令时 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.jartarget文件夹。如何将 JAR 文件重命名为 parser.jarpom.xml配置?

我试图手动重命名它,但是,这行不通。

最佳答案

第二个问题的答案:

如果您假设最终的 jar 文件应命名为 parser.jar,请将其放入您的 pom:

<build>
...
<finalName>parser</finalName>
...
</build>

来自文档:

The filename (excluding the extension, and with no path information) that the produced artifact will be called. The default value is ${artifactId}-${version}.

第一个问题的答案有点复杂。我会用一个非常蹩脚的词来解释它:“ super jar ”有点特别,不包含你期望的类。它有一个 BOOT-INF 目录,您的类位于该目录中,spring 在标准位置提供所谓的“启动器”。我建议你打开 jar 看看。您可以用类似的方式运行应用程序:

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...

关于java - java -cp 与 java -jar 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46694298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com