1

I would like to run my java class with cmd command. So, I have the next structure:

..
lib/org.json.jar
src/main/java/Parser.java

Now, after maven build I have some folders in the target:

classes/parser.class
lib/org.json.org

So, now I trying to execute the next command:

java - cp  package.report

But I have the next error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException

I trying execute also this command : java -cp "org.json.jar" package.report and got this error:

Error: Could not find or load main class package.report

What am I doing wrong?

1
  • Were you able to resolve the problem and get your Java class to run? Commented Jan 20, 2016 at 1:40

4 Answers 4

1

It looks like you need to do 2 things:

  1. Get the JAR file you need correctly added to your classpath, which requires a full reference to the actual file: -cp "the/full/path/to/the/lib/directory/andTheCompleteJarFilename.jar"

  2. Get your compiled classes on your classpath, which just requires the path to your root /classes directory: -cp "the/full/path/to/your/classes/directory"

To combine both of these together in your classpath specification, simply use a semicolon as the separator: -cp "the/jar/file/path/andFullName.jar;the/path/to/the/class/directory"

Finally, adding this into the java run command, it will look something like this: java -cp "jar/path/name.jar;full/path/to/class/dir" full.package.structure.path.AndTheClassName

This should get you up and running

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

Comments

0

You are overriding the classpath with -cp, try

java -cp "path-to-lib/*" package.report

Look at this question on how to use it Setting multiple jars in java classpath

Comments

0

You will need to build a single jar file with maven if you want to run a Main method from the command line with jara -jar my.jar

You'll need a pom like below

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <parent>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>...</artifactId>
  <packaging>jar</packaging>

  <properties>
    <encoding>UTF-8</encoding>
  </properties>

  <dependencies>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.5</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptors>
            <descriptor>src/assembly/assembly.xml</descriptor>
          </descriptors>
          <archive>
            <manifest>
              <mainClass>com....commandLineClssTestRunner</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

then an assembly.xml like this

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <id>jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <unpack>true</unpack>
      <unpackOptions>
        <excludes>
          <exclude>META-INF/spring.handlers</exclude>
          <exclude>META-INF/spring.schemas</exclude>
        </excludes>
      </unpackOptions>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <files>
    <file>
      <source>${project.basedir}/src/main/resources/META-INF/spring.handlers</source>
      <outputDirectory>META-INF</outputDirectory>
    </file>
    <file>
      <source>${project.basedir}/src/main/resources/META-INF/spring.schemas</source>
      <outputDirectory>META-INF</outputDirectory>
    </file>
    <file>
      <source>${project.basedir}/src/main/resources/logback.xml</source>
    </file>
  </files>
</assembly>

then you can run the code with

java -jar my.jar

Comments

0

It depends really what you are after? If you want to uberjar which is easy to run, you can use the Apache Maven Shade Plugin. This will allow you to create a single jar file with everything bundled up, ready to run.

If on the other hand, you just need the classpath in order to run some adhoc tests on your code, you can use the Maven dependency plugin to get a proper classpath string listing all your dependencies using the right path separator depending on your OS. Just type maven dependency:build-classpath. You can then take this path and use it within your call to java.

Comments

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.