项目文件目录
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
运行jar包提示找不到.properties文件解决方法~
最新推荐文章于 2022-10-13 15:59:43 发布