我的目標是構建我的程序並能夠從cmd行執行。 當我清理構建的應用程序,我得到以下錯誤,當我運行它:使用jar文件運行Java程序(Netbeans/Maven) - 需要幫助
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>[email protected]</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
THX對您有所幫助。
「由於:java.lang.ClassNotFoundException:com.google.api.client.json.JsonFact ory」Clealy指出,您從類路徑中遺漏了這個。 – dosdebug
我編譯後,我得到了我的目標目錄如下: 的CheckStyle-求CacheFile 的CheckStyle-checker.xml 的CheckStyle-header.txt 的CheckStyle,爲result.xml 類 爲dfareporting-CMDLINE樣本-1.0.0.jar findbugsXml.xml generated-sources maven-archiver surefire 因此,基於此,運行它的命令應該是什麼? – Rgonomike
你看到是否有你的jar打包的libs?或者,如果你從你的pom.xml引用它們到正確的路徑?因爲我不認爲如果你的庫被打包到jar中。 – dosdebug