java - Spring项目不会从命令行运行

标签 java spring gradle

我一辈子都想不出如何让Spring项目在IDE之外运行。我正在使用Gradle。我上下搜索,包括pply插件:“应用程序”字样,我看到生成了Jars,但是当我通过gradlew run运行时,它死于说找不到文件。

1)我需要一个单独的build.gradle文件来运行它吗?在尝试运行之前,我不得不取出大量其他项目内容(必须使用#删除所有内容)

2)为什么不起作用?我什至需要一个build.gradle文件吗?您如何从Spring IDE转到以最简单的方式在命令行上运行的程序?

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'

使用剥离的build.gradle编辑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 

编辑2:
感谢您的回复。我使用了disk.util.DiskBenchmark并仅修改了该特定文件,因为我需要添加一个缓存,因此我的名称为DiskMark.Java。这是我的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) {

我应该采取不同的方法吗?它在IDE中运行良好

编辑3:
我将Java文件移动到src / main / java下的新软件包中,软件包名称为DiskCharacterize,类名称为DiskMark。 仍然不起作用

我用这个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'

我切换到gs-gradle-0.1.0.jar存在的/ build / libs /目录。我用这个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'

,失败,显示
    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

到底是怎么回事?我需要运行它,而无法终生解决

编辑4:
gradle运行不起作用,即使添加了apply插件:“java”,插件应用程序仍然隐式调用Java插件。

但是,我让它运行了。我去了../build/distributions/文件夹,其中同时包含guava-2.3.1-SNAPSHOT.tar和guava-2.3.1-SNAPSHOT.zip,我解压缩了zip文件,找到了一个bash文件并简单地执行了它。程序运行了。我不知道为什么gradle run不起作用(如果您有任何想法,我仍然很想明白)。谢谢大家的帮助,谢谢本·M。

最佳答案

根据新信息编辑的答案:
基于Gradle Application Plugin Documentation,运行命令针对“主源集”运行。您尝试运行的类位于src / test下,这不是Gradle默认源位置的一部分(并且基于名称,如果是,则将成为测试源位置的一部分)。请查看Gradle Java Plugin文档(45.2),以获取关于什么是源集以及如何更改它们的更好的描述,这比我在这里可能给出的要好:)
简短的回答/建议:将包和文件从src / test移到名为src / main / java的源文件夹中,以允许Gradle运行找到所需的类(您可以替代地重新配置源集,但这似乎有点过头了对于你的情况)
旧答案(基于对Typo尝试使用库提供的类的错误评估):

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?

关于java - Spring项目不会从命令行运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37735483/

相关文章:

java - 使用 nline.setText ("\n"附加新行时应用程序崩溃)

java - Spring Security SecurityContextHolder.getContext().getAuthentication() 返回 null

spring - 如何在grails中更改主页?

gradle - gradle的作用

spring-boot - Spring Boot 2.0.5 - Activemq 5.14.0 与带有 gradle 的设置记录器有关的问题

ABI 缺少 Android ndkBuild android_gradle_build

java - 如何将请求级别对象嵌入到@Context

java - 在两个限制之间反弹值的最佳方法是什么?

java - 测试从数据库插入、更新和删除的类

java - 类型不匹配 : cannot convert from List<Map<String, Object>> 到 List<Object>