java -cp命令
存在两个路径,其中psdir下com/test/training/路径下有Main.class和Student.class
另外路径,libdir下com/newtest/support/路径下有Other.class
在mydir下执行命令
//Windows
java -cp \psdir;\libdir com.test.training.Main
//Unix platforms
java -cp /psdir:/libdir com.test.training.Main
如果在psdir路径下,是training.jar包,包内com/test/training/路径下有Main.class和Student.class
另外路径,libdir下com/newtest/support/路径下有Other.class
在mydir下执行命令
//Windows
java -cp \psdir\training.jar;\libdir com.test.training.Main
//Unix platforms
java -cp /psdir/training.jar:/libdir com.test.training.Main
mydir路径下ourapp.jar,jar包内包含Manifest文件,以及com/test/training/路径下有Main.class和Student.class,com/newtest/support/路径下有Other.class
Manifest-Version: 1.0
Main-Class: com.test.training.Main
此时才可以执行
java -jar ourapp.jar
环境变量
//设置变量以及取出
Properties props = new Properties();
props.setProperty("displayName","Bu Xuesong");
String name = props.getProperty("displayName");
System.out.println("name:"+name);
String acctNum = props.getProperty("accountNumber");
System.out.println("acctNum:"+acctNum);
String nextPosition = props.getProperty("accountNumber","1");
System.out.println("nextPosition:"+nextPosition);
//获取系统变量
String username = System.getProperty("user.name");
System.out.println(username);
String userHome = System.getProperty("user.home");
System.out.println(userHome);
String osArchitecture = System.getProperty("os.arch");
System.out.println(osArchitecture);
String javaVendor = System.getProperty("java.vendor");
System.out.println(javaVendor);
Map map = System.getenv();
for (String key:map.keySet()) {
System.out.println("key:"+key+";value="+map.get(key));
}
String logName = System.getenv("LOGNAME");
System.out.println(logName);
将properties变量存入xml中
public static void XMLtest() {
Properties prop = new Properties();
prop.setProperty("displayName","Xuesong Bu");
prop.setProperty("accountNumber","1-234-567");
try(OutputStream out = Files.newOutputStream(Paths.get("/Users/buxuesong/Documents/svn_code/demo/prop.xml"))){
prop.storeToXML(out,"My account");
}catch (Exception e){
System.out.println(e.getClass().getSimpleName()+" | "+e.getMessage());
}
Properties inProp = new Properties();
try(InputStream in = Files.newInputStream(Paths.get("/Users/buxuesong/Documents/svn_code/demo/prop.xml"))){
inProp.loadFromXML(in);
}catch(Exception e){
System.out.println(e.getClass().getSimpleName()+" | "+e.getMessage());
}
String displayName = inProp.getProperty("displayName");
System.out.println("displayName:"+displayName);
String accountNumber = inProp.getProperty("accountNumber");
System.out.println("accountNumber:"+accountNumber);
}
生成的xml
My account
Xuesong Bu
1-234-567
输出的日志:
displayName:Xuesong Bu
accountNumber:1-234-567
默认Properties方式,当创建Properties的时候不必每次都去重新读区,可以通过之前的Properties构造方法来默认创建
public static void defaultProperties() {
Properties defaults = new Properties();
defaults.setProperty("position","1");
Properties props = new Properties(defaults);
String nextPos = props.getProperty("position");
int pos = Integer.parseInt(nextPos);
System.out.println("pos:"+pos);
props.setProperty("position",Integer.toString(++pos));
nextPos = props.getProperty("position");
System.out.println("nextPos:"+nextPos);
}
输出:
pos:1
nextPos:2
通过读取默认的Properties以及将期存储为xml,后期可以再次取出。
以下程序在第一次执行时从默认MyDefaultValues.xml取出内容,放入userProps中,然后再将userProps存储到文件userValues.xml中,第二次执行就可以从userValues取出上一次存储的值
public class Main {
public static void main(String[] args) {
try{
Properties defaultProps = new Properties();
try(InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("MyDefaultValues.xml")){
defaultProps.loadFromXML(inputStream);
}
Properties userProps = new Properties(defaultProps);
loadUserProps(userProps);
String welcomeMessage = userProps.getProperty("welcomeMessage");
String farewellMessage = userProps.getProperty("farewellMessage");
System.out.println("welcomeMessage:"+welcomeMessage);
System.out.println("farewellMessage:"+farewellMessage);
if(userProps.getProperty("isFirstRun").equalsIgnoreCase("Y")){
userProps.setProperty("welcomeMessage","Welcome back");
userProps.setProperty("farewellMessage","Things will be familiar now");
userProps.setProperty("isFirstRun","N");
saveUserProps(userProps);
}
}catch (Exception e){
System.out.println(e.getClass().getSimpleName()+" | "+e.getMessage());
}
}
private static Properties loadUserProps(Properties userProps){
Path userFile = Paths.get("/Users/buxuesong/Documents/svn_code/demo/userValues.xml");
if(Files.exists(userFile)){
try(InputStream inputStream = Files.newInputStream(userFile)){
userProps.loadFromXML(inputStream);
}catch (Exception e){
System.out.println(e.getClass().getSimpleName()+" | "+e.getMessage());
}
}
return userProps;
}
private static void saveUserProps(Properties userProps){
try(OutputStream outputStream = Files.newOutputStream(Paths.get("/Users/buxuesong/Documents/svn_code/demo/userValues.xml"))){
userProps.storeToXML(outputStream,null);
}catch (Exception e){
System.out.println(e.getClass().getSimpleName()+" | "+e.getMessage());
}
}
}
MyDefaultValues.xml内容为:
Y
111Hello newcomer
It'll all be different soon
第一次运行输出:
welcomeMessage:111Hello newcomer
farewellMessage:It'll all be different soon
第二次运行输出:
welcomeMessage:Welcome back
farewellMessage:Things will be familiar now