运行jar包提示找不到.properties文件解决方法~

该程序通过配置文件设置忙闲时间比例,实现对CPU资源使用的调节。使用Java编写,通过循环实现指定时间内的CPU占用。

项目文件目录
CPUCostControl
-src
--default package
---CPUCostControl.java
-JRE System Library
-cpu_cost_control.properties

#CPUCostControl.java
import java.io.*;
import java.util.Properties;

public class CPUCostControl {

    public static void main(String[] args) throws IOException {
       
        Properties properties = new Properties();
        InputStream in = CPUCostControl.class.getClassLoader().getResourceAsStream("cpu_cost_control.properties");
        properties.load(in);
       
        int busyTime = Integer.parseInt(properties.getProperty("busyTime"));
        int idleTime = Integer.parseInt(properties.getProperty("idleTime"));
        long startTime = 0 ;
       
        System.out.println("busyTime:" + busyTime);
        System.out.println("idleTime:" + idleTime);
       
        while(true){
           
            startTime = System.currentTimeMillis();
            while((System.currentTimeMillis() - startTime) <= busyTime);

            try{
                Thread.sleep(idleTime);
            }catch(Exception e){
               
                System.out.println(e.getMessage());
            }
        }
    }
}

#cpu_cost_control.properties
busyTime=19
idleTime=1

注意:
1、InputStream in = CPUCostControl.class.getClassLoader().getResourceAsStream("cpu_cost_control.properties");
    如果将其写为FileInputStream in = new FileInputStream(new File("cpu_cost_control.properties"));
   则在运行jar包时会提示找不到cpu_cost_control.properties。
2、Manifest.mf文件中要包含Main-Class: CPUCostControl,否则运行jar时提示找不到main class.
3、运行jar命令为 java -jar test.jar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值