apache commons: ClassNotFoundException

你。 提交于 2019-12-13 05:18:27

问题


I'm trying to benefite from the EnumeratedIntegerDistribution() from org.apache.commons.math3.distribution, as shown below.

I'm working wiht jdk7 , on windows Xp, running from Command Line, in one and only project folder

I do:

  • download commons-math3-3.2 and unpackage it to my folder, where my java is.
  • compile my source with (no errors no warnings) javac -cp commons-math3-3.2/commons-math3-3.2.jar CheckMe.java
  • run java CheckMe

Here is the demonstration:

import java.lang.Math.*; import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;

public class CheckMe {

public CheckMe() {

    System.out.println("let us check it out"); 
    System.out.println(generate_rand_distribution (10));
}

private static int[] generate_rand_distribution (int count){
    int[] nums_to_generate          = new int[]    { -1,   1,    0  };
    double[] discrete_probabilities = new double[] { 0.4, 0.4, 0.2  };
    int[] samples = null;

    EnumeratedIntegerDistribution distribution = 
    new EnumeratedIntegerDistribution(nums_to_generate, discrete_probabilities);

    samples = distribution.sample (count);

    return (samples);
}   

public static void main (String args[]) { 
    System.out.println("Main: ");
    CheckMe  animation = new CheckMe();  
} 

}

However I have got:

ClassNotFoundException: org.apache.commons.math3.distribution.EnumeratedIntegerDistribution at line 18 - the call for EnumeratedIntegerDistribution()

If I run as I was just advised (below):

    java -cp   commons-math3-3.2/commons-math3-3.2.jar  CheckMe

I get error: impossible to find the principle class CheckMe

Thanks a lot in advance for your advise.


回答1:


You need to specify the classpath both when you compile and when you run. So running

java CheckMe.java 

is not enough. Also it should be

java CheckMe

assuming the class CheckMe is not in any package.

You need to execute

java -cp commons-math3-3.2/commons-math3-3.2.jar CheckMe

specifying the jars and other classes your CheckMe program needs.



来源:https://stackoverflow.com/questions/20954322/apache-commons-classnotfoundexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!