我这里报告的异常是:F:\temp\java>java -jar test.jar helle errors: null java.lang.NullPointerException at Property.getHost(Property.java:17) at Property.main(Property.java:45) null errors: null java.lang.NullPointerException at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at Property.setHost(Property.java:32) at Property.main(Property.java:46) errors: null java.lang.NullPointerException at Property.getHost(Property.java:17) at Property.main(Property.java:47) null
to chesterwoo() 兄:我运行过你的代码,其实无论是你提供的代码,还是楼主原本的代码,还是我提供的代码,在处理properties文件的时候使用的都是stream方式,这并没有什么不同。真正的问题在于:如何定位properties文件,而非怎样去处理它,定位之后,肯定是采用stream的方式去处理的。我的定位方式只是其中一种,方法肯定是很多的,我一直从事B/S结构的J2EE开发,对桌面开发并不熟悉,所以我用的方法是B/S下常用的方法,不过我的想法也很简单:实现了功能就行了。to xyxu1982(云娜):晚餐时间,俺却胃痛的难受,对于你的问题,我修行有限,无力去深究,请自行参阅JDK的API说明。
to pandaatgd(国宝) :其实你自己查JDK的API说明就能找到。Key Description of Associated Value java.version Java Runtime Environment version java.vendor Java Runtime Environment vendor java.vendor.url Java vendor URL java.home Java installation directory java.vm.specification.version Java Virtual Machine specification version java.vm.specification.vendor Java Virtual Machine specification vendor java.vm.specification.name Java Virtual Machine specification name java.vm.version Java Virtual Machine implementation version java.vm.vendor Java Virtual Machine implementation vendor java.vm.name Java Virtual Machine implementation name java.specification.version Java Runtime Environment specification version java.specification.vendor Java Runtime Environment specification vendor java.specification.name Java Runtime Environment specification name java.class.version Java class format version number java.class.path Java class path java.library.path List of paths to search when loading libraries java.io.tmpdir Default temp file path java.compiler Name of JIT compiler to use java.ext.dirs Path of extension directory or directories os.name Operating system name os.arch Operating system architecture os.version Operating system version file.separator File separator ("/" on UNIX) path.separator Path separator (":" on UNIX) line.separator Line separator ("\n" on UNIX) user.name User's account name user.home User's home directory user.dir User's current working directory
to wingtrace(暮寒.烟灰.风化物.PG世界最重要的是基础) thank you very much 我的API文档上面怎么没有,就是查不到才问你,谢谢
一般是properties路径问题,或根本没找到properties文件,会出现这个错误...查看一下你的jar,文件看suncertify.properties文件是否被打包进去...
2。main函数只在你测试代码的时候有用,打包进jar文件的时候就不需要了。
回答你2个问题
1.为了方便所以没写。
2.main函数打进包里这么会没用,我打包命令jar cvfm 其中m就是指定main函数的。
用法:jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目录] 文件名 ...
选项:
-c 创建新的存档
-t 列出存档内容的列表
-x 展开存档中的命名的(或所有的〕文件
-u 更新已存在的存档
-v 生成详细输出到标准输出上
-f 指定存档文件名
-m 包含来自标明文件的标明信息
-0 只存储方式;未用ZIP压缩格式
-M 不产生所有项的清单(manifest〕文件
-i 为指定的jar文件产生索引信息
-C 改变到指定的目录,并且包含下列文件:
如果一个文件名是一个目录,它将被递归处理。
清单(manifest〕文件名和存档文件名都需要被指定,按'm' 和 'f'标志指定的相同顺序。示例1:将两个class文件存档到一个名为 'classes.jar' 的存档文件中:
jar cvf classes.jar Foo.class Bar.class
示例2:用一个存在的清单(manifest)文件 'mymanifest' 将 foo/ 目录下的所有
文件存档到一个名为 'classes.jar' 的存档文件中:
jar cvfm classes.jar mymanifest -C foo/ .
少在这里倚老卖老,说不定你还要叫我师姐,今天不想吵架,快帮我改代码。
Manifest-Version: 1.0
Created-By: 1.4.1-rc (Sun Microsystems Inc.)
Main-Class: Property
class我估计是,配置文件未正确加载....你可以这样测试一下.......
try {
url = ClassLoader.getSystemClassLoader().getSystemResource(
"suncertify.properties");
properties_name = url.getPath();
System.out.println("Path:")+properties_name ;//--->测试文件路径是否正确
InputStream inputStream = new FileInputStream(properties_name);
properties.load(inputStream);
host = properties.getProperty("host");
}
catch (IOException e) {
System.out.println(e.getMessage());//------>输出异常消息
}
helle
errors: null
java.lang.NullPointerException
at Property.getHost(Property.java:17)
at Property.main(Property.java:45)
null
errors: null
java.lang.NullPointerException
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at Property.setHost(Property.java:32)
at Property.main(Property.java:46)
errors: null
java.lang.NullPointerException
at Property.getHost(Property.java:17)
at Property.main(Property.java:47)
null
import java.net.*;
import java.io.*;
class Property
{
private static Properties properties = new Properties();
// private static URL url=null;
private static String properties_name;
private static String host;
public static String getHost() {
try {
String str = System.getProperty("user.dir");
System.out.println("str= "+str); properties_name = str+"\\suncertify.properties";
System.out.println(properties_name); InputStream inputStream = new FileInputStream(properties_name);
properties.load(inputStream);
host = properties.getProperty("host"); }
catch (Exception e) {
System.err.println("getHost method errors: "+e.getMessage());
e.printStackTrace();
System.exit(1);
}
return host;
}
public static void setHost(String host) {
try {
OutputStream outputStream = new FileOutputStream(properties_name);
properties.setProperty("host", host);
properties.store(outputStream, "suncertify");
outputStream.close();
}
catch (Exception e) {
System.err.println("setHost method errors: "+e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args)
{
System.out.println("helle");
System.out.println(getHost());
setHost("127.0.0.1");
System.out.println(getHost());
System.out.println("heihei~~");
}
}
System.out.println("The url is null!");
}else{
System.out.println(url.toString());
properties_name = url.getPath();
System.out.println(properties_name);
InputStream inputStream = new FileInputStream(properties_name);
properties.load(inputStream);
host = properties.getProperty("host");
}
不把properties放到jar里的话,
你放到哪儿了?
如果想让java找到的话得放到你执行java的当前目录下相应地方,
放在其它地方是不行的!
"/system.properties");
Properties pros = new Properties();
pros.load(is);
小伙子不错呀是对的了。回答我2个问题:
1System.getProperty("user.dir")是什么意思,解释清楚点,我很笨的。
2我用url = ClassLoader.getSystemClassLoader().getSystemResource("suncertify.properties")也能定位suncertify.properties的路径,为什么打包后运行就不正常了,也解释清楚点。
给你一段核心代码,就可以解决这个问题。
String propname = "mytest.properties";
InputStream in = getClass().getResourceAsStream(propname);
或者
InputStream in = getClass().getClassLoader().getSystemClassLoader().getResourceAsStream(propname);
你的回答好象有问题, wingtrace(暮寒.烟灰.风化物.PG世界最重要的是基础)的代码就是对的。
System.getProperty(String key);
key: the property key
请问the property key主要有那些啊?有没有这个参数的列表,谢谢!
java.version Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
java.home Java installation directory
java.vm.specification.version Java Virtual Machine specification version
java.vm.specification.vendor Java Virtual Machine specification vendor
java.vm.specification.name Java Virtual Machine specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor Java Virtual Machine implementation vendor
java.vm.name Java Virtual Machine implementation name
java.specification.version Java Runtime Environment specification version
java.specification.vendor Java Runtime Environment specification vendor
java.specification.name Java Runtime Environment specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path List of paths to search when loading libraries
java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory
thank you very much
我的API文档上面怎么没有,就是查不到才问你,谢谢
喂,帮人帮到底嘛,为什么我那种方法打包后定位就出错了?还有你用的System.getProperty("user.dir")是什么意思?