0

Hi i Have a maven project used three libraries: junit, commons-lang, org.apache.commons My program is located in location pl.parser.example.MainClass (with main method) and three others : Currency.java, CurrencyPojo.java, CurrencyRates.java

My main method:

 public static void main(String[] args){
  try {
    String type = "c";

    String currencyCode = args[0];
    String inputStartDate = args[1];
    String inputEndDate = args[2];
...

I can't run this program from command line, i build it with maven to jar file and try:

java -cp AreYouExample.jar pl.parser.example.MainClass EUR 2013-01-28 2013-01-31

But i got exception:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/ma
th3/stat/descriptive/DescriptiveStatistics
        at pl.parser.example.CurrencyRates.obtainAvgPurchaseRate(
CurrencyRates.java:33)
        at pl.parser.example.MainClass.main(MainClass.java:34)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.stat.descr
iptive.DescriptiveStatistics
        at java.net.URLClassLoader$1.run(Unknown Source)...

I think that runner can't see library which i used in program (to get averange count from math)

I don'thave a manifest file, and classpath file. When I tried run jar with:

    ...> java MainClass EUR 2013-01-28 2013-01-31

Then i got exception:

Error: Could not find or load main class MainClass.java

But when i will go to folder with MainClass.java and run javaC MainClass.java... Then compiller cant find symbols used in program because sumbols are located in other class (in this folder/package)

I think that i should insert a main class i suggested this case in stack overflow, build in pom file but that not works.

EDIT: Now it works, i made a change in POM file like in other overstackflow topic I insertet this code:

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.0</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>your.main.Class</mainClass>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

Community
  • 1
  • 1
MikeB
  • 37
  • 2
  • 9

4 Answers4

0

You're only specifying your jar in the classpath when you're launching the app java -cp AreYouExample.jar so you need to add the apache-commons lib to the classpath as well.

Usually when building and preparing your application for distribution, I'd have a lib directory and configure maven copy your dependencies in there, so my startup script can add them all to the classpath and launch the app.

Morfic
  • 15,178
  • 3
  • 51
  • 61
  • But how to add classPath in java project? – MikeB Mar 26 '14 at 11:42
  • 1
    You're already adding your jar to the classpath `java -cp AreYouExample.jar`. You can have multiple jars separated by `;` on windows and `:` on *nix, like `java -cp jar1;jar2;jarN com.sample.MyClass`. You can read more about it here: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html – Morfic Mar 26 '14 at 11:48
0

you need a Main class in Manifest file in your jar that has been created by maven.

Manifest-Version: 1.0
Main-Class: com.main class name

it clearly is not able to start executing the main program in your jar

Error: Could not find or load main class MainClass.java

Create a Manifest file and specify your Main class there. Then use the classpath option -cp to pass any values to your jar like property file. All the common library jars will be loaded by maven automatically when you execute the jar from the command line.

vikeng21
  • 543
  • 8
  • 28
0

If you only run you program without telling Java where all the library classes reside, you will exactly get that NoClassDefFoundError.

There are several possibilities to run your program correctly.

  • java -jar YourJarFile.jar

This command requires a manifest file inside the JAR file. This manifest must include the two properties Main-Class and Class-Path. For your program this class path property in the manifest file must contain the libraries that you are using

  • java -cp <jar-file> your.pkg.MainClass

This command explicitely states the class path in the command, which now must be simply your own JAR file. Then it also states the main class, whose main method should be run. That means that you do not need the Main-Class property anymore in the manifest. But you still need the Class-Path property for finding the used libraries.

  • java -cp <jar-file>;<lib1>;<lib2>;... your.pkg.MainClass

With this command you do not need any explicit manifest anymore. Simply put your own JAR file and the libraries into the excplicit class path (inside the comman).


With Maven you are able to tell the archiver how to create the manifest. Additionally, you can have Maven create the whole distribution. Have a look at the assembly plugin.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

I had a similar problem some time ago. If you run your maven from eclipse and you haven't defined your source folder in your project then your eclipse maven plugin won't know what to compile, so no classes will be added to jar(check your maven logs and see it adds your classes). So you should assure yourself that src/main/java is added to eclipse project. Check your project properties build path.