Vahe Akhsakhalyan
Vahe Akhsakhalyan

Reputation: 2186

java run command from cmd

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?

Upvotes: 1

Views: 129

Answers (4)

Hardy
Hardy

Reputation: 19129

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.

Upvotes: 0

Essex Boy
Essex Boy

Reputation: 7950

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

Upvotes: 0

Sean Mickey
Sean Mickey

Reputation: 7716

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

Upvotes: 1

awsome
awsome

Reputation: 2153

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

Upvotes: 0

Related Questions