有 Java 编程相关的问题?

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

当从命令promt运行时,java maven不是运行主类

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

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试过了。类,只是BatchImport,但我没有得到主类

谢谢


共 (1) 个答案

  1. # 1 楼答案

    每个类都有一个包声明。你必须和老师一起上课

    java my.pkg.MyClass
    

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

    为了避免将来的麻烦:您必须从目录运行它,这是您的包目录的父目录。在您的情况下,它是屏幕截图中的classes目录