0

I want to read property file on Server side. I have DBConfig.java, useDBConfig.java and DBConfig.properties all placed in server package. I can't read the values from property file on Server Side. Your help is highly appreciated.

public interface DBConfig extends Constants {
@DefaultStringValue("host")
String host(String host);

@DefaultStringValue("port")
String port(String port);

@DefaultStringValue("username")
String username(String username);

@DefaultStringValue("password")
String password(String password);

}


public void useDBConfig() {
DBConfig constants = GWT.create(DBConfig.class);
Window.alert(constants.host());


host = constants.host(host);
port = constants.port(port);
username = constants.username(username);
password = constants.password(password);

}

property file...

host=127.0.0.1
port=3306
username=root
password=root

Thanks in advance.

Prasanth S
  • 3,725
  • 9
  • 43
  • 75
  • This [post](http://stackoverflow.com/questions/22269623/retrieve-init-parameters-outside-servlet/22270264#22270264) might help you. – Braj Aug 06 '14 at 09:51
  • "I can't read the values from property file on Server Side" - Do you get some kind of an error? please post stacktrace. – Ovi Faur Aug 06 '14 at 12:50

1 Answers1

1

GWT.Create can be used only in client mode. Are you sure that code execute in server side?

If i write in my application GWT.Create in server side i get this error:

java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in client code! It cannot be called, for example, from server code. If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor.

You can read a properties files in java. The file is similar that Constants files in GWT. Example of Properties file:

key = value host = 127.0.0.1 port = 80 username = guest password = guest

EOF

You can read this file, see the next code:

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

String fileToRead = "MY_PATH"+File.separator+"MY_FILE.properties";
Properties prop = new Properties();
try {
    File propertiesFile = new File(fileToRead);
    prop.load(new FileInputStream(propertiesFile));
    String host = prop.getProperty("host");
    String port = prop.getProperty("port");
    String username = prop.getProperty("username");
    String password = prop.getProperty("password");

    System.out.println(host);
    System.out.println(port);
    System.out.println(username);
    System.out.println(password);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

If the key doesnt exists getProperty(String key) return null. You can use prop.containsKey(String key); to see if the key exists. This function return a boolean (True if exists False in other case).

Greetings

user3909865
  • 175
  • 1
  • 7
  • I executed that code on server side. But you are right it is not supposed to run. Could you please tell me how to read that property file in Server Package. Plz! – Xerox Shah Aug 06 '14 at 12:24
  • I added a code for read properties file in java. You can use this for server side in your project. – user3909865 Sep 01 '14 at 09:14