Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

maven - NoClassDefFoundError while executing main class using java -classpath command

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.

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.

Please guide.

Command to execute main class:

java -classpath targetjobchain-dataloader.jar com.ebayenterprise.ecp.jobs.Main

Error Log:

C:office-datav11-Projectsjobchain-dataloader>java -classpath targetjobchain-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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...