1

having a lil issue, i have create a properties file :

config.properties located in ../resource/config.properties

this is the file currently :

destinationPDF=D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/
destination="D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/
fileList =D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt

have i done the properties file ok ?

also i want to access this file and load the variables into a class

i have tried

public void loadProp() {
    try {
        prop.load(new FileInputStream("../resources/config.properties"));
        System.out.println(prop.getProperty("destinationPDF"));
        System.out.println(prop.getProperty("destination"));
        System.out.println(prop.getProperty("fileList"));

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

but now the class will not compile becuase it can not find variable destination for example, so how do i load the variables from the file, and do i still need to declear the variable in the class ?

sorry if these are silly questions, first time using properties !

i do not get this error if i put in the variables normally like

private String destinationPDF = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/"; //USE ON TORNADO//"D:/My Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/";//USE ON PREDATOR    

EDIT:

have now

private Properties configProp = new Properties();

public void loadProps() {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties");
    try {
        configProp.load(in);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

EDIT 2:

public void loadProp() {
    InputStream in = this.getClass().getClassLoader().getResourceAsStream("../resources/config.properties");
    try {
        prop.load(in);
        System.out.println(prop.getProperty("destinationPDF"));
        System.out.println(prop.getProperty("destination"));
        System.out.println(prop.getProperty("fileList"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Properties prop = new Properties();
private String destinationPDF = prop.getProperty("destinationPDF");
public String destination = prop.getProperty("destination");

it is working, no erors etc but destination and destinationPDF are passing null values

8
  • 3
    Doesn't compile? Or gives errors while running?? Also, there are opening quotation marks on your destination property, but no closing ones.
    – jlordo
    Commented Feb 16, 2013 at 16:10
  • 1
    destination="D:/Documents is a typo or do you have like that in properties file itself? also I assume it is not finding your .properties file location. What stacktrace it is showing? Commented Feb 16, 2013 at 16:10
  • Have you defined prop variable somewhere?
    – Amit
    Commented Feb 16, 2013 at 16:12
  • sorry, it does not compile at all, it gives me the erro cariable desination is not define, and yeas the "D:/ is a type, in the real file there are no " Commented Feb 16, 2013 at 16:12
  • If your compilation error is variable desination is not defined, than your error is not in the code you posted above.
    – jlordo
    Commented Feb 16, 2013 at 16:13

3 Answers 3

3

You seem to misunderstand what properties files are. They're just data. They don't contain Java code, and aren't used to declare variables. To get the value associated to the key destinationPDF in the properties file, you need to call

String destinationPDF = prop.getProperty("destinationPDF");

after having initialized the prop variable and loaded the file using prop.load(new FileInputStream(...)). And then you'll have a variable initialized with the value of the key.

Side note: please respect the Java naming conventions: variables start with a lower-case letter.

5
  • 1
    also, I don't see him calling his loadProp() method.
    – jlordo
    Commented Feb 16, 2013 at 16:27
  • ok i have edited it, now i dont get any errors, but destination and desintationPDF is passing a null value and not the value from the file, any ideas? Commented Feb 16, 2013 at 16:46
  • You're reading values from the prop object before loading the properties file. So there's no property in prop yet.
    – JB Nizet
    Commented Feb 16, 2013 at 16:49
  • im sorry i dont understand, what part of the code is in the wrong palce ? Commented Feb 16, 2013 at 16:54
  • 1
    public String destinationPDF = prop.getProperty("destinationPDF");: the field is initialized as soon as new YourObject() is invoked. And at this time, obviously, the method loadProp() on the object has not been called yet, since the object doesn't even exist yet. That's like trying to drink water from a recipient you're constructing. The recipient must first be constructed, then filled, and then only you can drink what it contains.
    – JB Nizet
    Commented Feb 16, 2013 at 16:58
1

Problem is here:

        // destination = "D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/";  // main location for uploads (CHANGE THIS WHEN USING PREDATOR)
        File theFile = new File(destination + "/" + username);
        theFile.mkdirs();// will create a sub folder for each user (currently does not work, below hopefully is a solution) (DOES NOW WORK)
        System.out.println("Completed Creation of folder");
        NewDestination = destination + username + "/";

You have commented the destination variable and you are using here:

NewDestination = destination + username + "/";
4
  • Yes i commented out the destination variable, as i was hoping to use it from the properties file instead of declearing it in the class Commented Feb 16, 2013 at 16:26
  • @user2065929 then you should remove the code where you are using that undefined variable, that's why you are getting "destination" undefined error. Commented Feb 16, 2013 at 16:28
  • @user2065929: You have a misunderstanding of properties. See JB Nizet's answer, he explains why you can't use keys from properties files as variable names in your code.
    – jlordo
    Commented Feb 16, 2013 at 16:29
  • @ user2065929 - you have the mistaken assumption that loading property files creates variables in your code. It does not. All it creates is a dictionary of string values that you can look up by (string) keys.
    – Perception
    Commented Feb 16, 2013 at 16:33
0

I wonder whats the issue...I tested your code and it works fine...are you getting compilation error or runtime error?

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


public class Test1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        new Test1().loadProp();
    }

    Properties prop = new Properties();

    public void loadProp() {
        try {
            prop.load(new FileInputStream("c:/Test/Computer.txt"));
            System.out.println(prop.getProperty("destinationPDF"));
            System.out.println(prop.getProperty("destination"));
            System.out.println(prop.getProperty("fileList"));

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

Output:

D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/ D:/Documents/NetBeansProjects/printing~subversion/fileupload/Uploaded/ D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/Directory Files/directoryFiles.txt

2
  • You have much more code than the OP. He's missing the declaration (and initialization) of his variable(s).
    – jlordo
    Commented Feb 16, 2013 at 16:20
  • have posted my code above, i think i am not loading prop, is this the case ? Commented Feb 16, 2013 at 16:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.