Linux 单独编译运行Java文件(依赖外部jar包)

1 用于测试的简单java文件,依赖了apache的commons-math包

import org.apache.commons.math.Field;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.NormalDistributionImpl;

/**
 * @author KONGFANYI
 */
public class MathUtils {
    public static void main(String[] args) throws MathException {
        double miu = 1.3;
        doubel sigma = 1; 
        NormalDistributionImpl distribution = new NormalDistributionImpl(miu, sigma);
        double probability = distribution.cumulativeProbability(0, 0.75);
        System.out.println(probability );
    }
}

2 编译运行

运行的根目录

drwxr-xr-x 2 root root   57 7月  21 15:33 lib
-rw-r--r-- 1 root root 7535 7月  21 16:53 MathUtils.java

lib包内容

-rw-r--r-- 1 root root 988514 7月  12 16:16 commons-math-2.2.jar

编译命令

javac -cp ./lib/commons-math-2.2.jar MathUtils.java

编译后会产生.class字节码文件

drwxr-xr-x 2 root root   57 7月  21 15:33 lib
-rw-r--r-- 1 root root 6378 7月  21 18:56 MathUtils.class
-rw-r--r-- 1 root root 7535 7月  21 16:53 MathUtils.java

运行字节码文件

java -classpath .:lib/* MathUtils

额外补充往main中传参

java -classpath .:lib/* MathUtils param1 param2

字节码文件后面的参数param1, param2 会传入main中的String[] args数组中,可以再代码中获取到,多个参数使用空格分开。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容