高分相送,知道答案的请进,有结果就结贴!

guxinglou 2004-09-14 12:16:58
有一个这样的JAVA文件,文件名为:test.java,内容如下:

import java.io.File;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

class Test
{

private static String dsName = null;
private static String username = null;
private static String password = null;

public static void main(String args[])
throws Exception
{
String cfgFileName = null;
try
{
cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
File cfgFile = new File(cfgFileName);
FileInputStream in = new FileInputStream(cfgFile);
Properties p = new Properties();
p.load(in);
dsName = p.getProperty("system.datasource.name");
username = p.getProperty("system.database.username");
password = p.getProperty("system.database.password");
in.close();
}
catch(Exception ex)
{
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("Load from configfile(")).append(cfgFileName).append(") error . ").append(ex.getMessage()))));
}
}
}

在命令行键入:javac test.java //正常
输入:java Test //提示如下信息:

Exception in thread "main" java.lang.Exception:Load from configfile(null) error . null
at Test.main(test.java:31)

我想请问一下:CONFIG_FILE 文件应该放在哪里,java Test通过后就结贴。

先谢谢大家,分不够,可再加。

我已经在环境变量里面设置了一个值:CONFIG_FILE,值为:D:\oracle\ora92\Apache\Apache\htdocs\system\config.txt,一个实际存在的文件。

但提示错误照旧!!

请大家指点。


高分相送,顶者也有分!!!

...全文
195 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyforlove 2004-09-14
  • 打赏
  • 举报
回复
还是那句话,你自己设的环境变量,是不可以直接读出的。
kingfish 2004-09-14
  • 打赏
  • 举报
回复
jvm参数

-D D:\oracle\ora92\Apache\Apache\htdocs\system\config.txt
flyforlove 2004-09-14
  • 打赏
  • 举报
回复
sorry,-D后面是没有空格的。
为了看着方便,你可以这样。
java -Dconfig_file=%CONFIG_FILE% test

读取的时候cfgFileName = System.getProperty("config_file");
guxinglou 2004-09-14
  • 打赏
  • 举报
回复
呵呵,这个程序按道理是没有错误的,错误的就是配置问题了。

问题出在:
File cfgFile = new File(cfgFileName);
FileInputStream in = new FileInputStream(cfgFile);
Properties p = new Properties();
p.load(in);
dsName = p.getProperty("system.datasource.name");
username = p.getProperty("system.database.username");
password = p.getProperty("system.database.password");
in.close();
大家方便的话,不妨帮忙调试一下。再列出正确的代码及配置,谢谢啊!
flyforlove 2004-09-14
  • 打赏
  • 举报
回复
你在环境变量里设置文件路径,是不可以在java里直接得到的,
除非你在运行的时候加个开关,
比如java -D CONFIG_FILE=%CONFIG_FILE% test

你可以试试看。
stonecsdn 2004-09-14
  • 打赏
  • 举报
回复
InputStream inputStream =new FileInputStream("server.properties");//文件路径

Properties property = new Properties();

property.load(inputStream);
xch28 2004-09-14
  • 打赏
  • 举报
回复
你单步调试一下。
cfgFileName = System.getProperties().getProperty("CONFIG_FILE");
cfgFileName 是不是为null
另外,System.getProperties()是获取系统属性的。
System.getProperties().getProperty("java.home");
你可以看一下jdk的帮助。
guxinglou 2004-09-14
  • 打赏
  • 举报
回复
我设置的文件名为:config.txt内容如下:

system.datasource.name = system
system.database.username = system
system.database.password = test

不知道文本文件里面的文件是不是这样设置的呢?
bs221cn 2004-09-14
  • 打赏
  • 举报
回复
up
pcdll 2004-09-14
  • 打赏
  • 举报
回复
看你是怎么读取CONFIG_FILE的了,如果直接读,放在你和你的class一个目录下面,如果指定了路径,就不用多说了.
szabo 2004-09-14
  • 打赏
  • 举报
