jar cvf hello.jar HelloWorld.class config.properties
jar umf MANIFEST.MF hello.jar
java -jar hello.jar
读取jar包中的配置文件
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test {
public String getProper(){
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
//加载带包路径的配置文件
//this.getClass().getResourceAsStream("config.properties");
Properties p = new Properties();
try {
p.load(stream);
String m = p.getProperty("name");
return m;
} catch (IOException e) {
e.printStackTrace();
return "读取异常";
}
}
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.getProper());
//System.out.println(System.getProperty("user.dir"));
}
}
jar umf MANIFEST.MF hello.jar
java -jar hello.jar
读取jar包中的配置文件
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test {
public String getProper(){
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
//加载带包路径的配置文件
//this.getClass().getResourceAsStream("config.properties");
Properties p = new Properties();
try {
p.load(stream);
String m = p.getProperty("name");
return m;
} catch (IOException e) {
e.printStackTrace();
return "读取异常";
}
}
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.getProper());
//System.out.println(System.getProperty("user.dir"));
}
}