【问题标题】:Java importing external library : Apache commons mathJava 导入外部库:Apache commons math
【发布时间】:2016-02-28 01:49:10
【问题描述】:

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

我仍然卡住了,我无法使以下代码工作:

    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 和终端。

【问题讨论】:

  • 公开你的类,类名是大写的,你可以尝试将jar文件复制到与java文件相同的文件夹中再试一次
  • 您好,感谢您的提议。但是,将 jar 文件复制到 java 文件所在的同一文件夹后,我仍然遇到相同的错误...我尝试了 javac -cp ./commons-math3-3.5.jar LinearAlgebraTest.java 和 javac LinearAlgebraTest.java,因为到了什么我从 man 中了解到 ./ 通常是默认路径,通常我不应该使用 -cp。
  • 我对在这篇文章中应该注意什么有点困惑。读完之后,我再次尝试了闲置的 javac -cp /usr/share/java/commons-math3-3.2.jar:。 linearAlgebraLearning.java 将 .jar 放入 ~/javalib javac -cp ~/javalib:。 linearAlgebraLearning.java 将 jar 放在 LinearAlgebra 为 javac -cp ./name_of_the_jar.jar: 的同一文件夹中。 LinearAlgebraTest.java 我不知道。怎么了。
  • 您需要将导入更改为以 * 结尾或(更好)显式导入 RealMatrix 类,即 import org.apache.commons.math3.linear.RealMatrix;

标签: java eclipse apache-commons


【解决方案1】:

我先安装了每个 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}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2021-12-12
    相关资源
    最近更新 更多