idea中编写工具类并打成jar包

无入参形式

1、新建testjar文件夹

image.png

2、编写工具类
package testjar;

public class TestSum {
    /**
     * 无入参,直接调用
     * @return
     */
    public int testsum() {
        int sum = 1;
        System.out.println("无入参,直接jar -jar命令调用");
        return sum;
    }
    
    public static void main(String[] args) {
        TestSum test = new TestSum();
        test.testsum();
    }
}

3、打jar包
  • 依次选择file-project structrue-artifacts-from modules with dependencies

image.png

  • 选择正确的工具类方法,点击okimage.png
  • 选择build-build artifacts

image.png

  • 直接选择build

image.png

  • 得到jar包

image.png

  • 验证jar包

    jar包可以通过命令运行了
    image.png

通过命令传入参

工具类TestSum2和main调用的代码如下

package testjar;

public class TestSum2 {
    public int testsum2(int a , int b){
        int sum2;
        sum2 = a + b;
        System.out.println("两数之和是:"+ sum2);

        return sum2;
    }

    public static void main(String[] args) {

        int sum2;


        String a = args[0];
        System.out.println("第一个参数" + a);

        String b = args[1];
        System.out.println("第二个参数" + b);
        TestSum2 test = new TestSum2();
        sum2 = test.testsum2(Integer.parseInt(a), Integer.parseInt(b));
        


    }
}

  • main里面使用了Integer.parseInt()方法,将读取到的String类型转换成int类型
  • 打包方式跟1中的打包方式一样

通过配置文件传入参

testsum3工具类testSum3和main方法如下

package com.testjar;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.Set;

public class TestSum3 {
    /**
     * 测试方法:用来接受入参
     * @param a
     * @param b
     * @return
     */
    public int testsum3(int a, int b) {
        int sum3;
        sum3 = a + b;
        System.out.println("两数之和是:" + sum3);
        return sum3;
    }

    /**
     * 读取配置文件,获得配置文件的ResourceBundle对象
     * @return
     */
    public ResourceBundle readpro() throws IOException {
        //加号后面是自定义路径,这个路径表示当时jar包同级目录下的test.properties文件。
        String proFilePath = System.getProperty("user.dir") +"\\test.properties";
        System.out.println(proFilePath);
        //获取输入流文件对象
        FileInputStream file = new FileInputStream(proFilePath);
        //新建BufferedInputStream配置流对象
        BufferedInputStream inputstream = new BufferedInputStream(file);
        //读取配置文件
        ResourceBundle rb = new PropertyResourceBundle(inputstream);

        //getBundle方式:读取resources/目录下的application.properties;不需要写文件后缀,写文件前缀即可。
//        ResourceBundle rb = ResourceBundle.getBundle("test");

        //遍历输出所有的键值对
        for (String key : rb.keySet()) {
            String value = rb.getString(key);
            System.out.println(key + ":" + value);
        }
        return rb;
    }
    
    
    
    public static void main(String[] args) throws IOException {
        TestSum3 test = new TestSum3();
        ResourceBundle rb = test.readpro();
        //使用getString读取"a"和"b"键对应的值
        String a = rb.getString("a");
        String b = rb.getString("b");
        //配置项读取到的是String,需要转换成int
        int inta = Integer.parseInt(a);
        int intb = Integer.parseInt(b);
//        Set keys = rb.keySet();
        test.testsum3(inta, intb);

    }
}

其中读取配置文件有两种方式,一种是jar包内读取,配置文件在jar包,可以打开jar包修改

image.png前一种方式的读取配置文件的实现方法如下:

 //getBundle方式:读取resources/目录下的application.properties;不需要写文件后缀,写文件前缀即可。
//        ResourceBundle rb = ResourceBundle.getBundle("test");

这种方法不支持自定义路径,只能放在统计的resource里面image.png

  • 一种是将配置文件放在jar包外,放在与jar包同级的目录下,命名为test.properties

image.png

这种方式读取配置文件的实现方式如下:

# test.properties是配置文件名称
String proFilePath = System.getProperty("user.dir") +"\\test.properties";
        System.out.println(proFilePath);
        //1、获取输入流文件对象
        FileInputStream file = new FileInputStream(proFilePath);
        //2、新建BufferedInputStream配置流对象
        BufferedInputStream inputstream = new BufferedInputStream(file);
        //3、读取配置文件
        ResourceBundle rb = new PropertyResourceBundle(inputstream);

这两种方式最终都是获得读取对应配置文件的一个 ResourceBundle对象。 ResourceBundle对象有各种方法可以获取key和value:
rb.getString("a")就是获取配置文件中名叫a的key对应的值。main分别获取到ab的value,转换成int后调用testsum3方法。

  • 打jar的步骤可以与前面一致
  • 最终运行测试:
  • image.png

image.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值