我的目标是构建我的程序并能够从 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
谢谢你的帮助。