I'm doing internationalisation in Java.
I've created two files: MessageBundle_en_US.properties and MessageBundle_en_IN.properties.
MessageBundle_en_US.properties:
greeting=Hello, how are you?
MessageBundle_en_IN.properties
greeting=Halo, apa
And finally, here's the main class:
import java.util.*;
public class InternationalizationDemo {
public static void main(String[] args) {
ResourceBundle bundle =
ResourceBundle.getBundle("MessageBundle", Locale.US);
System.out.println(
"Message in "
+ Locale.US
+ ": "
+ bundle.getString("greeting")
);
//changing the default locale to indonasian
Locale.setDefault(new Locale("in", "ID"));
bundle = ResourceBundle.getBundle("MessageBundle");
System.out.println(
"Message in "
+ Locale.getDefault()
+ ": "
+ bundle.getString("greeting")
);
}
}
And I receive the following error stacktrace:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name MessageBundle, locale en_US
at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593)
at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1556)
at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:932)
at provame.InternationalizationDemo.main(InternationalizationDemo.java:8)
UPDATE
I've found the reason it was unable to see the file, first of all you need to put *properties file in the same directory as the main class, as the kind people suggested in the comments, and you should adjust the call to the file like that:
projectName/propertiesFileName
This was all.
Place your *.properties files in the same folder as InternationalizationDemo.java
Rename: MessageBundle_en_IN.properties to: MessageBundle_in_ID.properties
Related
//PRE-SET VARIABLES: symbolsToCheck, time
for (String s : symbolsToCheck) {
String fileName = "daylogs-" + time + "/" + s + ".txt";
File daylog = new File(fileName);
if (!daylog.exists()) {
if (!daylog.createNewFile()) {
System.out.println("ERROR creating day log for " + s);
} else {
System.out.println("Day log created: " + daylog.getCanonicalPath());
}
} else {
System.out.println("ERROR day log already exists for " + s);
}
}
Nothing is outputted from this, and I've confirmed that symbolsToCheck is populated (roughly a dozen strings). I can also confirm that time is set (integer timestamp) well before this code snippet is called. Been scratching my head for quite some time now, any ideas?
I've found the solution from a related post and Tom's suggestion, I've determined that the file creation is breaking due to my attempts to create a new folder and file at the same time, which does not work with createNewFile(). I followed the suggested in the related post and file creation works as expected now.
I'm working with the dropbox api to java and i have trying to read from dropbox.
When I use the path of the api it looks like this -
טיול פסח path: /Public/טיול פסח
(Ignore the hebrew words it's like -
someFolderName paht: /Public/someFolderName)
and it's looks as usual, but when I try to go to this path it's give me this exception -
Exception in thread "main" java.lang.NullPointerException
at node.Main.listFolders(Main.java:102)
at node.Main.main(Main.java:112)
When I use the toString() method of the api that give some of the metaData including the path of the folder it's shows me this -
טיול פסח toString(): Folder("/Public/\u05d8\u05d9\u05d5\u05dc \u05e4\u05e1\u05d7"...)
When the string within the quotes should be the path.
When I tried to enter this as the path it's give me this exception -
Exception in thread "main" java.lang.NullPointerException
at node.Main.listFolders(Main.java:102)
at node.Main.main(Main.java:112)
The code example:
The path is "/Public" and it's the argument s.
client is the access to my dropbox.
listing is list of children of the root folder.
public static void listFolders(String s) throws DbxException, IOException {
//list of children.
DbxEntry.WithChildren listing = client.getMetadataWithChildren(s);
for (DbxEntry child : listing.children) {
System.out.println(" " + child.name + " toString(): " + child.toString());
System.out.println(" " + child.name + " path: " + child.path);
}
}
The output of this with "/Public" as argument is:
טיול פסח toString(): Folder("/Public/\u05d8\u05d9\u05d5\u05dc \u05e4\u05e1\u05d7", iconName="folder", mightHaveThumbnail=false)
טיול פסח path: /Public/
Any thoughts?
I try to access a network folder / UNC path from Java on Mac OSX. On Windows, the following test program works fine (at least one of the tested paths):
public class PathTest {
public static void main(String[] args) {
for (String path : Arrays.asList(
"\\\\myserver\\transfer", "//myserver/transfer", "file://myserver/transfer", "smb://myserver/transfer")) {
File f = new File(path);
System.out.println(path + ": " + f.getAbsolutePath() + ", " + f.exists());
Path p = Paths.get(path);
System.out.println(path + ": " + p.toAbsolutePath() + ", " + Files.exists(p));
}
}
}
on Mac OS it fails to reach the folders:
\\myserver\transfer: /Users/tim/IdeaProjects/PathTest/\\myserver\transfer, false
//myserver/transfer: /myserver/transfer, false
file://myserver/transfer: /Users/tim/IdeaProjects/PathTest/file://myserver/transfer, false
smb://myserver/transfer: /Users/tim/IdeaProjects/PathTest/smb://myserver/transfer, false
When I use Finder, I can access the Folder (using the Guest user), by using "smb://myserver/transfer". What's wrong?
EDIT added NIO.2 test
Either mount the partition and access it as any local directory or use a specialized library such as JCIFS or Apache Commons VFS.
I have an application which deals with jdbc. It supposes to be used in any PC where there is JRE, but it does not suppose that use will use -cp command line or change his/her classpath variables. So the user has my application, JRE and a jdbc driver somewhere in file system. Now he or she enters a database connection information including path to jdbc driver jar and then make sql request. The problem is that I don't now how to make jdbc driver classes to be accessible in this application. The same way as the user explicitly add a driver to classpath.
I just altered part of the miks answer for your other posting.
Executing the following code got me a success.
import java.io.File;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
public class URLClassLoaderSample {
public static void main( String [] args ) throws Exception {
File f = new File( "/home/ravinder/Desktop/mysql-connector-java-5.1.18-bin.jar" );
URLClassLoader urlCl = new URLClassLoader( new URL[] { f.toURL() }, System.class.getClassLoader() );
Class mySqlDriver = urlCl.loadClass( "com.mysql.jdbc.Driver" );
System.out.println( mySqlDriver.newInstance() );
System.out.println( "Is this interface? = " + mySqlDriver.isInterface() );
Class interfaces[] = mySqlDriver.getInterfaces();
int i = 1;
for( Class _interface : interfaces ) {
System.out.println( "Implemented Interface Name " + ( i++ ) + " = " + _interface.getName() );
} // for(...)
Constructor constructors[] = mySqlDriver.getConstructors();
for( Constructor constructor : constructors ) {
System.out.println( "Constructor Name = " + constructor.getName() );
System.out.println( "Is Constructor Accessible? = " + constructor.isAccessible() );
} // for(...)
} // psvm(...)
} // class URLClassLoaderSample
Output seen is as follows:
com.mysql.jdbc.Driver#60aeb0
Is this interface? = false
Implemented Interface Name 1 = java.sql.Driver
Constructor Name = com.mysql.jdbc.Driver
Is Constructor Accessible? = false
And I don't understand what I should with log4jClass variable in my case *(com.mysql.jdbc.Driver)
Let me hope now you got it.
The best solution in this instance would be to distribute the required driver with your application and include either an executable wrapper or a shell script that sets the required variables accordingly. That allows the user to use it out of the box without having to mess with any complicated configuration and also doesn't require them to download any additional files.
Well, jdbc uses Class.forName("org.postgresql.Driver"); to load the driver. So once, you have the jar file, and have added it to the class-path, this part is easy. Just keep a hash of driver fqn classnames to jar file names. Or you can scan the jar for the Driver class.
Here's a convienent answer to how to add the jar file to the classpath once you find it.
i write this code to read json data , and there is an error when run the code
first this is the code i write ( i changed the code to correct the json string but the problem still exist )
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class defaults {
public static void main(String[] args) {
String jsonTxt = "{lhs: \"100 Euros\",rhs: \"128.551738 Australian dollars\",error: \"\",icc: true}";
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
String title = json.getString("title");
System.out.println( "title: " + title );
}
}
and i have found this error when run the code
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source).....
the error gone away if i remove lines that talks about json
You're missing the Apache Commons lang library from your classpath.
If you ever are stumped by a NoClassDefFoundError try plugging the class name into jarFinder - that will tell you jar files where the class can be found.
String jsonTxt = "{'lhs': '100 Euros','rhs':'128.551738 Australian dollars','error':'','icc': 'true'}";
JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );
System.out.println( "lhs: " + json.getString("lhs") );
System.out.println( "rhs: " + json.getString("rhs") );
System.out.println( "error: " + json.getString("error") );
System.out.println( "icc: " + json.getString("icc") );
OUTPUT:
lhs: 100 Euros
rhs: 128.551738 Australian dollars
error:
icc: true
you can give the json string with double quotes(") or single quotes(') or key without quotes. All works.
you need following jars:
1. commons-lang-2.4.jar
2. ezmorph-1.0.jar
3. json-lib-0.9.jar
for adding the jars through eclipse:
1.right click on project folder
2.click on prperties
3.select "java build path"
4.select libraries tab
5.click on "Add External jars"
6.Browse your jars, select and click ok.
You have a hanging "," after 'true'.
And technically strings and property names in JSON should use double quotes, not single quotes.
You doesn't have Apache commons-lang in your runtime classpath.