Java导入外部库:Apache commons math

时间:2015-11-25 10:11:24

标签: java eclipse apache-commons

看了很多讨论之后,例如:

我仍然卡住了,我无法使以下代码生效:

    import org.apache.commons.math3.linear;

class linearAlgebraLearning{
    public static void main(String[] args){


    // Create a real matrix with two rows and three columns, using a factory
    // method that selects the implementation class for us.
    double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}};
    RealMatrix m = MatrixUtils.createRealMatrix(matrixData);

    // One more with three rows, two columns, this time instantiating the
    // RealMatrix implementation class directly.
    double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}};
    RealMatrix n = new Array2DRowRealMatrix(matrixData2);

    // Note: The constructor copies  the input double[][] array in both cases.

    // Now multiply m by n
    RealMatrix p = m.multiply(n);
    System.out.println(p.getRowDimension());    // 2
    System.out.println(p.getColumnDimension()); // 2

    // Invert p, using LU decomposition
RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();

    }
}

所以这就是我一步一步做的事情。

首先我使用

安装了Apache
sudo apt-get install libcommons-math3-java

然后我看了一下commons-math3-java的安装位置。

dpkg -L libcommons-math3-java 
/.
/usr
/usr/share
/usr/share/maven-repo
/usr/share/maven-repo/org
/usr/share/maven-repo/org/apache
/usr/share/maven-repo/org/apache/commons
/usr/share/maven-repo/org/apache/commons/commons-math3
/usr/share/maven-repo/org/apache/commons/commons-math3/3.2
/usr/share/maven-repo/org/apache/commons/commons-math3/3.2/commons-math3-3.2.pom
/usr/share/maven-repo/org/apache/commons/commons-math3/debian
/usr/share/maven-repo/org/apache/commons/commons-math3/debian/commons-math3-debian.pom
/usr/share/doc
/usr/share/doc/libcommons-math3-java
/usr/share/doc/libcommons-math3-java/changelog.Debian.gz
/usr/share/doc/libcommons-math3-java/copyright
/usr/share/java
/usr/share/java/commons-math3.jar
/usr/share/maven-repo/org/apache/commons/commons-math3/3.2/commons-math3-3.2.jar
/usr/share/maven-repo/org/apache/commons/commons-math3/debian/commons-math3-debian.jar
/usr/share/java/commons-math3-3.2.jar

然后我使用(如Install commons math library for java in Ubuntu中所述)

javac -cp .:/usr/share/java/commons-math3-3.2.jar linearAlgebraLearning.java 

但是我仍然是导入错误消息:

linearAlgebraLearning.java:1: error: cannot find symbol
import org.apache.commons.math3.linear;

由于编译器没有找到类(如RealMatrix),因此还有其他错误。我知道这个问题已被多次询问过。这里的人可能已经厌倦了看到这个问题......但如果你能帮助我,我会很高兴。

Ps:因为我的linux发行版上有一些Eclipse的错误,我不使用IDE并使用gedit和终端。

1 个答案:

答案 0 :(得分:0)

我首先安装了每个libcommons软件包:

sudo apt install libcommons\*

然后设置类路径:

export CLASSPATH="/usr/share/java/commons-math3.jar:/usr/share/java/commons-lang3.jar"

您的Java应该会自动选择它。我使用jshell对其进行了测试,例如,它能够自动完成/导入BlockRealMatrix:

jshell> import org.apache.commons.math3.linear.BlockRealMatrix
jshell> BlockRealMatrix foo = new BlockRealMatrix(2, 2);
foo ==> BlockRealMatrix{{0.0,0.0},{0.0,0.0}}