java - 从命令提示符运行时,maven 不运行主类

标签 java maven-3

嗨,我使用 Maven 创建了一个项目。基本上我使用 eclipse 创建了项目。然后我构建该项目。 eclipse 后一切都运行良好。现在,当我编译项目时,maven 在目标目录中创建了一个类目录。

Maven Target directory structure

该目录包含所有类文件。现在,当我移动到主文件并通过

从命令提示符运行它时
java BatchImport.class

然后我收到错误

could not find or load main class BatchImport.class.

如何从命令提示符运行它?

这是我的 Maven 配置

<build>

    <!-- to avoid maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e error -->
    <pluginManagement>
        <plugins>

            <!-- Ignore/Execute plugin execution -->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                         <pluginExecutions>

                            <!-- copy-dependency plugin -->
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-dependency-plugin</artifactId>
                                    <versionRange>[1.0.0,)</versionRange>
                                    <goals>
                                        <goal>copy-dependencies</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>

        <!-- Maven compiler plugin
             If you run the code maven package now, Maven will package this Java project into a jar file 
             named “LS360BatchImportIntegration-1.0.0.jar“, in target folder. 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java-version}</source>
                <target>${java-version}</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>

        <!-- To make jar file like a exe file, you need to define a manifest file and declare the application 
             entry point inside via maven-jar-plugin in pom.xml. 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>${maven-jar-plugin.version}</version>

            <!-- The configuration of the plugin -->
            <configuration>

                <!-- Configuration of the archiver -->
                <archive>

                    <!-- Manifest specific configuration -->
                    <manifest>

                        <!-- Classpath is added to the manifest of the created jar file. -->
                        <addClasspath>true</addClasspath>

                        <!--
                           Configures the classpath prefix. This configuration option is
                           used to specify that all needed libraries are found under dependency-jars/
                           directory.

                           Use “classpathPrefix” to specify folder name in which all properties will be placed.
                       -->
                        <classpathPrefix>dependency-jars/</classpathPrefix>

                        <!-- Specifies the main class of the application -->
                        <mainClass>pk.training.basitMahmood.BatchImport</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <!--  uses maven-dependency-plugin to copy all dependencies to "target/dependency-jars/" folder, and 
              defines the dependency classpath with maven-jar-plugin 
        -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeGroupIds>
                            log4j, org.slf4j, org.springframework, commons-net, commons-collections, 
                            org.apache.commons, javax.mail, org.apache.velocity, commons-logging
                        </includeGroupIds>
                        <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

谢谢

编辑 -----------------

enter image description here

我尝试使用 BatchImport.class 和 BatchImport 但我没有得到主类?

谢谢

最佳答案

每个类都有一个包声明。您必须运行您的类(class)

java my.pkg.MyClass

其中“my.pkg”是包名称,“MyClass”是类名称。

为了避免将来的复杂性:您必须从该目录运行它,该目录是包目录的父目录。在您的情况下,它是屏幕截图中的类目录。

关于java - 从命令提示符运行时,maven 不运行主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18058019/

相关文章:

java - 为什么我的字符串不能正确输出我的值?

java - 在 Android 上使用 Rhino 的问题

maven-3 - mvn 版本 :set not updating child poms

maven-2 - 如何在 Maven-3 下使用 MOXy 从 XSD 生成 Java?

java - Ant 在 JDK9 上构建 Maven 期间无法创建 .pkg 文件

java - 使用@RequestScoped CDI bean的@Remote EJB

Java 格式双值与德语区域设置

Java swt canvas 绘制监听器如何工作?

java - maven如何决定构建什么类型的项目以及如何控制单个maven项目中的多个模块?

java - Maven:如何保护 Java 7 项目不使用针对 Java 8 编译的 jar Artifact ?