0

My goal is to build my program and being able to execute from the cmd line. When I clean a build the app, I get the following error when i run it:

    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

I'm not a java developer but I understood that it happens because of the pom.xml configuration (well hopefully I understood ;))

Here is my POM config:

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

What should I do now to be able to execute the jar file from cmd line?

EDIT: I already tried:

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 for you help.

8
  • "Caused by: java.lang.ClassNotFoundException: com.google.api.client.json.JsonFact ory" Clealy states, you are missing this from your classpath. Commented Jun 4, 2013 at 8:38
  • After I compile, I get the followings in my target directory: checkstyle-cachefile checkstyle-checker.xml checkstyle-header.txt checkstyle-result.xml classes dfareporting-cmdline-sample-1.0.0.jar findbugsXml.xml generated-sources maven-archiver surefire So based on that, what should be the command to run it? Commented Jun 4, 2013 at 9:13
  • Do you see if there are libs packaged with your jar? Or if you are referencing them from your pom.xml to the right path? Because I don't think if your libs are being packaged into the jar. Commented Jun 4, 2013 at 9:17
  • To run the jar, you do : java -jar JARFILE.jar and if you don't have manifest file then java -cp JARFILE.jar package.name.CLASSNAME Commented Jun 4, 2013 at 9:18
  • None if these commands work... The classes are all in the jar package:` CreateActivityReport.class CreateCompleteReport.class CreateFloodlightReport.class CreateStandardReport.class DfaReportingSample.class DownloadReportFile.class GenerateReportFile.class GetAllReports.class GetAllUserProfiles.class GetDimensionValues.class RefreshToken.class UpdateCompleteReport.class UrlDownload.class Commented Jun 4, 2013 at 9:27

2 Answers 2

1

add this to your pom.xml file:

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

Then first clean by maven and then build as Maven.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to add all your libraries to classpath.

For example, if all of your dependencies are in lib folder in working dir, then your command should be:

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

UPD.

There is other solution.

You should add this plugin to your 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>

And then just run assembly goal from this plugin and you will get all yours code and libs in one jar.

You also can do this from command line:

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

3 Comments

Shouldn't it be different if it's a Maven project? In my code I have these import statements and the code runs fine with Netbeans import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory;
@Rgonomike, nope, you should add all libs to your classpath... But, there is another solution. It's require some changes in your pom.xml. See my update.
Thanks, Mr.777 gave me a solution. The dependencies were not downloaded. Now they are but they are not in the Jar itself, which is fine by me. I won't dig further for the moment. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.