package org.apache.commons does not exist

29,040

This is the problem:

import org.apache.commons.math3;

That's trying to import a package - you can't do that. You have to either use a wildcard import:

import org.apache.commons.math3.*;

or import a specific type:

import org.apache.commons.math3.SomeTypeHere;

In your case it sounds like you actually want:

import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;

I've tried a sample class with just that import and the jar file downloaded from Apache, and it works fine.

Share:
29,040
Galia
Author by

Galia

Updated on July 25, 2020

Comments

  • Galia
    Galia almost 4 years

    I'd love to use EnumeratedIntegerDistribution() from org.apache.commons.math3.distribution, to get discrete probabilities distribution

    int[] nums_to_generate          = new int[]    { -1,   1,    0  };
    double[] discrete_probabilities = new double[] { 0.4, 0.4, 0.2  };
    

    I'm working wiht jdk7 , on windows Xp, running from Command Line

    I do:

    • add to my source file

      import org.apache.commons.math3; 
      
    • download commons-math3-3.2 and unpackage it to my current folder
    • compile my source with the classpath: (either)

      javac -cp ./commons-math3-3.2/commons-math3-3.2.jar:. ConflictsAnimation.java
      javac -cp   commons-math3-3.2/commons-math3-3.2.jar   ConflictsAnimation.java
      

    Still I've got a mysterious

        "error: package org.apache.commons does not exist"
    

    Who knows what happens ? I really need a help.

    Note:

    compilation (and run) is OK without the classpath and without the import of "apache" and call to numeratedIntegerDistribution().

    compilation with the classpath and without the "appache"s give nonsense errors.

    Thanks a lot in advance for your great skills, the programmers!


    Here is short demonstration:

    import java.lang.Math.*;
    import org.apache.commons.math3;
    
    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();  
        } 
    }
    
    • Jon Skeet
      Jon Skeet over 10 years
      What exactly are these "nonsense errors"? Can you show a short but complete program demonstrating the problem?
    • Dave Newton
      Dave Newton over 10 years
      You can't just import a package, you need to import actual classes. You'll need to provide more information for help, though, like where the jar and your classes are located etc.
    • Galia
      Galia over 10 years
      commons-math3-3.2 is located in my current folder together with my source file. I'm adding a short program having the same problem.
    • Dave Newton
      Dave Newton over 10 years
      If it's in your current folder then why are you giving it a path to the jar file?
  • Galia
    Galia over 10 years
    Hi Jon, the compilation was fin, however this apache might be not found at run what seems to cause class not found exception. stackoverflow.com/questions/20954322/… Thanks you for anything.
  • Jon Skeet
    Jon Skeet over 10 years
    @Galia: Yes, as per the answer there you need the jar file on the classpath when running it, too.
  • Galia
    Galia over 10 years
    yes it resolved the problem. But created somehow another one: any of my java executables if I run them now (which are not using apache) fail with the error of not finding the principle class. Nether restarting nor using "." as -cp argument didin't change this. May it be connected to the fact that I have got now Sdk and Jdk, though Before runnig with clathpass flag this did no problem to my other java exectutables.
  • PhoneixS
    PhoneixS almost 10 years
    If the problem is other, create a new question. And if this answer answer the original question mark it as accepted.