Loading properties file in GWT on Tomcat

后端 未结 2 1623
猫巷女王i
猫巷女王i 2021-01-23 15:58

I know there are lots of similar questions out there, but none of which that helped. We have a GWT application running on a tomcat server, which is loading a properties file for

相关标签:
2条回答
  • 2021-01-23 16:24

    If you properties file is in the same directory as the class you use to call "getResourceAsStream(..)", then you just need the name of the file, without the absolute path to it. For example, supposing you have:

    // notice that your build should copy this from the source directory to your /WEB-INF/classes/.... directory automatically so you can do a clean build
    com/x/monitorui/server/configuration.properties
    

    and a class located at the same directory, ie. in package:

    com.x.monitorui.server.DatabaseConnection
    

    then you can simply call

    Properties properties = new Properties();
    properties.load(DatabaseConnection.class.getResourceAsStream("configuration.properties"));
    
    0 讨论(0)
  • 2021-01-23 16:27

    Wo do nearly the same here with "Packager.class" as a class in the same directory; The idea here is to find the path via the class path, which works on tomcat:

    final ClassPool pool = ClassPool.getDefault( );
    final Class<?> baseClass = Packager.class;
    pool.insertClassPath( new ClassClassPath( baseClass ) );
    
    final URL url = pool.find( baseClass.getName( ) );
    
    String pathToBaseClass = url.toURI( ).getPath( );
    String baseDir = pathToBaseClass.substring( 0, pathToBaseClass.indexOf( "com" ) - 1 );
    
    0 讨论(0)
提交回复
热议问题