need help with runtime constants

E

El Durango

I am having problems with initializing runtime constants.
Basically I have a config file but would like the value data to be assigned
to a constant rather than an instance variable.

I tried the following code but it is not working there are 3 files pasted
here:

package proptest;

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

public class PropertyInitializer{
String CONFIGFILENAME = "ixmlsysinfo.config";
private static Properties props = new Properties();
FileInputStream fis = null;

PropertyInitializer(){
try{
fis = new FileInputStream(CONFIGFILENAME);
props.load(fis);
}
catch(FileNotFoundException fnf){
// stump
}
catch(IOException ioe){
// stump
}
}
public static String confValue(String key){
String propVal = props.getProperty(key);
return propVal;
}
}



package proptest;

public interface PropertyInfo
{
public static final String CONFIGPATH =
PropertyInitializer.confValue("ConfigPath");
public static final String RTFPATH =
PropertyInitializer.confValue("RTFDir");
public static final String XMLPATH =
PropertyInitializer.confValue("ProcedurePath");
public static final String IPVPATH =
PropertyInitializer.confValue("IPVPath");
public static final String IPVURL =
PropertyInitializer.confValue("IPVUrl");
public static final String XSLFILEPATH =
PropertyInitializer.confValue("XSLFilePath");
public static final int INTVALUE = (int)Math.random();
}


import proptest.PropertyInfo;

public class PropertyDriver implements PropertyInfo{

public static void main(String[] args){
System.out.println("CONFIGPATH: "+CONFIGPATH);
System.out.println("RTFPATH: "+RTFPATH);
System.out.println("XMLPATH: "+XMLPATH);
System.out.println("IPVPATH: "+IPVPATH);
System.out.println("IPVURL: "+IPVURL);
System.out.println("XSLFILEPATH: "+XSLFILEPATH);
System.out.println("INTVALUE: "+INTVALUE);
}
}

The reason I would like to keep it like this is I would like to reference
these variables like a global const by implementing the interface on the
classes that call these constants.
If anyone has a good suggestion I would appreciate it.
thank you for your assistance.
 
T

Tony Morris

El Durango said:
I am having problems with initializing runtime constants.
Basically I have a config file but would like the value data to be assigned
to a constant rather than an instance variable.

I tried the following code but it is not working there are 3 files pasted
here:

package proptest;

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

public class PropertyInitializer{
String CONFIGFILENAME = "ixmlsysinfo.config";
private static Properties props = new Properties();
FileInputStream fis = null;

PropertyInitializer(){
try{
fis = new FileInputStream(CONFIGFILENAME);
props.load(fis);
}
catch(FileNotFoundException fnf){
// stump
}
catch(IOException ioe){
// stump
}
}
public static String confValue(String key){
String propVal = props.getProperty(key);
return propVal;
}
}



package proptest;

public interface PropertyInfo
{
public static final String CONFIGPATH =
PropertyInitializer.confValue("ConfigPath");
public static final String RTFPATH =
PropertyInitializer.confValue("RTFDir");
public static final String XMLPATH =
PropertyInitializer.confValue("ProcedurePath");
public static final String IPVPATH =
PropertyInitializer.confValue("IPVPath");
public static final String IPVURL =
PropertyInitializer.confValue("IPVUrl");
public static final String XSLFILEPATH =
PropertyInitializer.confValue("XSLFilePath");
public static final int INTVALUE = (int)Math.random();
}


import proptest.PropertyInfo;

public class PropertyDriver implements PropertyInfo{

public static void main(String[] args){
System.out.println("CONFIGPATH: "+CONFIGPATH);
System.out.println("RTFPATH: "+RTFPATH);
System.out.println("XMLPATH: "+XMLPATH);
System.out.println("IPVPATH: "+IPVPATH);
System.out.println("IPVURL: "+IPVURL);
System.out.println("XSLFILEPATH: "+XSLFILEPATH);
System.out.println("INTVALUE: "+INTVALUE);
}
}

