共计 5371 个字符,预计需要花费 14 分钟才能阅读完成。
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<String,String> 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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>My account</comment>
<entry key="displayName">Xuesong Bu</entry>
<entry key="accountNumber">1-234-567</entry>
</properties>
输入的日志: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 内容为:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="isFirstRun">Y</entry>
<entry key="welcomeMessage">111Hello newcomer</entry>
<entry key="farewellMessage">It'll all be different soon</entry>
</properties>
第一次运行输入:
welcomeMessage:111Hello newcomer
farewellMessage:It'll all be different soon
第二次运行输入:
welcomeMessage:Welcome back
farewellMessage:Things will be familiar now