使用java -classpath命令执行主类时出现NoClassDefFoundError [英] NoClassDefFoundError while executing main class using java -classpath command

查看:70
本文介绍了使用java -classpath命令执行主类时出现NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven项目,在其中有一个Main.java类,其中包含我想从Windows命令提示符下执行的main()方法.我正在使用以下命令来执行它,但是它失败并显示一条错误消息(如下所示).该程序可以从IDE正常运行.

I have a Maven project and within that I have a Main.java class that contains the main() method that I would like to execute from Windows command prompt. I am using the following command to execute it but it fails with an error message (show below). This program works fine from IDE.

stackoverflow上的类似问题为什么我会在Java中收到NoClassDefFoundError? 已列出,但未提供解决方案.只会详细解释问题.

Similar question at stackoverflow Why am I getting a NoClassDefFoundError in Java? has been listed but does not provide a solution. It only explain the problem in a detailed manner.

请指导.

执行主类的命令:

java -classpath target\jobchain-dataloader.jar com.ebayenterprise.ecp.jobs.Main

错误日志:

C:\office-data\v11-Projects\jobchain-dataloader>java -classpath target\jobchain-dataloader.jar com.ebayenterprise.ecp.jobs.Main
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/quartz/ScheduleBuilder
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.quartz.ScheduleBuilder
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

Main.java

package com.ebayenterprise.ecp.jobs;   
public class Main {

    private static final Logger LOG = Logger.getLogger(Main.class);

    public static void main(String[] args) throws SchedulerException {
        LOG.debug("Scheduler started sucessfully.....");

        //get scheduler instance
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();

        //get scheduling time details
        Properties props = loadProperties("scheduler.properties");
        int mins = Integer.parseInt(props.getProperty("mins"));

        //kick off job
        scheduler.scheduleJob(getJobDetail(RecurringDataLoader.class), getJobTrigger(mins));
    }

    public static JobDetail getJobDetail(Class z) {
        return JobBuilder.newJob(z).withIdentity(RECURRING_DATA_LOADER_JOB, DATA_LOADER_JOB_GRP).build();
    }

    //create a simple trigger
    public static Trigger getJobTrigger(int mins) {
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(RECURRING_DATA_LOADER_TRIGGER, DATA_LOADER_TRIGGER_GRP)
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInMinutes(mins)
                        .repeatForever())
                .build();
        return trigger;
    }

}

pom.xml

   <dependencies>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>7.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1004-jdbc41</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
    </dependencies>

推荐答案

能够通过在pom中添加以下插件来使程序正常运行.

Was able to get the program working by adding following plugins in the pom.

第一个依赖项在jar中创建META-INF/manifest.mf文件,第二个依赖项在现有target目录中创建lib文件夹并将其所有副本复制到该目录中.

The first dependency creates the META-INF/manifest.mf file in the jar and the second one creates the lib folder in the existing target directory and copies all the dependencies in there.

  <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>com.ebayenterprise.ecp.jobs.Main</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>                     
    <plugin>
        <!-- Build an executable JAR with runtime dependencies so that this program can be executed from command line using java -jar command -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <overWriteReleases>false</overWriteReleases>
                    <overWriteSnapshots>false</overWriteSnapshots>
                    <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
            </execution>
        </executions>
    </plugin>

这篇关于使用java -classpath命令执行主类时出现NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