回复
将其路径全写上试试。
smallMage 2004-09-14
  • 打赏
  • 举报
回复
文件是在当前目录下吗
wxt1013 2004-09-14
  • 打赏
  • 举报
回复
给你一个关于Properties读与写的实例,希望对你有用。这个类已经测试通过。
注:要在你的classpath下新建一个文件:fda_init.properties,才能正常运行

import java.io.*;
import java.util.*;

public class PropertiesOperate {

public int init_flag = 0;
public int init_step = 0;
public String driver = "";
public String db_ip = "";
public String db_name = "";
public String user = "";
public String password = "";

public static void main(String[] args) {
PropertiesOperate po;

po = new PropertiesOperate();
po.readDBParam();
po.db_name = "scc";
po.saveDBParam();

po = new PropertiesOperate();
po.readDBParam();
System.out.println(po.db_name);
po.db_name = "test";
po.saveDBParam();

po = new PropertiesOperate();
po.readDBParam();
System.out.println(po.db_name);
}

public boolean readDBParam() {
Properties initProps = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream("/fda_init.properties");
initProps.load(in);
}
catch (Exception e) {
System.err.println("Error reading FDA properties "
+ "in FDAGlobals");
e.printStackTrace();
return false;
}
finally {
try {
if (in != null) { in.close(); }
} catch (Exception e) {}
}

String temp;
if (initProps != null) {
temp=initProps.getProperty("initFlag");
try{
init_flag = Integer.parseInt(temp);
} catch (Exception e1) {
init_flag = -1;
}

temp=initProps.getProperty("initStep");
try{
init_step = Integer.parseInt(temp);
} catch (Exception e1) {
init_step = -1;
}

driver = initProps.getProperty("dbDriver");
db_ip = initProps.getProperty("dbIP");
db_name = initProps.getProperty("dbName");
user = initProps.getProperty("dbUsername");
password = initProps.getProperty("dbPassword");

return true;
}

return false;
}

public boolean saveDBParam(){
Properties initProps = new Properties();
OutputStream out = null;
try{
initProps.setProperty("initFlag",""+getInit_flag());
initProps.setProperty("initStep",""+getInit_step());
initProps.setProperty("dbDriver", getDriver());
initProps.setProperty("dbIP", getDb_ip());
initProps.setProperty("dbName", getDb_name());
initProps.setProperty("dbUsername", getUser());
initProps.setProperty("dbPassword", getPassword());
String fileName=getClass().getResource("../fda_init.properties").getFile();
out=new FileOutputStream(fileName);
initProps.store(out,"");
out.flush();
out.close() ;
return true;
}catch(Exception e){
e.printStackTrace() ;
System.out.println("Some exception happened!");
try{
out.close();
}catch(Exception er){}
return false;
}
}


/**
* @return
*/
public String getDb_ip() {
if(db_ip==null) return "";
else return db_ip;
}

/**
* @return
*/
public String getDb_name() {
if(db_name==null) return "";
else return db_name;
}

/**
* @return
*/
public String getDriver() {
if(driver==null) return "";
else return driver;
}

/**
* @return
*/
public int getInit_flag() {
return init_flag;
}

/**
* @return
*/
public int getInit_step() {
return init_step;
}

/**
* @return
*/
public String getPassword() {
if(password==null) return "";
else return password;
}

/**
* @return
*/
public String getUser() {
if(user==null) return "";
else return user;
}

/**
* @param string
*/
public void setDb_ip(String string) {
db_ip = string;
}

/**
* @param string
*/
public void setDb_name(String string) {
db_name = string;
}

/**
* @param string
*/
public void setDriver(String string) {
driver = string;
}

/**
* @param string
*/
public void setInit_flag(int flag) {
init_flag = flag;
}

/**
* @param string
*/
public void setInit_step(int step) {
init_step = step;
}

/**
* @param string
*/
public void setPaswword(String string) {
password = string;
}

/**
* @param string
*/
public void setUser(String string) {
user = string;
}

}

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