0

我的目标是构建我的程序并能够从 cmd 行执行。当我清理构建应用程序时,运行它时出现以下错误:

    C:\Users\Jonny\Desktop>java -cp dfareporting-cmdline-sample-1.0.0.jar com.google
.api.services.samples.dfareporting.cmdline.DfaReportingSample
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/api/client
/json/JsonFactory
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.google.api.client.json.JsonFact
ory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        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)
        ... 6 more

我不是 java 开发人员,但我知道这是由于 pom.xml 配置而发生的(希望我理解 ;))

这是我的 POM 配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.google.apis-samples</groupId>
  <artifactId>dfareporting-cmdline-sample</artifactId>
  <version>1.0.0</version>
  <name>Example for the DFA Reporting API using JSON and OAuth 2.0</name>

  <url>http://code.google.com/p/google-api-java-client/</url>

  <issueManagement>
    <system>code.google.com</system>
    <url>http://code.google.com/p/google-api-java-client/issues/list</url>
  </issueManagement>

  <inceptionYear>2012</inceptionYear>

  <prerequisites>
    <maven>2.0.9</maven>
  </prerequisites>

  <scm>
    <connection>scm:hg:https://samples.google-api-java-client.googlecode.com/hg/</connection>
    <developerConnection>scm:hg:https://samples.google-api-java-client.googlecode.com/hg/</developerConnection>
    <url>http://code.google.com/p/google-api-java-client/source/browse/?repo=samples</url>
  </scm>

  <developers>
    <developer>
      <id>jdilallo</id>
      <name>Joseph DiLallo</name>
      <email>jdilallo@google.com</email>
      <organization>Google</organization>
      <organizationUrl>http://www.google.com</organizationUrl>
      <roles>
        <role>owner</role>
        <role>developer</role>
      </roles>
      <timezone>-5</timezone>
    </developer>
  </developers>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.google.api.services.samples.dfareporting.cmdline.DfaReportingSample</mainClass>
          <systemProperties>
            <systemProperty>
              <key>java.util.logging.config.file</key>
              <value>logging.properties</value>
            </systemProperty>
          </systemProperties>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <configLocation>../checkstyle.xml</configLocation>
          <consoleOutput>true</consoleOutput>
          <failOnViolation>false</failOnViolation>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <failOnError>false</failOnError>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <finalName>${project.artifactId}-${project.version}</finalName>
  </build>
  <dependencies>
    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-dfareporting</artifactId>
      <version>v1.1-rev14-1.15.0-rc</version>
    </dependency>
    <dependency>
      <groupId>com.google.http-client</groupId>
      <artifactId>google-http-client-jackson2</artifactId>
      <version>${project.http.version}</version>
    </dependency>
    <dependency>
      <groupId>com.google.oauth-client</groupId>
      <artifactId>google-oauth-client-jetty</artifactId>
      <version>${project.oauth.version}</version>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>14.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.directory.studio</groupId>
      <artifactId>org.apache.commons.io</artifactId>
      <version>2.4</version>
    </dependency>
    <dependency>
      <groupId>net.sf.opencsv</groupId>
      <artifactId>opencsv</artifactId>
      <version>2.3</version>
    </dependency>
  </dependencies>
  <properties>
    <project.http.version>1.15.0-rc</project.http.version>
    <project.oauth.version>1.15.0-rc</project.oauth.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

我现在应该怎么做才能从 cmd 行执行 jar 文件?

编辑:我已经尝试过:

C:\Users\Jonny\Desktop\test>java -cp dfareporting-cmdline-sample-1.0.0.jar;./cla
sses/* com.google.api.services.samples.dfareporting.cmdline.DfaReportingSample
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/api/client
/googleapis/json/GoogleJsonResponseException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.google.api.client.googleapis.js
on.GoogleJsonResponseException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        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)
        ... 6 more

谢谢你的帮助。

4

2 回答 2

1

将此添加到您的 pom.xml 文件中:

<build> 
<plugins> 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-dependency-plugin</artifactId> 
<executions> 
<execution> 
<id>copy-dependencies</id> 
<phase>prepare-package</phase> 
<goals> 
<goal>copy-dependencies</goal> 
</goals> 
<configuration> 
<outputDirectory>${project.build.directory}/dependency-jars</outputDirectory> 
<overWriteReleases>false</overWriteReleases> 
<overWriteSnapshots>false</overWriteSnapshots> 
<overWriteIfNewer>true</overWriteIfNewer> 
</configuration> 
</execution> 
</executions> 
</plugin> 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-jar-plugin</artifactId> 
<configuration> 
<archive> 
<manifest> 
<addClasspath>true</addClasspath> 
<mainClass>FULLPACKAGENAME.CLASSNAME</mainClass> 
<classpathPrefix>dependency-jars/</classpathPrefix> 
</manifest> 
<manifestEntries> 
<Class-Path>.</Class-Path> <!-- HERE IS THE IMPORTANT BIT --> 
</manifestEntries> 
</archive> 
</configuration> 
</plugin> 
</plugins> 
</build>

然后首先通过 maven 清理,然后构建为 Maven。

于 2013-06-04T10:34:49.893 回答
1

您需要将所有库添加到类路径。

例如,如果您的所有依赖项都lib在工作目录的文件夹中,那么您的命令应该是:

java -cp dfareporting-cmdline-sample-1.0.0.jar;./lib/* com.google.api.services.samples.dfareporting.cmdline.DfaReportingSample

UPD。

还有其他解决方案。

您应该将此插件添加到您的 pom.xml 中:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>attached</goal>
      </goals>
      <phase>package</phase>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>Your.Main.class</mainClass>
          </manifest>
          <manifestEntries>
            <Premain-Class>Your.agent.class</Premain-Class>
            <Agent-Class>Your.agent.class</Agent-Class>
            <Can-Redefine-Classes>true</Can-Redefine-Classes>
            <Can-Retransform-Classes>true</Can-Retransform-Classes>
          </manifestEntries>
        </archive>
      </configuration>
    </execution>
  </executions>
</plugin>

然后只需从这个插件运行汇编目标,您将在一个 jar 中获得所有代码和库。

您也可以从命令行执行此操作:

> mvn org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-2:assembly
于 2013-06-04T08:34:24.643 回答