有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用参数从命令行运行java Maven项目时出错

我有一个Maven项目,其文件结构如下:

src/main/java/com/TestFolder/{Java file name}

这个Java文件包含一个main方法,我需要使用命令行中的参数执行该方法。如何做到这一点?请帮忙

目前我正在做这样的事情:

mvn exec:java -Dexec.mainClass=src.main.java.com.TestFolder.MyMainJavaClass -Dexec.args="1"

这是正确的语法吗?因为当我运行此命令时,会出现以下错误:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-  plugin:1.4.0:java (default-cli) on project Staples_7Lakh: Execution default-cli of goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java failed: Plugin org.codehaus.mojo:exec-maven-plugin:1.4.0 or one of its dependencies could not be resolved: The following artifacts could not be resolved: backport-util-concurrent:backport-util-concurrent:jar:3.1, org.apache.maven:maven-core:jar:2.2.1, org.codehaus.plexus:plexus-utils:jar:3.0.20: Could not transfer artifact backport-util-concurrent:backport-util-concurrent:jar:3.1 from/to central (https://repo.maven.apache.org/maven2): GET request of: backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar from central failed: SSL peer shut down incorrectly -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

共 (1) 个答案

  1. # 1 楼答案

    这个问题是因为可执行jar中缺少依赖项。可执行jar需要存在所有运行时依赖项。我还遇到了一个类似的问题,我使用maven assembly plugin解决了这个问题。请在下面的配置中找到组装插件配置的pom文件-

    <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>xyz.jeetendra.hibernate.Sample</groupId>
    <artifactId>Excercise1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>Excercise1</name>
    <url>http://maven.apache.org</url>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies> 
    <build>
        <finalName>UserService</finalName>
        <plugins>
        <!  maven Assenmbly Plugin  >
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
        <configuration>
            <! get all project dependency  >
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <!  Main class in mainfest make a executable jar  >
            <archive>
                <manifest>
                    <mainClass>xyz.jeetendra.hibernate.sample.Service</mainClass>
                </manifest>
            </archive>      
        </configuration>
        <executions>
            <execution>
                        <id>make-assembly</id> <!  this is used for inheritance merges  >
                        <phase>package</phase> <!  bind to the packaging phase  >
                        <goals>
                            <goal>single</goal>
                        </goals>
                </execution>
        </executions>
        </plugin>
    </plugins>
    </build>