質問

I'm having serious trouble trying to load a resource into a bundle in my application. I've been at this for hours and I really don't know what I'm doing wrong.

I have a Java application where I'm attempting to apply internationalization to my logging. Everything works fine in Eclipse, it's only when I attempt to deploy the compiled jars to the test environment that the properties file cannot be located. My properties file is called logging_en.properties. I've verified that it's on the file system (Linux, by the way). When I launch my application, I execute the following command:

$JAVA_HOME/bin/java -verbose:gc -Xloggc:/var/tmp/app_gc.log -Xms1000m -Xms1000m  -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:NewRatio=3  -classpath $CLASSPATH -Djava.rmi.server.codebase=file:///data/dev/app/common/ext/dirmi-1.1.1.jar -Djava.awt.headless=true -Dcom.sun.management.jmxremote.port=xxx -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false $JAVA_SECURITY $IMPORT_PROPERTIES $APP_PROPERTIES app.serverimpl.ServerStarter

My CLASSPATH variable is:

/data/dev/app/server/logging_en.properties:/data/dev/app/common/lib/common.jar:/data/dev/app/common/ext/dirmi-1.1.1.jar:/data/dev/app/common/ext/log4j-1.2.17.jar:/data/dev/app/common/ext/cojen-2.2.3.jar:/data/dev/app/common/lib/server.jar

The properties file is in my classpath. In the code itself, the exact line I run is:

LogManager.getLogger("server").setResourceBundle(java.util.ResourceBundle.getBundle("logging"));

At the very beginning when the server starts, I invariably get the Missing Resource exception:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name logging, locale en_US

Just to be safe, I've gone so far as to copy the file to logging.properties and logging_en_US.properties and added all three to my classpath. Still the same issue. I know I'm doing something stupid or missing something obvious, but I'm really at a loss. I'd prefer not to bundle the file into my jar so it can be modified on the fly, so I'm hoping it's possible to do this from the classpath.

役に立ちましたか?

解決

For individual files, the classpath needs to contain the folder where the files(s) are located, not the path to the actual file itself. Change your classpath to read like:

/data/dev/app/server/:/data/dev/app/common/lib/common.jar:...

(Jar files are treated like folders, as you can see above).

To be clear - the ResourceBundle.getBundle("logging") call is going to be doing the equivalent of something like this internally:

ClassLoader.getSystemClassLoader().getResource("logging_en.properties")

and when it does that, it will look for that path ("logging_en.properties") in the folders on the classpath - which means it will look in /data/dev/app/server and then inside the /data/dev/app/common/lib/common.jar file (it has a cache of the unzipped contents of it) and so on. (There is more pluggability involved and not all class loaders have to correspond to files, etc. - but in the common case the above is what does occur.)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top