简体   繁体   中英

Spring project won't run from the command line

I can't for the life of me figure out how to get my Spring project to run outside the IDE. I'm using Gradle. I've searched up and down, included the pply plugin: 'application' wording, I see Jars generated but when I run via gradlew run, it dies saying it can't find files.

1) Do I need a separate build.gradle file to run this? I had to take out a ton of other project stuff before it attempted to run (had to remove all times with #)

2) Why won't it work? Do I even need a build.gradle file? How do you go from Spring IDE to something that runs on the command line in the easiest of ways?

apply plugin: 'application'
  dependencies {
    compile project(':caffeine')#
    compile libraries.guava#

    testCompile test_libraries.junit#
    testCompile test_libraries.truth#
    testCompile test_libraries.easymock#
    testCompile test_libraries.guava_testlib#
    compile group: 'org.apache.activemq', name: 'activemq-kahadb-store', version: '5.13.3'
    compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.13.3'
  }

  jar.manifest {#
    name 'com.github.ben-manes.caffeine.guava'#
    instruction 'Import-Package',#
      'com.google.common.cache',#
      'com.github.benmanes.caffeine.cache',#
      'com.github.benmanes.caffeine.cache.stats'#
    instruction 'Export-Package',#
      'com.github.benmanes.caffeine.guava'#
  }#

  jar {
      baseName = 'gs-gradle'
      version =  '0.1.0'
  }

  tasks.withType(Javadoc) {
    options.addStringOption('Xdoclint:none', '-quiet')
  }
  repositories {
     mavenCentral()
  }
  test {#
    systemProperty 'guava.osgi.version', versions.guava#
    systemProperty 'caffeine.osgi.jar', 
    project(':caffeine').jar.archivePath.path#
    systemProperty 'caffeine-guava.osgi.jar',#    
    project(':guava').jar.archivePath.path#
  }#
  mainClassName = 'org.apache.activemq.store.kahadb.disk.util.DiskMark'

Edit Output of Gradle with stripped build.gradle:

Total time: 0.579 secs
    tareks-MacBook-Pro:distributions tarekzegar$ gradlew run --stacktrace
    :compileJava UP-TO-DATE
    :processResources UP-TO-DATE
    :classes UP-TO-DATE
    :run
    Error: Could not find or load main class org.apache.activemq.store.kahadb.disk.util.DiskMark
    :run FAILED

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':run'.
    > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java'' finished with non-zero exit value 

