浏览 2665 次
锁定老帖子 主题:在JAVA类中指定系统参数的问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-10-25
我想在JAVA中设置系统的trustStore变量,而不是通过启动参数-Djavax.net.ssl.trustStore=trustStore来加载,因为这涉及到修改系统启动参数。 如果我是在起动类的静态块中加载是可以加载成功的,发起SSL请求的时候不会报错:
static{ InputStream is = Thread.currentThread().getClass().getResourceAsStream(CONFIG_PATH+"wss.properties"); try { wsProperties.load(is); Map<String,String> trustStoreProperty = new HashMap<String,String>(); trustStoreProperty.put("javax.net.ssl.trustStore", wsProperties.get(WSUtil.TRUST_STORE).toString()); System.getProperties().putAll(trustStoreProperty); } catch (Exception e) { logger.error("Error happened when load properties file:"+CONFIG_PATH+"wss.properties", e); } } 可是如果我将这段代码加到非启动类,即另外的被调用的类中,就设置不成功。我刚以为是CLASSLOADER的问题,我尝试在被调用类中使用:
static{ Method property=Thread.currentThread().getContextClassLoader().getSystemClassLoader().getParent().loadClass("java.lang.System").getDeclaredMethod("getProperties", null); Properties p = (Properties)property.invoke("getProperties", null); p.putAll(trustStoreProperty); } 也不行,不知道是什么原因了,网上也没有找到相关的答案,有经验的达人给支个招了。
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-10-25
InputStream is = Thread.currentThread().getClass().getResourceAsStream(CONFIG_PATH+"wss.properties");
在打成jar包的时候无法获取到配置文件 改成 URL fileUrl = getClass().getResource(fileName); wsProperties.load(fileUrl.openConnection().getInputStream()); 试试 |
|
返回顶楼 | |
发表时间:2012-10-26
InputStream is = Thread.currentThread().getClass().getResourceAsStream(CONFIG_PATH+"wss.properties");
关键是CONFIG_PATH的值是什么。如果这个值是以/开头,那么会从你的类路径+CONFIG_PATH+"wss.properties"中找这个配置文件。如果不带,则是从这个类所在的包+CONFIG_PATH+"wss.properties"找文件。如果你在A类中可以,B类中不可以。那么肯定就是这的问题。最好是将配置文件CONFIG_PATH+"wss.properties"放在类路径下,然后如果用的Class.getResourceAsStream就需要在开头加上/如果是用ClassLoader.getResourceAsStream则不需要加上/ |
|
返回顶楼 | |