0

I am very new to java and I was trying to use a package to sample some Chisquared distributed random variables . When I try to import the entire org.apache.commons.math3 package using

'import org.apache.commons.math3

I get an error and my code doesn't complile giving a import error but when I use import org.apache.commons.math3.distribution.ChiSquaredDistribution;

Is this because we have to import specific classes and we cant import entire libraries.

user3503589
  • 139
  • 8
  • 1
    try `import org.apache.commons.math3.*` , I'm not experienced with apache but the `*` normally imports all parts of the package. – Patrick Apr 02 '19 at 13:47
  • 1
    You can't import packages but you can import all classes within a package via `import your.package.*;` - note that this doesn't include subpackages. – Thomas Apr 02 '19 at 13:49
  • org.apache.commons.math3.distribution.ChiSquaredDistribution is a sub-package of import org.apache.commons.math3? Is that why when I import using import org.apache.commons.math3.* , I cant access ChiSquaredDistribution class? – user3503589 Apr 02 '19 at 13:52

2 Answers2

4

You can't import packages but you can import all classes within a package via import your.package.*; - note that this doesn't include subpackages.

Thus using import org.apache.commons.math3.*; you could use all classes in that package but not classes like ChiSquaredDistribution because that one is in the subpackage distribution. You'd need import org.apache.commons.math3.distribution.*; for that.

Alternatively you use a decent IDE and let it generate the import statements for you - no need to do that yourself (and you shouldn't use wildcard imports anyways)

Thomas
  • 87,414
  • 12
  • 119
  • 157
1

You should be able to import everything within the math3 folder by adding .* at the end.

import org.apache.commons.math3.*
Natalo77
  • 155
  • 7