Edit2: Thanks for the response. I took disk.util.DiskBenchmark and modified just that particular file because I needed to add a cache, hence mine is called DiskMark.Java. here are the first few lines of my DiskMark

    package org.apache.activemq.store.kahadb.disk.util;

    import java.io.File;
    import java.io.RandomAccessFile;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.Random;
    import java.util.Iterator;

    import com.github.benmanes.caffeine.cache.Caffeine;
    import com.github.benmanes.caffeine.cache.RemovalCause;
    import com.github.benmanes.caffeine.cache.RemovalListener;
    import com.github.benmanes.caffeine.guava.CaffeinatedGuava;
    import com.google.common.cache.Cache;

    import org.apache.activemq.util.RecoverableRandomAccessFile;

    /**
     * This class is used to get a benchmark the raw disk performance.
     */
    public class DiskMark {

    private static final boolean SKIP_METADATA_UPDATE =
    Boolean.getBoolean("org.apache.activemq.file.skipMetadataUpdate");

    boolean verbose;
    // reads and writes work with 4k of data at a time.
   int bs = 1024 * 4;
   // Work with 100 meg file.
   long size = 1024 * 1024 * 500;
   long sampleInterval = 10 * 1000;
   static Cache<Long, byte[]> cache;
   static Callable<byte[]> loader;
   static ArrayList<Long> longIndexList; 
   boolean enableCache = true; 

   public static void main(String[] args) {

Should I approach this differently? It runs fine in the IDE

EDIT 3: I moved java file to a new package under src/main/java, package name is DiskCharacterize, class name DiskMark. Still does not work.

I built it with this build.gradle

  /**
   * Guava compatibility adapter.
   *
   * The tests are forked from Guava commit e370dde.
   */
  apply plugin: 'application'
  dependencies {
    compile project(':caffeine')
    compile libraries.guava

    testCompile test_libraries.junit
    testCompile test_libraries.truth
    testCompile test_libraries.easymock
    testCompile test_libraries.guava_testlib
    compile group: 'org.apache.activemq', name: 'activemq-kahadb-store', version: '5.13.3'
    compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.13.3'
  }

  jar.manifest {
    name 'com.github.ben-manes.caffeine.guava'
    instruction 'Import-Package',
      'com.google.common.cache',
      'com.github.benmanes.caffeine.cache',
      'com.github.benmanes.caffeine.cache.stats'
    instruction 'Export-Package',
      'com.github.benmanes.caffeine.guava'
  }


  jar {
      baseName = 'gs-gradle'
      version =  '0.1.0'
  }

  tasks.withType(Javadoc) {
    options.addStringOption('Xdoclint:none', '-quiet')
  }
  repositories {
     mavenCentral()
  }
  test {
    systemProperty 'guava.osgi.version', versions.guava
    systemProperty 'caffeine.osgi.jar', project(':caffeine').jar.archivePath.path
    systemProperty 'caffeine-guava.osgi.jar', project(':guava').jar.archivePath.path
  }
  mainClassName = 'DiskCharacterize.DiskMark'

I switch over to the /build/libs/ directory where gs-gradle-0.1.0.jar exists. I populate this dir with this build.gradle

  /**
   * Guava compatibility adapter.
   *
   * The tests are forked from Guava commit e370dde.
   */
  apply plugin: 'application'
  /*dependencies {
    compile project(':caffeine')
    compile libraries.guava

    testCompile test_libraries.junit
    testCompile test_libraries.truth
    testCompile test_libraries.easymock
    testCompile test_libraries.guava_testlib
    compile group: 'org.apache.activemq', name: 'activemq-kahadb-store', version: '5.13.3'
    compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.13.3'
  }

  jar.manifest {
    name 'com.github.ben-manes.caffeine.guava'
    instruction 'Import-Package',
      'com.google.common.cache',
      'com.github.benmanes.caffeine.cache',
      'com.github.benmanes.caffeine.cache.stats'
    instruction 'Export-Package',
      'com.github.benmanes.caffeine.guava'
  }*/

  jar {
      baseName = 'gs-gradle'
      version =  '0.1.0'
  }

  tasks.withType(Javadoc) {
    options.addStringOption('Xdoclint:none', '-quiet')
  }
  repositories {
     mavenCentral()
  }
  /*test {
    systemProperty 'guava.osgi.version', versions.guava
    systemProperty 'caffeine.osgi.jar', project(':caffeine').jar.archivePath.path
    systemProperty 'caffeine-guava.osgi.jar', project(':guava').jar.archivePath.path
  }*/
  mainClassName = 'DiskCharacterize.DiskMark'

it fails with

    xxxxxx$ gradlew run
    :compileJava UP-TO-DATE
    :processResources UP-TO-DATE
    :classes UP-TO-DATE
    :run
    Error: Could not find or load main class DiskCharacterize.DiskMark
    :run FAILED

    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ':run'.
    > Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

What is going on? I need this to run and can't for the life of me figure it out

EDIT4: gradle run does not work, even with apply plugin: 'java' added, the plugin application implicitly calls java plugin anyway.

However, I got it running. I went to ../build/distributions/ folder which had both guava-2.3.1-SNAPSHOT.tar & guava-2.3.1-SNAPSHOT.zip, I unzipped the zip file, found a bash file and simply executed it; program ran. Why gradle run doesn't work is unknown to me (I still really want to understand that if you have any ideas). Thank you all for you help btw, thank you Ben M.

Edited Answer Based on New Information:

Based on the Gradle Application Plugin Documentation the run command runs against the "main source set". The class you are trying to run is under src/test, which isn't part of the Gradle default source locations (and based on the name, would be part of the test source location if it was). Look at the Gradle Java Plugin documention (45.2) for a better description on what source sets are and how to alter them than I could possible give here :)

Short answer/suggestion: move your package and file into a source folder called src/main/java from src/test to allow Gradle run to find the class where it expects to (you could alternatively reconfigure the source sets, but that seems like overkill for your case)

Old Answer (Based on Flawed Assessment of Typo attempting to Use Library-Provided Class):

I am assuming that you are trying to run the class org.apache.activemq.store.kahadb.disk.util.DiskMark as your main class. Based on the name of the class, I am guessing you are trying to use this from the activemq-kahadb dependency in your build.gradle file (as opposed to writing it yourself with a nearly identical package structure to the activemq-kahadb structure)

For the version you are dependent on, there does not appear to be a class with that name (based on the package tree ). I didn't find any version of ActiveMQ with a class of that name. There is a class called org.apache.activemq.store.kahadb.disk.util.DiskBenchmark which contains a main method - was that what you meant to invoke?

Looking at the DiskMark source you have up and the error message you are getting, the fully qualified name of the DiskMark class (ie, package name + class name) is: org.apache.activemq.store.kahadb.disk.util.DiskMark , not DiskCharacterize.DiskMark . Hence, the class is unable to be found.

There are two solutions:

Solution #1: Tell gradle to look for the right class:

mainClassName = 'org.apache.activemq.store.kahadb.disk.util.DiskMark'

Additionally, as a matter of project organization, the storage location of each file in your project should match the package of your class. In DiskMark 's case, the path should be:

%ProjectRoot%/src/main/java/org/apache/activemq/store/kahadb/disk/util/DiskMark.java

Solution #2: Change your class's packaging (and anything else that would need to match)

package DiskCharacterize;
//...
public class DiskMark {
    //...
}

File location:

%projectRoot%/src/main/java/DiskCharacterize/DiskMark.java

This solution is valid, but your current package, DiskCharacterize , does not obey java stylistic conventions (and makes DiskMark look like an inner class to those not familiar with your code).

As a result, I highly suggest combining both solutions and changing your package name to use an all lowercase package name. org.tarek.diskcharacterize would be a very appropriate name for your package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM