2013-06-04 131 views
0

我的目標是構建我的程序並能夠從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對您有所幫助。

+0

「由於:java.lang.ClassNotFoundException:com.google.api.client.json.JsonFact ory」Clealy指出,您從類路徑中遺漏了這個。 – dosdebug

+0

我編譯後,我得到了我的目標目錄如下: 的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

+0

你看到是否有你的jar打包的libs?或者,如果你從你的pom.xml引用它們到正確的路徑?因爲我不認爲如果你的庫被打包到jar中。 – dosdebug

回答

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的。

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> 

然後就是從這個插件運行的彙編的目標,你會得到所有你的代碼和庫在一個罐子。

您也可以通過命令行做到這一點:

> mvn org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-2:assembly 
+0

不應該它如果它是一個Maven項目,會不一樣? 在我的代碼中,我有這些導入語句,並且代碼與Netbeans 'import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory;' – Rgonomike

+0

@Rgonomike,nope,你應該將所有庫添加到你的類路徑中......但是,還有另一種解決方案。這需要對你的pom.xml進行一些修改。查看我的更新。 – pbespechnyi

+0

謝謝,Mr.777給了我一個解決方案。沒有下載依賴關係。現在他們是但他們不在罐子裏,這對我來說很好。 我暫時不會進一步挖掘。 謝謝! – Rgonomike