The reason I would like to keep it like this is I would like to reference
these variables like a global const by implementing the interface on the
classes that call these constants.
If anyone has a good suggestion I would appreciate it.
thank you for your assistance.

If I went to the motor mechanic, and told him that "my car isn't working",
would you consider that useful information ?
Or would you think that more information is required - such as "What" and
"when" and "why" and "what makes you think it" isn't working ?

The code that you have pasted has numerous flaws in it. Is one of these what
isn't working ?
For example, does "not working" mean "has a resource leak" ? If it is, then
close the FileInputStream.
Or does "not working" mean something else ?

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
E

El Durango

Sorry for the incomplete info I rushed my post.
The output I expect should contain the value data of a config file which I
did not include but has the basic format of:

Key:Value

so for instance the config file called ixmlsysinfo.config has the key/value
combo of:
ConfigPath:work/drivea/foldera
RTFDir:work/drivea/folderb
....etc.

I would expect the PropertyInitializer class to retreive and pass this info
to the PropertyInfo interface which defines the global constants. Then the
PropertyDriver which implements the interface can freely call these "runtime
constants".
Therefor the info on the screen when the PropertyDriver is run should
have:
work/drivea/foldera
work/drivea/folderb
....
...
etc.
The last value should be a random integer.

However I get the following:
null
null
....
...
0

I read somewhere that this should work, but it seems I may have received
misinformation.
As I mentioned before I want to have runtime global constants if possible.
The data does not change within the program but gets initialized at runtime.
If this is not possible is there a different solution where I can mimic this
behaviour?
Sorry again for the lack of info I initially posted. I hope I cleared it up.
 
A

Andrew Hobbs

El Durango said:
I am having problems with initializing runtime constants.
Basically I have a config file but would like the value data to be assigned
to a constant rather than an instance variable.

I tried the following code but it is not working there are 3 files pasted
here:

package proptest;

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

public class PropertyInitializer{
String CONFIGFILENAME = "ixmlsysinfo.config";
private static Properties props = new Properties();
FileInputStream fis = null;

PropertyInitializer(){
try{
fis = new FileInputStream(CONFIGFILENAME);
props.load(fis);
}
catch(FileNotFoundException fnf){
// stump
}
catch(IOException ioe){
// stump
}
}

1. Place appropriate messages after try{} catch() blocks. You never know
whether that is your problem

2. What does your config file look like. Are the properties and values
separated by either '=' , ';' or whitespace.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************


public static String confValue(String key){
String propVal = props.getProperty(key);
return propVal;
}
}



package proptest;

public interface PropertyInfo
{
public static final String CONFIGPATH =
PropertyInitializer.confValue("ConfigPath");
public static final String RTFPATH =
PropertyInitializer.confValue("RTFDir");
public static final String XMLPATH =
PropertyInitializer.confValue("ProcedurePath");
public static final String IPVPATH =
PropertyInitializer.confValue("IPVPath");
public static final String IPVURL =
PropertyInitializer.confValue("IPVUrl");
public static final String XSLFILEPATH =
PropertyInitializer.confValue("XSLFilePath");
public static final int INTVALUE = (int)Math.random();
}


import proptest.PropertyInfo;

public class PropertyDriver implements PropertyInfo{

public static void main(String[] args){
System.out.println("CONFIGPATH: "+CONFIGPATH);
System.out.println("RTFPATH: "+RTFPATH);
System.out.println("XMLPATH: "+XMLPATH);
System.out.println("IPVPATH: "+IPVPATH);
System.out.println("IPVURL: "+IPVURL);
System.out.println("XSLFILEPATH: "+XSLFILEPATH);
System.out.println("INTVALUE: "+INTVALUE);
}
}

The reason I would like to keep it like this is I would like to reference
these variables like a global const by implementing the interface on the
classes that call these constants.
If anyone has a good suggestion I would appreciate it.
thank you for your assistance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top