2017-10-11 365 views
0

我使用Spring引導應用程序工作,可交付成果是JAR文件。該文件需要使用命令java -cp "parser.jar" com.ef.Parser運行,並執行一些超出此問題範圍的操作。在這種情況下,我有兩個問題。java -cp與java -jar之間的區別

當我把像 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

這裏有什麼問題?

我有了的代碼如下提供的com.ef

包名稱的Parser.java

@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,我得到target文件夾中的parser-1.5.7.RELEASE.jar。如何使用pom.xml配置將JAR文件重命名爲parser.jar

我試圖手動重命名它,但是,這不起作用。

+0

什麼是'Parser.java'包的名稱?你可以發佈完全相同的代碼嗎? – Ravi

回答

2

回答你的第二個問題:

如果您認爲最終的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}.

回答你的第一個問題是一個比較複雜一點。用一句非常蹩腳的話來說,我會嘗試像這樣解釋它:「超級瓶子」有點特別,並且不包含你期望它們的類。它有一個BOOT-INF目錄,你的類位於該目錄,而spring在標準位置提供所謂的「啓動器」。我建議你打開罐子看看它。您可以使用類似的方式運行應用程序:

java -cp target/parser.jar -Dloader.main=com.ef.parser.Parser org.springframework.boot.loader.PropertiesLauncher 

    . ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.5.7.RELEASE) 

詳細信息可用here

罐的結構是這樣的:

BOOT-INF 
    |->classes 
    |---com.ef.Parser 
    |->lib 
META-INF 
org.springframework... 
+0

你實際上是否指'java -cp target/parser.jar -Dloader.main = com.ef.Parser org.springframework.boot.loader.PropertiesLauncher'?然而,在這兩種情況下,我都會收到'錯誤:無法找到或加載主類org.springframework.boot.loader.PropertiesLauncher'的消息。 – Arefe

+1

我複製了粘貼命令,我在其中啓動了簡單項目。 –

+0

現在它的工作,謝謝 – Arefe

0

1)你確定你的文件中有正確的名稱空間嗎?它不會顯示在您的代碼中。另外,檢查你的manifest文件 - 什麼是類跑了,當你與-jar命令運行它

2)您需要添加

<finalName>${project.artifactId}</finalName> 

要將構建部分在pom.xml

相關問題